obcache is a utility for client side data caching, and ensuring all users of the data see updates. For use with react, see react-obcache.
It uses listener reference counting to avoid no longer needed data piling up.
Install
npm install obcache --save
Usage
Start by creating a cache. You can then .register
handlers which are
responsible for providing promises of data.
; var cache = ;; cache;
This example shows the full api of obcache. Typically you won't deal with it directly, but use a wrapper for your ui library/framework.
; var profile = cache profilekey === 'userProfile├4920840918098'; // makes the api request if the data isn't already present// and registers listeners for new values// they're called immediately if there is already a value or errorvar unsubscribe = profile; ; // manually set the value// this is typically used when e.g. you have a user listing call that wants// to create several userProfile observables in the cacheprofile; // set it to the new value; revert if the promise is rejected// this can be used for optimistic updatesprofile; // similar to the above but only updates the keys in the object mentioned// so with state {a: 1, b: 2}; .update({a: 3}) results in {a: 3, b: 2}// with set, it'd be replaced with {a: 3}// updates are immutable transactionsprofileprofile
ES6 deps
obcache works best if setImmediate is available. Core.js (or babel polyfill) provides it.
Object.assign is required.
Development
Run npm run dev
in one terminal, and npm run dev-test
in another.
To do a single build, npm run build
and npm run test
.