Skip to content

Instantly share code, notes, and snippets.

@bradfordlemley
Last active May 29, 2019 20:55
Show Gist options
  • Save bradfordlemley/8eb479f95445ec74b90c6dbd941057e7 to your computer and use it in GitHub Desktop.
Save bradfordlemley/8eb479f95445ec74b90c6dbd941057e7 to your computer and use it in GitHub Desktop.
Todo library with observable state
export default function createTodoLib() {
let state = {
todos: [],
isFetching: false,
};
let state$ = new Observable();
function setState(newState) {
state = newState;
state$.next(state);
}
function updateState(update) {
setState({
...state,
...update,
});
}
return {
get state() {return state},
state$,
addTodo(title) {
updateState({ todos: state.todos.concat({ title }) });
},
async fetchTodos() {
updateState({ isFetching: true });
const newTodos = await fetchFromCloud();
updateState({
isFetching: false,
todos: state.todos.concat(newTodos),
});
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment