Skip to content

Instantly share code, notes, and snippets.

@alshdavid
Last active May 15, 2019 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alshdavid/69062797a080af2ee73e6bc8aa118797 to your computer and use it in GitHub Desktop.
Save alshdavid/69062797a080af2ee73e6bc8aa118797 to your computer and use it in GitHub Desktop.
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