Skip to content

Instantly share code, notes, and snippets.

@FeliciousX
Created September 22, 2016 07:06
Show Gist options
  • Save FeliciousX/48a17fbd793723f11f649de1de4e76b5 to your computer and use it in GitHub Desktop.
Save FeliciousX/48a17fbd793723f11f649de1de4e76b5 to your computer and use it in GitHub Desktop.
Higher Order Components
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
Copy link

1

@ADRIAGUSJO
Copy link

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