Skip to content

Instantly share code, notes, and snippets.

@zerobias
Created September 6, 2018 11:23
Show Gist options
  • Save zerobias/dbc938fb49502a47494bff5aab353586 to your computer and use it in GitHub Desktop.
Save zerobias/dbc938fb49502a47494bff5aab353586 to your computer and use it in GitHub Desktop.
export const defaultCache = store => {
return {
invalidate: hash =>
new Promise(resolve => {
delete store[hash];
resolve(hash);
}),
invalidateAll: () =>
new Promise(resolve => {
store = {};
resolve();
}),
read: hash =>
new Promise(resolve => {
resolve(store[hash] || null);
}),
update: callback =>
new Promise(resolve => {
if (typeof callback === 'function') {
Object.keys(store).forEach(key => {
callback(store, key, store[key]);
});
}
resolve();
}),
write: (hash, data) =>
new Promise(resolve => {
store[hash] = data;
resolve(hash);
}),
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment