@@ -22,7 +22,9 @@ var _MODE_GETTER_ = 3;
22
22
var _MODE_MAP_FIELD_ = 4 ;
23
23
var _MODE_ITERABLE_ = 5 ;
24
24
var _MODE_MAP_ = 6 ;
25
- var _MODE_MAP_FIELD_NOTIFY_ONLY_ = 10 ;
25
+ var _NOT_NOTIFIED_ = 10 ;
26
+ var _NOTIFIED_ = 11 ;
27
+
26
28
export class GetterCache {
27
29
constructor ( map ) {
28
30
this . _map = map ;
@@ -31,6 +33,7 @@ export class GetterCache {
31
33
return this . _map [ field ] || null ;
32
34
}
33
35
}
36
+
34
37
export class DirtyCheckingChangeDetectorGroup extends ChangeDetector {
35
38
constructor ( parent , cache ) {
36
39
this . _parent = parent ;
@@ -186,11 +189,16 @@ export class DirtyCheckingChangeDetectorGroup extends ChangeDetector {
186
189
return lines . join ( '\n' ) ;
187
190
}
188
191
}
192
+
189
193
export class DirtyCheckingChangeDetector extends DirtyCheckingChangeDetectorGroup {
190
- constructor ( cache , notifier = new ChangeNotifier ( ) ) {
194
+ constructor ( cache , observerSelector = null ) {
191
195
super ( null , cache ) ;
192
196
this . _fakeHead = DirtyCheckingRecord . marker ( ) ;
193
- this . _notifier = notifier ;
197
+ this . _observerSelector = observerSelector || { getObserver ( ) { return null ; } } ;
198
+ }
199
+
200
+ getObserver ( obj , field ) {
201
+ return this . _observerSelector . getObserver ( obj , field ) ;
194
202
}
195
203
196
204
_assertRecordsOk ( ) {
@@ -278,47 +286,6 @@ class ChangeIterator {
278
286
}
279
287
}
280
288
281
- // TODO: Add unit tests for this!
282
- export class ChangeNotifier {
283
- constructor ( ) {
284
- this . objectRecords = new WeakMap ( ) ;
285
- }
286
- notify ( object ) {
287
- var records = this . objectRecords . get ( object ) || [ ] ;
288
- records . forEach ( ( record ) => {
289
- // TODO: Is this the correct way of manually dirty checking
290
- // some records??
291
- var watch = record . _handler . _watchHead ;
292
- if ( watch && record . check ( true ) ) {
293
- watch . _dirty = true ;
294
- watch . invoke ( ) ;
295
- }
296
- } ) ;
297
- }
298
- isNotifyOnly ( object , fieldName ) {
299
- return false ;
300
- }
301
- addWatch ( object , fieldName , record ) {
302
- var records = this . objectRecords . get ( object ) ;
303
- if ( ! records ) {
304
- records = [ ] ;
305
- this . objectRecords . set ( object , records ) ;
306
- }
307
- records . push ( record ) ;
308
- return records . length ;
309
- }
310
- removeWatch ( object , fieldName , record ) {
311
- var records = this . objectRecords . get ( object ) ;
312
- if ( records ) {
313
- var index = records . indexOf ( record ) ;
314
- if ( index !== - 1 ) {
315
- records . splice ( index , 1 ) ;
316
- }
317
- }
318
- return records ? records . length : 0 ;
319
- }
320
- }
321
-
322
289
class DirtyCheckingRecord extends ChangeRecord {
323
290
constructor ( group , object , fieldName , getter , handler ) {
324
291
this . _group = group ;
@@ -351,21 +318,22 @@ class DirtyCheckingRecord extends ChangeRecord {
351
318
return this . _object ;
352
319
}
353
320
_clearObject ( ) {
354
- var notifier = this . _group && this . _group . _root . _notifier ;
355
- if ( notifier && this . _object && (
356
- this . _mode === _MODE_MAP_FIELD_NOTIFY_ONLY_ || this . _mode === _MODE_MAP_FIELD_ )
357
- ) {
358
- notifier . removeWatch ( this . _object , this . _field , this ) ;
321
+ if ( this . _observer ) {
322
+ this . _observer . close ( ) ;
323
+ this . _observer = null ;
359
324
}
325
+
360
326
this . _object = null ;
361
327
}
362
328
set object ( obj ) {
363
329
this . _clearObject ( obj ) ;
364
330
this . _object = obj ;
331
+
365
332
if ( obj === null ) {
366
333
this . _mode = _MODE_IDENTITY_ ;
367
334
return ;
368
335
}
336
+
369
337
if ( this . field === null ) {
370
338
// _instanceMirror = null; --- Again, do we need reflection?
371
339
if ( typeof obj === "object" ) {
@@ -385,28 +353,32 @@ class DirtyCheckingRecord extends ChangeRecord {
385
353
}
386
354
return ;
387
355
}
388
- if ( typeof obj === "object" ) {
389
- var notifier = this . _group && this . _group . _root . _notifier ;
390
- if ( notifier && notifier . isNotifyOnly ( obj , this . _field ) ) {
391
- this . _mode = _MODE_MAP_FIELD_NOTIFY_ONLY_ ;
392
- } else {
393
- this . _mode = _MODE_MAP_FIELD_ ;
394
- }
395
- notifier . addWatch ( obj , this . _field , this ) ;
396
- // _instanceMirror = null; --- Reflection needed?
397
- } else if ( this . _getter !== null ) {
356
+
357
+ this . _observer = this . _group && this . _group . _root . getObserver ( obj , this . _field ) ;
358
+
359
+ if ( this . _observer ) {
360
+ this . _mode = _NOTIFIED_ ;
361
+ this . newValue = this . _observer . open ( ( value ) => {
362
+ this . newValue = value ;
363
+ this . _mode = _NOTIFIED_ ;
364
+ } ) ;
365
+ } else if ( this . _getter !== null ) {
398
366
this . _mode = _MODE_GETTER_ ;
399
- // _instanceMirror = null; --- Reflection needed?
400
- } else {
401
- this . _mode = _MODE_REFLECT_ ;
402
- // _instanceMirror = reflect(obj); --- I'm really not sure about this!
367
+ } else {
368
+ this . _mode = _MODE_MAP_FIELD_ ;
403
369
}
404
370
}
405
- check ( inNotify ) {
371
+ check ( ) {
406
372
// assert(_mode != null); --- Traceur v0.0.24 missing assert()
407
373
var current ;
408
374
switch ( this . _mode ) {
409
- case _MODE_MARKER_ : return false ;
375
+ case _NOT_NOTIFIED_ :
376
+ case _MODE_MARKER_ :
377
+ return false ;
378
+ case _NOTIFIED_ :
379
+ current = this . newValue ;
380
+ this . _mode = _NOT_NOTIFIED_ ;
381
+ break ;
410
382
case _MODE_REFLECT_ :
411
383
// TODO:
412
384
// I'm not sure how much support for Reflection is available in Traceur
@@ -417,14 +389,7 @@ class DirtyCheckingRecord extends ChangeRecord {
417
389
break ;
418
390
case _MODE_GETTER_ :
419
391
current = this . _getter ( this . object ) ;
420
- break ;
421
- case _MODE_MAP_FIELD_NOTIFY_ONLY_ :
422
- if ( inNotify ) {
423
- current = this . object [ this . field ] ;
424
- } else {
425
- return false ;
426
- }
427
- break ;
392
+ break ;
428
393
case _MODE_MAP_FIELD_ :
429
394
if ( ! this . object ) return undefined ;
430
395
current = this . object [ this . field ] ;
@@ -439,6 +404,7 @@ class DirtyCheckingRecord extends ChangeRecord {
439
404
throw "UNREACHABLE" ;
440
405
// assert(false); --- Traceur 0.0.24 missing assert()
441
406
}
407
+
442
408
var last = this . currentValue ;
443
409
if ( last !== current ) {
444
410
// TODO:
@@ -458,6 +424,7 @@ class DirtyCheckingRecord extends ChangeRecord {
458
424
return true ;
459
425
}
460
426
}
427
+
461
428
return false ;
462
429
}
463
430
remove ( ) {
0 commit comments