A helper library to deal with servers that occasionally return stale data as a response. This can be problematic if you use the server state to update a UI and it switches between old and new state.
The module is released in to the public npm registry and can be installed using:
npm install --save stale-state
import StaleState from 'stale-state';
const stale = new StaleState({ /* options here */ });
The following options are accepted:
previously
Allows you to set an initial state.name
Name of the instance. Used in our debug output.requests
The amount ofrequests
need to be made to validate if declined data is really out of data.interval
Interval for thefetch
function.
Add a function that does the request for data that might need a verification. The supplied function receives a single argument which is an error first completion callback.
stale.request((next) => {
your.api.call(data, function (err, data) {
next(err, data);
});
});
This is the most important API in this module, the part where you verify if the received data is stale or not and decline or accept the new data. The supplied method receives 3 arguments:
previously
The previous data that we received.currently
The new data that we received from yourrequest
method.state
An object with anaccept
,same
anddecline
function to accept or decline the newly requested data.
stale.check((previous, current, state) => {
if (previous.value == current.value) return state.same();
if (previous.value < currrent.value) return state.accept();
state.decline();
});
Start fetching data. This calls the request
method and starts processing the
data using the check
method. If an interval is set in the options it will also
start a new interval so your data will be fetched continuously if you need to
poll a specific endpoint.
stale.fetch();
We've found a new accepted state that can be used in your application. This function receives the newly checked data.
stale.commit((data) => {
application.setState(data);
});
Receives all errors that happen during checking. For example your request
method is now calling the callback with errors etc.
stale.error((err) => {
console.error(err);
});
MIT