rx-thunk-subject
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

rx-thunk-subject

An rx subject from an async thunk.

  • The thunk is not executed until the first subscription is done
  • The value can be invalidated thus refreshing existing subscribers values
  • Useful for managing changing values from the server
  • Subscribers only need to manage sync values

Create a ThunkSubject from an async thunk

async function query() : Promise<Data> {
    return fetch("/api/myquery");
}

//The query function will not be executed until the first subscribe call
const data = new ThunkSubject(query);

Subscribe to your data

function handleData(value: Data) {
    //handle your data.
}

//The query function is called, handleData gets the query promise
data.subscribe(handleData)

...
//Other handler receives the last thunk value, the query function is not called
data.subscribe(otherHandler);

Refresh your data

//The query function is called if there are any subscribers, all subscribers get the new value
//If there isn't any subscribers, the function is called on the next subscription
data.invalidate();

Readme

Keywords

none

Package Sidebar

Install

npm i rx-thunk-subject

Weekly Downloads

1

Version

2.0.2

License

MIT

Last publish

Collaborators

  • rafaelsalguero