Skip to content

Instantly share code, notes, and snippets.

@bultas
Created February 7, 2019 11:09
Show Gist options
  • Save bultas/56939a2938b290a2af898d4f3435f004 to your computer and use it in GitHub Desktop.
Save bultas/56939a2938b290a2af898d4f3435f004 to your computer and use it in GitHub Desktop.
Store - vanilla JS TEA/Redux module
export const dispatch = e => window.dispatchEvent(e);
const handler = subscribe => state => update => action => {
const [newState, cmds] = update(state)(action);
if (state != newState) {
subscribe(newState);
}
state = newState;
if (cmds && cmds.length > 0) {
cmds.forEach(cmd => {
cmd(state); // TODO CMD returns plain actions and Dispatch action here
});
}
};
const domActor = eventHandler => events => {
events.forEach(([key, reducer]) => {
window.addEventListener(key, eventHandler(reducer), false);
});
};
export const store = (render, initState, events) =>
domActor(handler(render)(initState))(events);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment