Skip to content

Instantly share code, notes, and snippets.

@bradfordlemley
Last active May 29, 2019 20:54
Show Gist options
  • Save bradfordlemley/b2112c2c3c8bd0e76c052c409dfd6984 to your computer and use it in GitHub Desktop.
Save bradfordlemley/b2112c2c3c8bd0e76c052c409dfd6984 to your computer and use it in GitHub Desktop.
Stated Lib Base with deriveState
export default function createBase(initialState, deriveState){
let state = deriveState ? deriveState(initialState) : initialState;
let state$ = new Observable();
function setState(newState) {
state = deriveState ? deriveState(newState) : newState;
state$.next(state);
}
function updateState(update) {
setState({
...state,
...update,
});
}
return {
get state() {return state},
state$,
setState,
updateState,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment