Created
March 6, 2023 02:52
-
-
Save dinocarl/de5438eead04406ff7b2d381919069c4 to your computer and use it in GitHub Desktop.
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
// permitted actions for state changes | |
const eventActions = { | |
write: (path, val = null) => assocPath(path, val), | |
erase: (path) => dissocPath(path), | |
}; | |
const noop = () => () => {}; | |
// run a legitmate action on a state object | |
const eventToState = ({ type, path, val }, stateObj, actions) => propOr( | |
noop, | |
type, | |
actions | |
)(path, val)(stateObj); | |
// passing in a set of legitmate actions, update a state object | |
// with event data. uses a reducer-compatible fn signature | |
const eventsToStateUsing = (actions) => (acc, detail) => has(detail.type, actions) | |
// action is legit; update a path in the object | |
? eventToState(detail, acc, actions) | |
// action is not legit; return original object unchanged | |
: acc; | |
const someState = {about: {'456': null, '789': 'articleList'}, currTab: ''}; | |
const evt1 = { type: 'write', path: ['currTab'], val: 'misc' }; | |
const evt2 = { type: 'write', path: ['about', '1112'] }; | |
const evt3 = { type: 'write', path: ['contact', '123'], val: 'email' }; | |
const evt4 = { type: 'erase', path: ['about', '456'] }; | |
[ | |
reduce( | |
eventsToStateUsing(eventActions), | |
someState, | |
[evt1, evt2, evt3, evt4] | |
), | |
'----', | |
eventsToStateUsing(eventActions)( | |
eventsToStateUsing(eventActions)( | |
eventsToStateUsing(eventActions)( | |
eventsToStateUsing(eventActions)( | |
someState, | |
evt1 | |
), | |
evt2 | |
), | |
evt3 | |
), | |
evt4 | |
), | |
'----', | |
eventToState(evt1, someState, eventActions), | |
eventToState(evt2, someState, eventActions), | |
eventToState(evt3, someState, eventActions), | |
eventToState(evt4, someState, eventActions), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment