-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.js
159 lines (125 loc) · 3.27 KB
/
history.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
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
import fn from "@/fn";
const ACTION = { tag: "action" }
const VALUE = { tag: "value" }
class ActionHistory {
/*
Capture calls of an action, link them in a tree
For call-level options granularity:
action.trackWith({...options})(...args)
*/
constructor(target, func) {
this.root = {
prevs: [],
nexts: [],
}
this.func = func
this.target = target
this.pointer = this.root
}
record(args) {
var node = {
nexts: [],
prevs: [this.pointer],
args: args
}
this.pointer.nexts.push(node)
this.pointer = node
}
recordInPlace(args) {
this.pointer.args = args
}
peekNext() {
/*
return the next node
picks the latest branch of nexts
*/
return this.pointer.nexts.at(-1)
}
peekPrev() {
/*
return the next node
picks the latest branch of nexts
*/
const res = this.pointer.prevs.at(-1)
if (res === this.root) return undefined
return res
}
tryNext() {
/*
moves pointer one step forward, if possible
*/
const potentialNext = this.peekNext()
this.pointer = potentialNext ?? this.pointer
return this
}
tryPrev() {
/*
moves pointer one step back, if possible
*/
const potentialPrev = this.peekPrev()
this.pointer = potentialPrev ?? this.pointer
return this
}
call() {
if (this.pointer.args === undefined) {
throw new Error('calling ActionHistory.call() on root')
}
this.func.call(this.target, ...this.pointer.args)
}
}
function trackAction(target, action) {
/*
props.keys() -> { propKey: History(target, target[propKey]), ... }
+ auto record on call
*/
const func = action
var history = new ActionHistory(target, func)
function wrapper(...args) {
history.record(args)
return func.call(target, ...args)
}
/*
(options, default):
isTracked: true -- record the arguments
isOverwrite: false -- record call by overwriting the one at current pointer
isDummyCall: false -- don't call the action
isArgsFromPrev: false -- use previous arguments instead, if possible
*/
wrapper.trackWith = (options) => {
const tracking = fn.condShort(
[options.isTracked === false, (args) => { }],
[options.isOverwrite, (args) => history.recordInPlace(args)],
[true, (args) => history.record(args)]
)
const calling = fn.condShort(
[options.isDummyCall, (args) => { }],
[true, (args) => func.apply(target, args)]
)
const argumenting = fn.condShort(
[options.isArgsFromPrev, (args) => history.peekPrev()?.args ?? args],
[true, (args) => args]
)
return (...args) => {
const currentArgs = argumenting(args)
tracking(currentArgs)
return calling(currentArgs)
}
}
wrapper.history = history
return wrapper
}
function track(target, props) {
/*
props.keys() -> { propKey: History(target, target[propKey]), ... }
+ auto record on call
*/
for (const key in props) {
if (props[key] === ACTION) {
target[key] = trackAction(target, target[key])
} else {
throw new Error('Not Implemented')
}
}
return history
}
export default { ACTION, VALUE, trackAction, track }