forked from goatslacker/alt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeTravel.js
78 lines (60 loc) · 1.71 KB
/
TimeTravel.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { assign } from './functions'
import makeFinalStore from './makeFinalStore'
function timetravel(alt, options = {}) {
const history = assign({
max: 300
}, options)
const payloadStore = makeFinalStore(alt)
const payloads = []
let current = 0
function captureMoment(snapshot) {
if (payloads.length > history.max - 1) {
payloads.shift()
}
// trash history because an undo has taken place
if (current < payloads.length) {
payloads.splice(current + 1, payloads.length)
}
current += 1
payloads.push(snapshot)
}
return Store => {
class TimeTravelStore extends Store {
static displayName = Store.displayName || Store.name
constructor(...args) {
super(...args)
this.on('init', _ => {
// capture the initial snapshot
captureMoment(alt.serialize({
[this._storeName]: this
}))
// capture subsequent shots
payloadStore.listen(_ => captureMoment(
alt.takeSnapshot(this._storeName)
))
})
this.exportPublicMethods({
events() {
return payloads.slice()
},
undo(n = 1) {
const max = payloads.length - 1
const index = Math.min(n, max)
const payload = payloads[max - index]
current = max - index
alt.bootstrap(payload)
},
redo(n = 1) {
const max = payloads.length - 1
const index = Math.min(current + n, max)
const payload = payloads[index]
current = index
alt.bootstrap(payload)
}
})
}
}
return TimeTravelStore
}
}
export default timetravel