This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathwatch_record.js
333 lines (285 loc) · 9.03 KB
/
watch_record.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import {
_LinkedList,
_LinkedListItem,
_WatchList,
_ArgHandlerList,
_EvalWatchList
} from './linked_list';
import {
Watch
} from './watch';
var haveMap = (typeof WeakMap === "function" && typeof Map === "function");
class _Handler {
constructor(watchGrp, expression) {
_LinkedList._initialize(this);
_LinkedListItem._initialize(this);
_WatchList._initialize(this);
this.watchGrp = watchGrp;
this.expression = expression;
this.watchRecord = this.forwardingHandler = null;
}
addReactionFn(reactionFn) {
// TODO: Traceur assertions
// assert(this._next !== this);
// assert(typeof reactionFn === "function");
return this.watchGrp._rootGroup._addDirtyWatch(_WatchList._add(this,
new Watch(this.watchGrp, this.watchRecord, reactionFn)));
}
addForwardHandler(forwardToHandler) {
// TODO: Traceur assertions
// assert(forwardToHandler.forwardingHandler === null);
_LinkedList._add(this, forwardToHandler);
forwardToHandler.forwardingHandler = this;
}
// I'm not sure this really makes sense in JS, since we don't actually know when finallization
// occurs. I think this is called manually in a few areas though, so I'll leave it for now.
release() {
if (_WatchList._isEmpty(this) && _LinkedList._isEmpty(this)) {
this._releaseWatch();
// Remove outselves from cache, or else new registrations will go to us, but we are dead
// Potential GC pressure...
if (this.watchGrp) {
delete this.watchGrp._cache[this.expression];
}
if (this.forwardingHandler !== null) {
// TODO(misko): why do we need this check? --- Because _LinkedList._remove() manipulates
// properties of the list, and it will be referencing properties of null and throw a
// noSuchMethod error if forwardingHandler === null
_LinkedList._remove(this.forwardingHandler, this);
this.forwardingHandler.release();
this.forwardingHandler = null;
}
// We can remove ourselves
this._next = this._previous = this;
}
}
_releaseWatch() {
this.watchRecord.remove();
this.watchGrp.fieldCost--;
}
acceptValue(object) {
return null;
}
onChange(record) {
// TODO: Traceur assertions
// assert(this._next !== this); // Verify we are not detached
// If we have reaction functions, then queue them up for asynchronous processing.
var watch = this._watchHead;
var root = this.watchGrp._rootGroup;
while (watch !== null) {
root._addDirtyWatch(watch);
watch = watch._nextWatch;
}
// If we have a delegateHandler, then forward the new value to it.
var delegateHandler = this._head;
while (delegateHandler !== null) {
delegateHandler.acceptValue(record.currentValue);
delegateHandler = delegateHandler._next;
}
}
}
export class _ConstantHandler extends _Handler {
constructor(watchGroup, expression, constantValue) {
super(watchGroup, expression);
this.watchRecord = new _EvalWatchRecord.constant(this, constantValue);
}
release() {
return null;
}
}
export class _FieldHandler extends _Handler {
constructor(watchGroup, expression) {
super(watchGroup, expression);
}
// This function forwards the watched object to the next [_Handler] synchronously
acceptValue(object) {
this.watchRecord.object = object;
if (this.watchRecord.check()) this.onChange(this.watchRecord);
}
}
export class _CollectionHandler extends _Handler {
constructor(watchGroup, expression) {
super(watchGroup, expression);
}
// This function forwards the watched object to the next [_Handler] synchronously
acceptValue(object) {
this.watchRecord.object = object;
if (this.watchRecord.check()) this.onChange(this.watchRecord);
}
_releaseWatch() {
this.watchRecord.remove();
this.watchGrp._collectionCost--;
}
}
export class _ArgHandler extends _Handler {
constructor(watchGroup, watchRecord, index) {
// TODO(caitp): assert that watchRecord is an _EvalWatchRecord?
super(watchGroup, `arg[${index}]`);
this._previousArgHandler = this._nextArgHandler = null;
this.watchRecord = watchRecord;
this.index = index;
}
_releaseWatch() {
return null;
}
acceptValue(object) {
this.watchRecord.dirtyArgs = true;
this.watchRecord.args[this.index] = object;
}
}
export class _InvokeHandler extends _Handler {
constructor(watchGroup, expression) {
super(watchGroup, expression);
_ArgHandlerList._initialize(this);
}
acceptValue(object) {
this.watchRecord.object = object;
}
release() {
super.release();
var current = this._argHandlerHead;
while (current !== null) {
current.release();
current = current._nextArgHandler;
}
// TODO(caitp): should _argHandlerHead/Tail be nulled here? Or would that cause too much GC
// pressure.
}
_releaseWatch() {
this.watchRecord.remove();
// TODO(caitp): why return undefined here?
}
}
var _MODE_DELETED_ = -1;
var _MODE_MARKER_ = 0;
var _MODE_FUNCTION_ = 1;
var _MODE_FUNCTION_APPLY_ = 2;
var _MODE_NULL_ = 3;
var _MODE_FIELD_CLOSURE_ = 4;
var _MODE_MAP_CLOSURE_ = 5;
var _MODE_METHOD_ = 6;
// TODO(caitp): This does not seem like the correct module for _EvalWatchRecord to live
export class _EvalWatchRecord {
constructor(watchGrp, handler, fn, name, arity, marker) {
this.watchGrp = watchGrp;
this.handler = handler;
this.name = name;
this.fn = fn;
// TODO(caitp): The ES6 draft is not super clear about what can be done with Symbols.
// This may be entirely unnecessary.
// this.symbol = name === null ? null : new Symbol(name);
this._prevEvalWatch = this._nextEvalWatch = null;
if (marker === true) return;
this.args = new Array(arity);
// TODO(caitp): Does the FunctionApply type really need to be implemented?
if (typeof fn === 'function') {
this.mode = _MODE_FUNCTION_;
} else {
this.mode = _MODE_NULL_;
}
}
static marker() {
var record = new _EvalWatchRecord(null, null, null, null, null, true);
record.args = null;
record.mode = _MODE_MARKER_;
return record;
}
static constant(handler, constantValue) {
var record = _EvalWatchRecord.marker();
record.currentValue = constantValue;
record.handler = handler;
return record;
}
get object() {
return this._object;
}
set object(value) {
// TODO(caitp): Traceur assertions
// assert(this.mode !== _MODE_DeLETED_);
// assert(this.mode !== _MODE_MARKER_);
// assert(this.mode !== _MODE_FUNCTION_);
// assert(this.mode !== _MODE_FUNCTION_APPLY_);
// assert(this.symbol !== null);
this._object = value;
if (value === null) {
this.mode = _MODE_NULL_;
} else {
if (haveMap && (value instanceof Map || value instanceof WeakMap)) {
this.mode = _MODE_MAP_CLOSURE_;
} else if (this.name) {
this.mode = _MODE_METHOD_;
}
}
/**
* TODO(caitp): The usefulness of these blocks is yet to be discovered, but I'm sure it's out
* there somewhere.
*
* else {
* if (value instanceof Map) {
* this.mode = _MODE_MAP_CLOSURE_;
* } else {
* this._instanceMirror = this.reflect(value);
* this.mode = this._hasMethod(this._instanceMirror, this.symbol)
* ? _MODE_METHOD_
* : _MODE_FIELD_CLOSURE_;
* }
* }
*/
}
check() {
var value;
switch (this.mode) {
case _MODE_MARKER_:
case _MODE_NULL_:
return false;
case _MODE_FUNCTION_:
if (!this.dirtyArgs) return false;
value = this.fn.apply(null, this.args);
this.dirtyArgs = false;
break;
case _MODE_METHOD_:
value = methodInvoke(this._object, this.name, this.args);
break;
// TODO: the rest of these items don't really make sense in JS, as far as I can tell.
// Investigate and ask about this.
default:
throw "UNREACHABLE";
}
var current = this.currentValue;
if (current !== value) {
if (current !== current && value !== value) {
// Ignore, it appears to be a NaN -> NaN change.
current = value;
} else {
this.previousValue = current;
this.currentValue = value;
this.handler.onChange(this);
return true;
}
}
return false;
}
remove() {
// TODO(caitp): Traceur assertions
// assert(this.mode !== _MODE_DELETED_);
this.mode = _MODE_DELETED_;
this.watchGrp.evalCost--;
_EvalWatchList._remove(this.watchGrp, this);
}
toString() {
if (this.mode === _MODE_MARKER_) return `MARKER[${this.currentValue}]`;
return `${this.watchGrp.id}:${this.handler.expression}`;
}
// TODO(caitp): worry about this later...
// static _hasMethod(mirror, symbol) {
// return mirror.type.instanceMembers[symbol] is MethodMirror;
// }
}
var __no_args__ = [];
function methodInvoke(object, method, args) {
if (object || (typeof object !== 'undefined' && object !== null)) {
if (typeof object[method] === "function") {
return object[method].apply(object, args || __no_args__);
}
}
}