Created
April 15, 2017 16:10
-
-
Save mattlockyer/221fce787737d0018370e91c15dda911 to your computer and use it in GitHub Desktop.
Testing Immutable.js as data store that will always point to the most updated reference of the map
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
//with store | |
const STORE = { | |
head: new Map(), | |
undo: () => { | |
const action = STORE.queue.pop(); | |
STORE.head = STORE.queue[STORE.queue.length - 1]; | |
return action; | |
}, | |
queue: [] | |
}; | |
STORE.queue.push(STORE.head); | |
['get'].forEach(k => STORE[k] = (...args) => STORE.head[k](...args)); | |
['set', 'merge'].forEach(k => STORE[k] = (...args) => { | |
STORE.head = STORE.head[k](...args); | |
STORE.queue.push(STORE.head); | |
}); | |
STORE.merge({ | |
0: 'zero' | |
}); | |
STORE.set(1, 'one'); | |
console.log(STORE.get('0')); //key was changed to string via Map.merge (not my doing) | |
console.log(STORE.get(1)); | |
console.log(STORE.queue[0].get(1)); //should NOT work (empty map, first head reference) | |
const oldStore = STORE.undo(); | |
console.log(STORE.get(1)); //should NOT work | |
STORE.undo(); | |
console.log(STORE.get('0')); //should NOT work | |
//oldStore before undo (SHOULD WORK) | |
console.log(oldStore.get('0')); | |
console.log(oldStore.get(1)); | |
console.log('USING MAP AS IS'); | |
//without store | |
const test = new Map(); | |
test.merge({ | |
0: 'zero' | |
}); | |
test.set(1, 'one'); | |
console.log(test.get('0')); //should NOT work | |
console.log(test.get(1)); //should NOT work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment