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_group.js
630 lines (493 loc) · 17.5 KB
/
watch_group.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import {
AST
} from './ast';
import {
_LinkedList,
_LinkedListItem,
_WatchList,
_WatchGroupList,
_ArgHandlerList,
_EvalWatchList
} from './linked_list';
import {
_Handler,
_ConstantHandler,
_CollectionHandler,
_InvokeHandler,
_FieldHandler,
_ArgHandler,
_EvalWatchRecord
} from './watch_record';
import {
ChangeRecord,
ChangeRecordIterator
} from './dirty_checking';
function putIfAbsent(obj, key, ctor) {
if (key in obj) return obj[key];
return (obj[key] = ctor());
}
export class WatchGroup {
constructor(id, getterCache, context, cache, rootGroup) {
this.id = id;
// Initialize _WatchGroupList
this._watchGroupHead = this._watchGroupTail = null;
this._nextWatchGroup = this._prevWatchGroup = null;
this._dirtyWatchHead = this._dirtyWatchTail = null;
this._getterCache = getterCache;
this.context = context;
this._cache = cache;
this._rootGroup = rootGroup;
this._nextChildId = 0;
this._marker = _EvalWatchRecord.marker();
this._marker.watchGrp = this;
this._evalWatchHead = this._evalWatchTail = this._marker;
this._dirtyMarker = ChangeRecord.marker();
this._recordHead = this._recordTail = this._dirtyMarker;
// Stats...
this.fieldCost = 0; // Stats: Number of field watchers which are in use
this.collectionCost = 0; // Stats: Number of collection watchers which are in use
this.evalCost = 0; // Stats: Number of invocation watchers (closures/methods) which are in use
}
// Stats: Number of field watchers which are in use including child groups
get totalFieldCost() {
var cost = this.fieldCost;
var group = this._watchGroupHead;
while (group !== null) {
cost += group.totalFieldCost;
group = group._nextWatchGroup;
}
return cost;
}
// Stats: Number of collection watchers which are in use including child groups
get totalCollectionCost() {
var cost = this.collectionCost;
var group = this._watchGroupHead;
while (group !== null) {
cost += group.totalCollectionCost;
group = group._nextWatchGroup;
}
return cost;
}
// Stats: Number of invocation watchers (closures/methods) which are in use, including child
// groups
get totalEvalCost() {
var cost = this.evalCost;
var group = this._watchGroupHead;
while (group !== null) {
cost += group.totalEvalCost;
group = group._nextWatchGroup;
}
return cost;
}
get recordCount() {
var count = 0,
cursor = this._recordHead,
end = this._childInclRecordTail;
while (cursor !== null) {
if (!cursor.isMarker) {
++count;
}
if (cursor === end) break;
cursor = cursor.nextRecord;
}
return count;
}
get isAttached() {
var group = this;
var root = this._rootGroup;
while (group !== null) {
if (group === root) {
return true;
}
group = group._parentWatchGroup;
}
return false;
}
// TODO:
// I am not at all sure about the `expression` abstraction. In Angular.dart, this is a parse tree
// or AST, and is tied rather closely to the core Parser implementation.
//
// In my mind, this library should be useful independent from the core Angular parser, but I am
// not sure how to accomplish this elegantly. It seems that regardless of how this is structured,
// there is going to be some overhead in converting to a parse tree understood by the
// dirty-checker.
//
// If anyone has any clever suggestions regarding this, please file an issue so that we can
// bike-shed this.
watchExpression(expression, reactionFn) {
var watchRecord;
if (expression.expression in this._cache) {
watchRecord = this._cache[expression.expression];
} else {
this._cache[expression.expression] = watchRecord = expression.setupWatch(this);
}
return watchRecord.handler.addReactionFn(reactionFn);
}
get _childWatchGroupTail() {
var tail = this, nextTail;
while ((nextTail = tail._watchGroupTail) !== null) {
tail = nextTail;
}
return tail;
}
get _childInclRecordTail() {
return this._childWatchGroupTail._recordTail;
}
// Create a new child [WatchGroup]
//
// - [context] if present the child [WatchGroup] expressions will evaluate against the new
// [context]. If not present than child expressions will evaluate on same context allowing
// the reuse of the expression cache.
newGroup(context) {
if (arguments.length === 0 || context === null) {
context = this.context;
}
var root = this._rootGroup === null ? this : this._rootGroup;
var id = `${this.id}.${this._nextChildId++}`;
var cache = context === null ? this._cache : {};
var childGroup = new WatchGroup(id, this._getterCache, context, cache, root);
return childGroup;
}
addGroup(childGroup){
childGroup.id = `${this.id}.${this._nextChildId++}`;
childGroup._parentWatchGroup = this;
var prevEval = this._childWatchGroupTail._evalWatchTail;
var nextEval = prevEval._nextEvalWatch;
var prevRecord = this._childWatchGroupTail._recordTail;
var nextRecord = prevRecord.nextRecord;
_WatchGroupList._add(this, childGroup);
var evalMarker = childGroup._marker;
evalMarker._prevEvalWatch = prevEval;
evalMarker._nextEvalWatch = nextEval;
if (prevEval !== null) prevEval._nextEvalWatch = evalMarker;
if (nextEval !== null) nextEval._prevEvalWatch = evalMarker;
var childRecordHead = childGroup._recordHead;
var childRecordTail = childGroup._recordTail;
childRecordHead.prevRecord = prevRecord;
childRecordTail.nextRecord = nextRecord;
if (prevRecord !== null) prevRecord.nextRecord = childRecordHead;
if (nextRecord !== null) nextRecord.prevRecord = childRecordTail;
// TODO:(eisenbergeffect) attach observers associated with dirty records?
}
remove() {
// TODO:(eisenbergeffect) detach observers associated with dirty records?
var prevRecord = this._recordHead.prevRecord;
var nextRecord = this._childInclRecordTail.nextRecord;
if (prevRecord !== null) prevRecord.nextRecord = nextRecord;
if (nextRecord !== null) nextRecord.prevRecord = prevRecord;
// TODO:(eisenbergeffect) investigate these two lines; not sure such properties exist on records
this._recordHead._prevWatchGroup = null;
this._recordTail._prevWatchGroup = null;
_WatchGroupList._remove(this._parentWatchGroup, this);
this._nextWatchGroup = this._prevWatchGroup = null;
this._rootGroup._removeCount++;
this._parentWatchGroup = null;
// Unlink the _watchRecord
var firstEvalWatch = this._evalWatchHead;
var lastEvalWatch = (this._watchGroupTail === null
? this
: this._watchGroupTail)._evalWatchTail;
var prev = firstEvalWatch._prevEvalWatch;
var next = lastEvalWatch._nextEvalWatch;
if (prev !== null) prev._nextEvalWatch = next;
if (next !== null) next._prevEvalWatch = prev;
this._evalWatchHead._prevEvalWatch = null;
this._evalWatchTail._nextEvalWatch = null;
this._evalWatchHead = this._evalWatchTail = null;
}
toString() {
var lines = [], watch;
if (this === this._rootGroup) {
var allWatches = [];
watch = this._evalWatchHead;
var prev = null;
while (watch !== null) {
allWatches.push(watch.toString());
// TODO: Traceur assertions
// assert(watch._prevEvalWatch === prev);
prev = watch;
watch = watch._nextEvalWatch;
}
lines.push('WATCHES: ' + allWatches.join(', '));
}
var watches = [];
watch = this._evalWatchHead;
while (watch !== this._evalWatchTail) {
watches.push(watch.toString());
watch = watch._nextEvalWatch;
}
watches.push(watch.toString());
lines.push(`WatchGroup[${this.id}](watches: ${watches.join(', ')})`);
var childGroup = this._watchGroupHead;
while (childGroup !== null) {
lines.push(` ${childGroup.toString().split('\n').join('\n ')}`);
childGroup = childGroup._nextWatchGroup;
}
return lines.join("\n");
}
//
// Watch a name field on lhs represented by expression
addFieldWatch(lhs, name, expression) {
var that = this;
var fieldHandler = new _FieldHandler(this, expression);
// Create a ChangeRecord for the current field and assign the change record to the handler.
var watchRecord = this.watchField(null, name, fieldHandler);
this.fieldCost++;
fieldHandler.watchRecord = watchRecord;
var lhsWR = putIfAbsent(this._cache, lhs.expression, function() {
return lhs.setupWatch(that);
});
// We set a field forwarding handler on LHS. This will allow the change objects to propagate to
// the current WatchRecord.
lhsWR.handler.addForwardHandler(fieldHandler);
// propagate the value from the LHS to here
fieldHandler.acceptValue(lhsWR.currentValue);
return watchRecord;
}
addCollectionWatch(ast) {
var that = this;
var collectionHandler = new _CollectionHandler(this, ast.expression);
var watchRecord = this.watchField(null, null, collectionHandler);
this.collectionCost++;
collectionHandler.watchRecord = watchRecord;
// We set a field forwarding handler on LHS. This will allow the change objects to propagate to
// the current WatchRecord.
var astWR = putIfAbsent(this._cache, ast.expression, function() {
return ast.setupWatch(that);
});
// TODO: Add tests for this line!
astWR.handler.addForwardHandler(collectionHandler);
// propagate the value from the LHS to here
collectionHandler.acceptValue(astWR.currentValue);
return watchRecord;
}
addFunctionWatch(fn, argsAST, expression) {
return this._addEvalWatch(null, fn, null, argsAST, expression);
}
addMethodWatch(lhs, name, argsAST, expression) {
return this._addEvalWatch(lhs, null, name, argsAST, expression);
}
_addEvalWatch(lhsAST, fn, name, argsAST, expression) {
var that = this;
var invokeHandler = new _InvokeHandler(this, expression);
var evalWatchRecord = new _EvalWatchRecord(this, invokeHandler, fn, name, argsAST.length);
invokeHandler.watchRecord = evalWatchRecord;
if (lhsAST !== null) {
var lhsWR = putIfAbsent(this._cache, lhsAST.expression, function() {
return lhsAST.setupWatch(that);
});
lhsWR.handler.addForwardHandler(invokeHandler);
invokeHandler.acceptValue(lhsWR.currentValue);
}
// Convert the args from AST to WatchRecords
var i = 0;
argsAST.map(function(ast) {
return ast.setupWatch(that);
}).forEach(function(record) {
var argHandler = new _ArgHandler(this, evalWatchRecord, i++);
_ArgHandlerList._add(invokeHandler, argHandler);
record.handler.addForwardHandler(argHandler);
argHandler.acceptValue(record.currentValue);
});
// Must be done last
_EvalWatchList._add(this, evalWatchRecord);
this.evalCost++;
if (this._rootGroup.isInsideInvokeDirty) {
// This check means that we are inside invoke reaction function.
// Registering a new EvalWatch at this point will not run the
// .check() on it which means it will not be processed, but its
// reaction function will be run with null. So we process it manually.
evalWatchRecord.check();
}
return evalWatchRecord;
}
watchField(context, field, handler){
var getter = field === null ? null : this._getterCache.get(field);
return this._recordAdd(new ChangeRecord(this, context, field, getter, handler));
}
_recordAdd(record) {
var previous = this._recordTail,
next = previous === null ? null : previous.nextRecord;
record.nextRecord = next;
record.prevRecord = previous;
if (previous !== null) previous.nextRecord = record;
if (next !== null) next.prevRecord = record;
this._recordTail = record;
if (previous === this._dirtyMarker) this._recordRemove(this._dirtyMarker);
return record;
}
_recordRemove(record) {
var previous = record.prevRecord,
next = record.nextRecord;
if (record === this._recordHead && record === this._recordTail) {
// we are the last one, must leave marker behind.
this._recordHead = this._recordTail = this._dirtyMarker;
this._dirtyMarker.nextRecord = next;
this._dirtyMarker.prevRecord = previous;
if (previous !== null) previous.nextRecord = this._dirtyMarker;
if (next !== null) next.prevRecord = this._dirtyMarker;
} else {
if (record === this._recordTail) this._recordTail = previous;
if (record === this._recordHead) this._recordHead = next;
if (previous !== null) previous.nextRecord = next;
if (next !== null) next.prevRecord = previous;
}
}
}
export class RootWatchGroup extends WatchGroup {
constructor(getterCache, observerSelector, context) {
// TODO: Traceur Assertions
// assert(context and context is Function or Object)
this._getterCache = getterCache;
this._observerSelector = observerSelector || { getObserver(){ return null; } };
this.context = context;
this._cache = {};
this._parentWatchGroup = null;
// Initialize _WatchGroupList
this._watchGroupTail = this._watchGroupHead = null;
this.id = '';
this._nextChildId = 0;
// TODO: When _EvalWatchRecord is implemented...
this._marker = _EvalWatchRecord.marker();
this._marker.watchGrp = this;
this._evalWatchHead = this._evalWatchTail = this._marker;
this._dirtyWatchHead = this._dirtyWatchTail = null;
this._fakeHead = ChangeRecord.marker();
this._dirtyMarker = ChangeRecord.marker();
this._recordHead = this._recordTail = this._dirtyMarker;
// Stats...
this.fieldCost = 0;
this.collectionCost = 0;
this.evalCost = 0;
this._rootGroup = this;
}
getObserver(obj, field){
return this._observerSelector.getObserver(obj, field);
}
// Detect changes and process the [ReactionFn]s
//
// Algorithm:
// 1) process the [ChangeDetector#collectChanges]
// 2) process function/closure/method changes
// 3) call an [ReactionFn]s
//
// Each step is called in sequence. ([ReactionFn]s are not called until all previous steps are
// completed).
detectChanges(exceptionHandler, changeLog, fieldStopWatch, evalStopWatch, processStopWatch) {
// 1) Process the ChangeRecords from the change detector
var changeRecordIterator = this.collectChanges(exceptionHandler, fieldStopWatch);
if (processStopWatch) {
processStopWatch.start();
}
while (changeRecordIterator.iterate()) {
var record = changeRecordIterator.current;
if (changeLog){
changeLog(record.handler.expression, record.currentValue, record.previousValue);
}
record.handler.onChange(record);
}
if (processStopWatch) {
processStopWatch.stop();
}
// 2) Process function evaluations
var evalRecord = this._evalWatchHead;
var evalCount = 0;
while (evalRecord !== null) {
try {
++evalCount;
if (evalRecord.check() && changeLog){
changeLog(
evalRecord.handler.expression,
evalRecord.currentValue,
evalRecord.previousValue
);
}
} catch (e) {
if (exceptionHandler) exceptionHandler(e);
else throw e;
}
evalRecord = evalRecord._nextEvalWatch;
}
if (evalStopWatch) {
evalStopWatch.stop();
evalStopWatch.increment(evalCount);
}
if (processStopWatch){
processStopWatch.stop();
}
// Because the handler can forward changes between each other synchronously, we need to call
// reaction functions asynchronously. This processes the asynchronous reaction function queue.
var count = 0;
var dirtyWatch = this._dirtyWatchHead;
this._dirtyWatchHead = null;
var root = this._rootGroup;
root._removeCount = 0;
try {
while (dirtyWatch !== null) {
count++;
try {
if (root._removeCount === 0 || dirtyWatch._watchGroup.isAttached) {
dirtyWatch.invoke();
}
} catch (e) {
if (exceptionHandler) exceptionHandler(e);
else throw e;
}
var nextDirtyWatch = dirtyWatch._nextDirtyWatch;
dirtyWatch._nextDirtyWatch = null;
dirtyWatch = nextDirtyWatch;
}
} finally {
this._dirtyWatchTail = null;
}
if (processStopWatch) {
processStopWatch.stop();
processStopWatch.increment(count);
}
return count;
}
collectChanges(exceptionHandler, stopwatch){
if (stopwatch) {
stopwatch.start();
}
var changeTail = this._fakeHead,
current = this._recordHead,
count = 0;
while (current !== null) {
try {
if (current.check()) {
changeTail = changeTail.nextChange = current;
}
++count;
} catch (e) {
if (exceptionHandler) exceptionHandler(e);
else throw e;
}
current = current.nextRecord;
}
changeTail.nextChange = null;
if (stopwatch) {
stopwatch.stop();
stopwatch.increment(count);
}
var changeHead = this._fakeHead.nextChange;
this._fakeHead.nextChange = null;
return new ChangeRecordIterator(changeHead);
}
get isInsideInvokeDirty() {
return this._dirtyWatchHead === null && this._dirtyWatchTail !== null;
}
_addDirtyWatch(watch) {
if (!watch._dirty) {
watch._dirty = true;
if (this._dirtyWatchTail === null) {
this._dirtyWatchHead = this._dirtyWatchTail = watch;
} else {
this._dirtyWatchTail._nextDirtyWatch = watch;
this._dirtyWatchTail = watch;
}
watch._nextDirtyWatch = null;
}
return watch;
}
}