Created
November 11, 2015 09:40
-
-
Save Papipo/6b6b94e8c2db8b085838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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