Ok so you like load 'n' cache but prefer Observables instead of Promises? No problem at all! This small package provides a tiny layer to provide native Observable support. Internally it still uses Promises and async/await
code but you don't need to worry about.
If you can see a reason to fork the project to cut out promises entirely please let me know.
This project provides an rxjs compliant operator that:
These steps can be achieved easily using a ReplaySubject
and the share()
operator but, using this package you can also:
This library provides a one-stop solution to application level resource caching in a rxjs environment. You can achieve a similar result using the rxjs operators and some custom logic but it would be much harder to maintain.
The main class of this project is LoadNCacheObservable
that can be used directly or, in more .pipe()
friendly way, using the loadNCache()
function. Piping any observable through the loadNCache()
function result in caching its last emitted value. Any subsequent subscription will receive the cached value and the inner subscriber is left alone (unless a flush occurs).
When using a LoadNCache instance directly you can call the flush()
method to clear the cache value. For better rxjs integration the loadNCache()
function accepts an option: flushOn
. flushOn
is a function that should return an Observable
that will emit when the value must be flushed.
See the usage section for an example.
Check out https://monesidn.github.io/load-n-cache-rxjs/
npm install --save load-n-cache-rxjs
and then:
import { loadNCache } from "load-n-cache-rxjs";
public randStr$ = new Observable<string>((sub) => {
sub.next(randomStr());
sub.complete();
})
.pipe(loadNCache());
Just pipe you source observable into the loadNCache function to start caching.
public randStr$ = new Observable<string>((sub) => {
sub.next(randomStr());
sub.complete();
})
.pipe(loadNCache({ flushOn: () => timer(1000) }));
The timer(1000)
observable is subscribed after fetching a new value. After 1000ms from the fetching it emits and the value is flushed.
public randStr$ = new Observable<string>((sub) => {
sub.next(randomStr());
sub.complete();
})
.pipe(loadNCache({
persistence: 'localStorage',
persistenceKey: 'my-ls-key'
}));
You can supply a persistence manager or use one of the supported out of the box. See below for more.
LoadNCacheObservable
classLoadNCacheObservable
is a custom subclass of Observable that generates a LoadNCache instance and uses it to cache the last value emitted from the inner observable. This observable is very similar to a ReplaySubject(1)
with a some key differences:
Please don't interact directly with the LoadNCache instance from this class as it may result in undefined behaviour.
To the loadNCache function or to the LoadNCacheObservable constructor you can pass persistence options that work like those from the main project. See https://github.com/monesidn/load-n-cache for details.
Generated using TypeDoc