Created
September 22, 2016 07:06
-
-
Save FeliciousX/48a17fbd793723f11f649de1de4e76b5 to your computer and use it in GitHub Desktop.
Higher Order Components
This file contains 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
function Undoable( Component ) { | |
return function( sources ) { | |
const component = Component( sources ); | |
const add$ = component.state$ // component exposes the state$. Each new state$ is considered as an add action | |
.map( state => function( history ) { | |
const newHistory = history.clone(); // shallow clone. Should use immutable.js or something | |
newHistory.states.push( state ); | |
newHistory.index = newHistory.states.length - 1; | |
return newHistory; | |
}); | |
const undo$ = component.undo$ // component exposes the undo action$ | |
.map( () => function( history ) { | |
const newHistory = history.clone(); | |
newHistory.index -= 1; | |
return newHistory; | |
}); | |
const redo$ = component.redo$ // component exposes the redo action$ | |
.map( () => function( history ) { | |
const newHistory = history.clone(); | |
if ( history.states.length - 1 >= history.index ) { | |
return history; | |
} else { | |
newHistory.index += 1; | |
return newHistory; | |
} | |
}); | |
const initHistory = { | |
states: [], | |
index: -1 | |
}; | |
const history$ = xs.merge( add$, undo$, redo$ ) | |
.fold( (history, reducer) => reducer( history ), initHistory ) | |
.map( history => history.states[ history.index ] ); | |
// now how do i use this history$ to update the component? | |
// I thought of passing a proxyHistory$ above but then how would the Component handle that | |
// and not cause an infinite loop of state$ in component changing. Propogating back to the history$ etc. | |
return { | |
...component, | |
}; | |
} | |
} |
ADRIAGUSJO
commented
Sep 22, 2016
toy happy with me / git,git,git /hub-hub-hub
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment