Created
February 7, 2019 11:09
-
-
Save bultas/56939a2938b290a2af898d4f3435f004 to your computer and use it in GitHub Desktop.
Store - vanilla JS TEA/Redux module
This file contains hidden or 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
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