Rx Extensions
A set of useful extensions and operators for RxJS
State Store
Specialized subject for storing and modifying application state. Inspired by the NgRx Store.
Usage
// instantiate with initial stateconst stateStore = <MyState>INITIAL_STATE; // call modify to update the state// modify takes 2 arguments, some data to feed into the modifier, and the modifier itself which is a function that updates the state const modifier = state: MyState data: MyModifierData: { // use non mutative methods to update state return ...state ...data;} stateStore; // modifiers can also return observables of dataconst modifier$ = state: MyState data$: Observable<MyModifierData>: Observable<MyState> return data$; stateStore; // get data with select, feed as many key arguments as needed for nested keysstateStore; // calling next will reset the data modifier to whatever is fed as an argument// semantic reset method is available as a wrapperstateStore
Data Store
Specialized extension of StateStore for data that meets the interface {[key: string]: T}
Usage
// initial state is optional, defaults to {}const dataStore = <MyData> // set data key with set, can also take observable of the data (this is true for all set and update operations)dataStore // get data key with getdataStore; // update data key with update, takes modifier of the data at the key and data to update (can return observable)// use set to totally replace, use update when you need the data in the store to make the updateconst myUpdateModifier = { return ...state ...data;}dataStore; // delete keys with deletedataStore; // the above all also have `Many` variations:dataStore dataStore; // updateMany takes an argument of an object with keys to update and the data to use in the update, and the modifier to run on each keyconst myUpdateManyModifier = { return ...state ...data;}dataStore; dataStore; // reset data, takes optional argument of data state, will be {} by defaultdataStore
Resource Store
Specialized extension of DataStore that deals with a remote resource
Usage
// two ways to use // 1. via construction that takes a config with (optional) inistial state and a function to fetch the remote resource : Observable<MyType> { return http;} const resourceStore = <MyType string>getResource; // 2. or you may extend the AbstractResourceStore and provide a getResource function <MyType string> { return http; } // both methods may define a key builder function as well in the case of more complex input: type MyInput = id: string ; { return inputid} const resourceStore = <MyType string>getResource keyBuilder; <MyType string> { return http; } { return inputid } // the resource store provides a cacheGet method that will check the cache, if it's not there, it will call the getResource method and set it resourceStore // it also provides a cacheSet method that gets the remote resource and populates the cache with the response resourceStore
Array Store
Specialized extension of StateStore for data that meets the interface Array<T>
Work In Progress... Documentation coming
Pull Subject
Implements observer pattern to pull responses from observers and gather responses
Usage
// using this interfaceinterface MyType key: string; key2: number; // declare like a normal subject, optionally declare with a collector reducer// default collector reducer merges partials like {...collected, ...collector}const pullSubject = <MyType>; // use pull method to register a collector, which needs to return a partial typepullSubject; // register async collectors with pullASync (these MUST complete!)pullSubject; // subscribe to the pull subject to receive the merged result of all collectorspullSubject; // trigger the collection with pull subject next (you can pass initial or default values for the reducer here, defaults to {})pullSubjectnext; // deregister a collector with the pull return valueconst pullSub = pullSubject; pullSub;
Utilities and Operators
concatJoin
Like forkJoin
but executes streams sequentially
batchJoin
Like forkJoin
but executes streams sequentially in batches of a specified size, default 5
pluckDistinct
combines pluck
and distinctUntilChanged
pluckManyLatest
plucks several keys and emits all when any of them change
pluckManyMerge
plucks several keys and the key that changed when it changes, along with the key that changed