Last active
February 6, 2019 12:38
-
-
Save gtkatakura-bysoft/53b8080f3eef7820bc6d3dc0d47df88c 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
const R = require('ramda') | |
const undoable = (shape) => { | |
const operation = actionSelection => R.evolve( | |
R.map(R.cond([ | |
[R.is(Function), R.identity], | |
[R.is(Array), actionSelection], | |
[R.is(Object), R.pipe(undoable, actionSelection)], | |
[R.T, R.identity], | |
]), shape) | |
) | |
return [ | |
operation(R.head), // perform | |
operation(R.last), // undo | |
] | |
} | |
const oldItem = {a: 1} | |
const itemModified = {b: 2} | |
const itemId = 1 | |
const [add, remove] = undoable({ | |
collections: { | |
payload: [R.prepend(oldItem), R.reject(R.eqProps('id', itemModified))], | |
count: [R.inc, R.dec], | |
dependencies: R.without([itemId]), | |
}, | |
}) | |
const [add1, remove1] = undoable({ | |
collections: { | |
payload: [R.prepend(itemModified), R.reject(R.eqProps('id', oldItem))], | |
count: [R.inc, R.dec], | |
dependencies: R.without([itemId]), | |
}, | |
}) | |
console.log( | |
add({ | |
collections: { | |
payload: [itemModified], | |
count: 0, | |
dependencies: [2, 3], | |
}, | |
}) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment