/
main.ts
202 lines (184 loc) · 5.24 KB
/
main.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import config from "../config";
import * as Core from "./core";
import * as DOM from "./dom";
import * as S from "@effectful/serialization";
import "./persist";
export const journal = Core.journal;
export const reset = Core.reset;
const flush: () => void =
config.timeTravel && config.mutationObserver ? DOM.flush : function() {};
declare module "../state" {
interface Frame extends Core.MaybeTarget<Frame> {}
}
const undoImpl: () => Core.Record | null = config.timeTravelForward
? function undo(): Core.Record | null {
flush();
const { now } = journal;
if (!now) return null;
const ops = now.operations;
now.operations = void 0;
for (let i = ops; i != null; i = i.prev) i.call();
flush();
let { future, past } = journal;
now.prev = future;
journal.future = now;
journal.now = past;
if (past) {
journal.past = past.prev;
past.prev = null;
} else {
journal.past = null;
}
return past;
}
: function undo() {
flush();
const { now, past } = journal;
if (!now || !past) return null;
journal.now = null;
for (let i = past.operations; i != null; i = i.prev) i.call();
journal.now = past;
journal.past = past.prev;
flush();
return past;
};
/**
* This rollbacks all changed objects and runs functions registered by `record`,
* (assumes an empty last checkpoint).
*/
export const undo: () => Core.Record | null = config.timeTravel
? undoImpl
: function undo() {
return null;
};
export const redo: () => Core.Record | null = config.timeTravel
? function redo() {
flush();
const { future } = journal;
if (!future) return null;
const ops = future.operations;
future.operations = void 0;
const { now } = journal;
if (now) now.prev = journal.past;
journal.past = now;
journal.now = future;
journal.future = future.prev;
for (let i = ops; i != null; i = i.prev) i.call();
flush();
return future;
}
: function redo() {
return null;
};
/**
* Returns an object which can be passed
* to `undo` to restored the current state
*/
export const checkpoint: () => Core.Record | null = config.timeTravel
? config.mutationObserver
? function checkpoint() {
flush();
return Core.checkpoint();
}
: Core.checkpoint
: function() {
return null;
};
/** Registers an operation to be called on `undo` in reverse direction */
export const record = config.timeTravel
? Core.record
: function record(_: Core.Operation) {};
/**
* when constructing new objects replacing its prototype to unwrapped one,
* otherwise it will catch all setters of not yet wrapped target
*/
export const unwrapPrototype = config.timeTravel
? function(target: Core.Target) {
const proto = Object.getPrototypeOf(target);
if (proto.hasOwnProperty(Core.traceDataSymbol))
Object.setPrototypeOf(target, proto[Core.traceDataSymbol].target);
}
: function(_: Core.Target) {};
export {
wrap,
Operation,
Record,
traceDataSymbol,
traceMetaSymbol
} from "./core";
export { Core, DOM };
const deleteTag = {};
interface IPropSnapshot extends Core.Operation {
target: any;
index: string | symbol | number;
oldValue: any;
newValue: any;
back: boolean;
}
const PropSnapshot: any = config.timeTravelForward
? function(
this: IPropSnapshot,
target: any,
index: string | symbol | number,
oldValue: any,
newValue: any
) {
this.target = target;
this.index = index;
this.newValue = newValue;
this.oldValue = oldValue;
this.back = true;
}
: function(
this: IPropSnapshot,
target: any,
index: string | symbol | number,
oldValue: any,
newValue: any
) {
this.target = target;
this.index = index;
this.newValue = newValue;
this.oldValue = oldValue;
this.back = true;
};
PropSnapshot.prototype.call = config.timeTravelForward
? function(this: IPropSnapshot) {
if (this.back) {
if (this.oldValue === deleteTag) delete this.target[this.index];
else this.target[this.index] = this.oldValue;
} else this.target[this.index] = this.newValue;
this.back = !this.back;
Core.record(this);
}
: function(this: IPropSnapshot) {
if (this.oldValue === deleteTag) delete this.target[this.index];
else this.target[this.index] = this.oldValue;
};
S.regConstructor(PropSnapshot);
/**
* defines setter a getter in `value` to support time travel
* TODO: delete handler
*/
export function propHack(value: any, propName: string) {
const sym = Symbol(`${propName}`);
Object.defineProperty(value, propName, {
set(value) {
this[sym] = value;
Core.record(new PropSnapshot(this, propName, deleteTag, value));
Object.defineProperty(this, propName, {
configurable: true,
set(value) {
Core.record(new PropSnapshot(this, propName, this[sym], value));
this[sym] = value;
},
get() {
return this[sym];
}
});
},
get() {}
});
}
if (config.persistTimeTravel) S.regConstructor(Core.TraceData);
else S.regConstructor(Core.TraceData, S.NotSerializableDescriptor);