-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtimeTravelStore.js
44 lines (34 loc) · 972 Bytes
/
timeTravelStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// From Gunar Gessner
// https://github.com/sam-js/timetravel
//
let snapshots = {}
snapshots.data = []
snapshots.model = []
const listeners = []
export const subscribe = listener => {
listeners.push(listener)
listener(snapshots)
return function unsubscribe() {
const index = listeners.indexOf(listener)
listeners.splice(index, 1)
}
}
const publish = _ => listeners.forEach(listener => listener(snapshots))
export const getListOfSnapshots = _ => snapshots
export const getSnapshot = index => {
snapshots.data = snapshots.data.slice(0, (index+1))
snapshots.model = snapshots.model.slice(0, (index+1))
publish()
return { ...snapshots.model[index] }
}
export const saveSnapshot = (dataset, model) => {
if (dataset) {
snapshots.data.push({ ...dataset })
}
if (model) {
snapshots.model.push({ ...model })
}
console.log('Saved snapshots:', snapshots)
// Publish to subscribers
publish()
}