Last active
May 15, 2019 12:38
-
-
Save alshdavid/69062797a080af2ee73e6bc8aa118797 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
const createStore = () => { | |
let items = []; | |
const watchers = []; | |
const add = item => { | |
items.push(item); | |
emit(); | |
}; | |
const complete = id => { | |
items = items.map(item => { | |
if (item.id === id) { | |
item.complete = true; | |
} | |
return item; | |
}); | |
emit(); | |
}; | |
const emit = () => watchers.forEach(cb => cb(items)); | |
const onChange = cb => { | |
const i = watchers.length - 1; | |
watchers.push(cb); | |
cb(items) | |
return () => watchers.splice(1, i); | |
}; | |
return { | |
items, | |
add, | |
complete, | |
onChange | |
}; | |
}; | |
const create = body => ({ | |
id: Math.floor(Math.random() * 100), | |
body, | |
complete: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment