Skip to content

Instantly share code, notes, and snippets.

@Papipo
Created November 11, 2015 09:40
Show Gist options
  • Save Papipo/6b6b94e8c2db8b085838 to your computer and use it in GitHub Desktop.
Save Papipo/6b6b94e8c2db8b085838 to your computer and use it in GitHub Desktop.
function store(opts) {
var list = [];
var local = opts.local;
var remote = opts.remote;
function replace(items) {
if (items.length) {
list = items;
}
return items;
}
function fetchNew() {
return local.last()
.then(function(last) {
return last ? remote.after(last) : remote.latest();
});
}
function latest() {
local.latest()
.then(replace)
.then(fetchNew)
.then(replace)
.catch(function(e) {
console.error(e.stack);
});
return function() {
return list;
};
}
return {
latest: latest
};
}
module.exports = store;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment