This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
time-travel.js
67 lines (51 loc) · 1.63 KB
/
time-travel.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
'use strict';
module.exports = TimeTravel;
function TimeTravel(state) {
var history = [state()];
// Tracks the current position in history.
var cursor = 0;
var isRedoOrUndo = false;
state(function recordState(newState) {
// This function gets called whenever there is a state change.
// State changes happen due to events being handled, or due to
// undo/redo.
// If we are replaying items in the history,
// we don't want to re-add them to the end of the history.
// Just quit.
if (isRedoOrUndo) {
return;
}
// If we've made it this far, `newState` is due to a new action,
// not due to undo/redo.
// If we've called `undo` a bunch of times,
// the cursor won't be at the end.
// Any states past the cursor should be cut off.
history.splice(cursor + 1);
// Add the new item to the history
history.push(newState);
cursor = history.length - 1;
});
return { undo: undo, redo: redo };
function undo() {
if (cursor < 1) {
// Don't move before the beginning of time
return undefined;
}
cursor--;
isRedoOrUndo = true;
state.set(history[cursor]);
isRedoOrUndo = false;
return history[cursor];
}
function redo() {
if (cursor + 1 >= history.length) {
// Don't move past the end of time
return undefined;
}
cursor++;
isRedoOrUndo = true;
state.set(history[cursor]);
isRedoOrUndo = false;
return history[cursor];
}
}