Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit b871575

Browse files
vicbmhevery
authored andcommitted
style(change detection): some cleanup
Closes #834
1 parent 35fad7a commit b871575

6 files changed

+68
-75
lines changed

lib/change_detection/ast.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ String _argList(List<AST> items) => items.join(', ');
124124
/**
125125
* The name is a bit oxymoron, but it is essentially the NullObject pattern.
126126
*
127-
* This allows children to set a handler on this Record and then let it write the initial
128-
* constant value to the forwarding Record.
127+
* This allows children to set a handler on this Record and then let it write
128+
* the initial constant value to the forwarding Record.
129129
*/
130130
class _ConstantWatchRecord extends WatchRecord<_Handler> {
131131
final currentValue;

lib/change_detection/change_detection.dart

+41-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library change_detection;
22

3-
typedef EvalExceptionHandler(error, stack);
3+
typedef void EvalExceptionHandler(error, stack);
44

55
/**
66
* An interface for [ChangeDetectorGroup] groups related watches together. It
@@ -13,49 +13,48 @@ abstract class ChangeDetectorGroup<H> {
1313
* Watch a specific [field] on an [object].
1414
*
1515
* If the [field] is:
16-
* - _name_ - Name of the property to watch. (If the [object] is a Map then
16+
* * _name_ - Name of the property to watch. (If the [object] is a Map then
1717
* treat the name as a key.)
18-
* - _[]_ - Watch all items in an array.
19-
* - _{}_ - Watch all items in a Map.
20-
* - _._ - Watch the actual object identity.
21-
*
18+
* * _null_ - Watch all the items for arrays and maps otherwise the object
19+
* identity.
2220
*
2321
* Parameters:
24-
* - [object] to watch.
25-
* - [field] to watch on the [object].
26-
* - [handler] an opaque object passed on to [Record].
22+
* * [object] to watch.
23+
* * [field] to watch on the [object].
24+
* * [handler] an opaque object passed on to [Record].
2725
*/
2826
WatchRecord<H> watch(Object object, String field, H handler);
2927

30-
/** Use to remove all watches in the group in an efficient manner. */
28+
/// Remove all the watches in an efficient manner.
3129
void remove();
3230

33-
/** Create a child [ChangeDetectorGroup] */
31+
/// Create a child [ChangeDetectorGroup]
3432
ChangeDetectorGroup<H> newGroup();
3533
}
3634

3735
/**
3836
* An interface for [ChangeDetector]. An application can have multiple instances
39-
* of the [ChangeDetector] to be used for checking different application domains.
37+
* of the [ChangeDetector] to be used for checking different application
38+
* domains.
4039
*
4140
* [ChangeDetector] works by comparing the identity of the objects not by
4241
* calling the `.equals()` method. This is because ChangeDetector needs to have
4342
* predictable performance, and the developer can implement `.equals()` on top
4443
* of identity checks.
4544
*
46-
* - [H] A [Record] has associated handler object. The handler object is
47-
* opaque to the [ChangeDetector] but it is meaningful to the code which
48-
* registered the watcher. It can be a data structure, an object, or a function.
49-
* It is up to the developer to attach meaning to it.
45+
* [H] A [Record] has associated handler object. The handler object is opaque
46+
* to the [ChangeDetector] but it is meaningful to the code which registered the
47+
* watcher. It can be a data structure, an object, or a function. It is up to
48+
* the developer to attach meaning to it.
5049
*/
5150
abstract class ChangeDetector<H> extends ChangeDetectorGroup<H> {
5251
/**
5352
* This method does the work of collecting the changes and returns them as a
5453
* linked list of [Record]s. The [Record]s are returned in the
5554
* same order as they were registered.
5655
*/
57-
Iterator<Record<H>> collectChanges({ EvalExceptionHandler exceptionHandler,
58-
AvgStopwatch stopwatch });
56+
Iterator<Record<H>> collectChanges({EvalExceptionHandler exceptionHandler,
57+
AvgStopwatch stopwatch });
5958
}
6059

6160
abstract class Record<H> {
@@ -64,10 +63,9 @@ abstract class Record<H> {
6463

6564
/**
6665
* The field which is being watched:
67-
* - _name_ - Name of the field to watch.
68-
* - _[]_ - Watch all items in an array.
69-
* - _{}_ - Watch all items in a Map.
70-
* - _._ - Watch the actual object identity.
66+
* * _name_ - Name of the field to watch.
67+
* * _null_ - Watch all the items for arrays and maps otherwise the object
68+
* identity.
7169
*/
7270
String get field;
7371

@@ -78,9 +76,16 @@ abstract class Record<H> {
7876
*/
7977
H get handler;
8078

81-
/** Current value of the [field] on the [object] */
79+
/**
80+
* * The current value of the [field] on the [object],
81+
* * a [CollectionChangeRecord] if an iterable is observed,
82+
* * a [MapChangeRecord] if a map is observed.
83+
*/
8284
get currentValue;
83-
/** Previous value of the [field] on the [object] */
85+
/**
86+
* * Previous value of the [field] on the [object],
87+
* * [:null:] when an iterable or a map are observed.
88+
*/
8489
get previousValue;
8590
}
8691

@@ -89,27 +94,24 @@ abstract class Record<H> {
8994
* manually triggering the checking.
9095
*/
9196
abstract class WatchRecord<H> extends Record<H> {
92-
/** Set a new object for checking */
97+
/// Set a new object for checking
9398
set object(value);
9499

95-
/**
96-
* Check to see if the field on the object has changed. Returns [true] if
97-
* change has been detected.
98-
*/
100+
/// Returns [:true:] when changes have been detected
99101
bool check();
100102

101103
void remove();
102104
}
103105

104106
/**
105107
* If the [ChangeDetector] is watching a [Map] then the [currentValue] of
106-
* [Record] will contain an instance of this object. A [MapChangeRecord]
108+
* [Record] will contain an instance of [MapChangeRecord]. A [MapChangeRecord]
107109
* contains the changes to the map since the last execution. The changes are
108110
* reported as a list of [MapKeyValue]s which contain the key as well as its
109111
* current and previous value.
110112
*/
111113
abstract class MapChangeRecord<K, V> {
112-
/// The underlying iterable object
114+
/// The underlying map object
113115
Map get map;
114116

115117
/// A list of [CollectionKeyValue]s which are in the iteration order. */
@@ -162,13 +164,12 @@ abstract class ChangedKeyValue<K, V> extends MapKeyValue<K, V> {
162164
ChangedKeyValue<K, V> get nextChangedKeyValue;
163165
}
164166

165-
166167
/**
167168
* If the [ChangeDetector] is watching an [Iterable] then the [currentValue] of
168-
* [Record] will contain this object. The [CollectionChangeRecord] contains the
169-
* changes to the collection since the last execution. The changes are reported
170-
* as a list of [CollectionChangeItem]s which contain the item as well as its
171-
* current and previous position in the list.
169+
* [Record] will contain an instance of [CollectionChangeRecord]. The
170+
* [CollectionChangeRecord] contains the changes to the collection since the
171+
* last execution. The changes are reported as a list of [CollectionChangeItem]s
172+
* which contain the item as well as its current and previous index.
172173
*/
173174
abstract class CollectionChangeRecord<V> {
174175
/** The underlying iterable object */
@@ -195,10 +196,10 @@ abstract class CollectionChangeRecord<V> {
195196
* which tracks the [item]s [currentKey] and [previousKey] location.
196197
*/
197198
abstract class CollectionChangeItem<V> {
198-
/** Previous item location in the list or [null] if addition. */
199+
/** Previous item location in the list or [:null:] if addition. */
199200
int get previousIndex;
200201

201-
/** Current item location in the list or [null] if removal. */
202+
/** Current item location in the list or [:null:] if removal. */
202203
int get currentIndex;
203204

204205
/** The item. */
@@ -241,8 +242,8 @@ abstract class RemovedItem<V> extends CollectionChangeItem<V> {
241242
RemovedItem<V> get nextRemovedItem;
242243
}
243244

244-
typedef FieldGetter(object);
245-
typedef FieldSetter(object, value);
245+
typedef dynamic FieldGetter(object);
246+
typedef void FieldSetter(object, value);
246247

247248
abstract class FieldGetterFactory {
248249
get isMethodInvoke;

lib/change_detection/dirty_checking_change_detector.dart

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import 'package:angular/change_detection/change_detection.dart';
99
*
1010
* GOALS:
1111
* - Plugable implementation, replaceable with other technologies, such as
12-
* Object.observe().
12+
* Object.observe().
1313
* - SPEED this needs to be as fast as possible.
1414
* - No GC pressure. Since change detection runs often it should perform no
15-
* memory allocations.
15+
* memory allocations.
1616
* - The changes need to be delivered in a single data-structure at once.
17-
* There are two reasons for this:
18-
* 1. It should be easy to measure the cost of change detection vs
19-
* processing.
20-
* 2. The feature may move to VM for performance reason. The VM should be
21-
* free to implement it in any way. The only requirement is that the list of
22-
* changes need to be delivered.
17+
* There are two reasons for this:
18+
* 1. It should be easy to measure the cost of change detection vs
19+
* processing.
20+
* 2. The feature may move to VM for performance reason. The VM should be
21+
* free to implement it in any way. The only requirement is that the
22+
* list of changes need to be delivered.
2323
*
2424
* [DirtyCheckingRecord]
2525
*
@@ -395,8 +395,8 @@ class DirtyCheckingRecord<H> implements Record<H>, WatchRecord<H> {
395395
FieldGetter _getter;
396396

397397
DirtyCheckingRecord(this._group, this._fieldGetterFactory, this.handler,
398-
this.field, object) {
399-
this.object = object;
398+
this.field, _object) {
399+
object = _object;
400400
}
401401

402402
DirtyCheckingRecord.marker()
@@ -407,14 +407,14 @@ class DirtyCheckingRecord<H> implements Record<H>, WatchRecord<H> {
407407
_getter = null,
408408
_mode = _MODE_MARKER_;
409409

410-
get object => _object;
410+
dynamic get object => _object;
411411

412412
/**
413413
* Setting an [object] will cause the setter to introspect it and place
414414
* [DirtyCheckingRecord] into different access modes. If Object it sets up
415415
* reflection. If [Map] then it sets up map accessor.
416416
*/
417-
set object(obj) {
417+
void set object(obj) {
418418
_object = obj;
419419
if (obj == null) {
420420
_mode = _MODE_IDENTITY_;

lib/change_detection/dirty_checking_change_detector_dynamic.dart

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,20 @@ class DynamicFieldGetterFactory implements FieldGetterFactory {
1717
try {
1818
return method(object, name) != null;
1919
} catch (e, s) {
20-
print('isMethod($object, $name) => false\n$s');
2120
return false;
2221
}
2322
}
2423

2524
Function method(Object object, String name) {
2625
Symbol symbol = new Symbol(name);
2726
InstanceMirror instanceMirror = reflect(object);
28-
return (List args, Map namedArgs) {
29-
return instanceMirror.invoke(symbol, args, namedArgs).reflectee;
30-
};
27+
return (List args, Map namedArgs) =>
28+
instanceMirror.invoke(symbol, args, namedArgs).reflectee;
3129
}
3230

3331
FieldGetter getter(Object object, String name) {
3432
Symbol symbol = new Symbol(name);
3533
InstanceMirror instanceMirror = reflect(object);
36-
return (Object object) {
37-
return instanceMirror.getField(symbol).reflectee;
38-
};
34+
return (Object object) => instanceMirror.getField(symbol).reflectee;
3935
}
4036
}

lib/change_detection/dirty_checking_change_detector_static.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class StaticFieldGetterFactory implements FieldGetterFactory {
2222
}
2323

2424
Function method(Object object, String name) {
25-
var getter = getters[name];
26-
if (getter == null) throw "Missing getter: (o) => o.$name";
27-
return getter;
25+
var method = getters[name];
26+
if (method == null) throw "Missing method: $name";
27+
return method;
2828
}
2929
}

lib/change_detection/watch_group.dart

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ part 'linked_list.dart';
66
part 'ast.dart';
77
part 'prototype_map.dart';
88

9-
typedef ReactionFn(value, previousValue);
10-
typedef ChangeLog(String expression, current, previous);
9+
typedef void ReactionFn(value, previousValue);
10+
typedef void ChangeLog(String expression, current, previous);
1111

1212
/**
1313
* Extend this class if you wish to pretend to be a function, but you don't know
@@ -340,7 +340,7 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList {
340340
lines.add('WatchGroup[$id](watches: ${watches.join(', ')})');
341341
var childGroup = _watchGroupHead;
342342
while (childGroup != null) {
343-
lines.add(' ' + childGroup.toString().split('\n').join('\n '));
343+
lines.add(' ' + childGroup.toString().replace('\n', '\n '));
344344
childGroup = childGroup._nextWatchGroup;
345345
}
346346
return lines.join('\n');
@@ -364,10 +364,10 @@ class RootWatchGroup extends WatchGroup {
364364
int _removeCount = 0;
365365

366366

367-
RootWatchGroup(FieldGetterFactory this._fieldGetterFactory,
367+
RootWatchGroup(this._fieldGetterFactory,
368368
ChangeDetector changeDetector,
369-
Object context):
370-
super._root(changeDetector, context);
369+
Object context)
370+
: super._root(changeDetector, context);
371371

372372
RootWatchGroup get _rootGroup => this;
373373

@@ -597,7 +597,7 @@ abstract class _Handler implements _LinkedList, _LinkedListItem, _WatchList {
597597
}
598598

599599
class _ConstantHandler extends _Handler {
600-
_ConstantHandler(WatchGroup watchGroup, String expression, dynamic constantValue)
600+
_ConstantHandler(WatchGroup watchGroup, String expression, constantValue)
601601
: super(watchGroup, expression)
602602
{
603603
watchRecord = new _EvalWatchRecord.constant(this, constantValue);
@@ -681,10 +681,6 @@ class _InvokeHandler extends _Handler implements _ArgHandlerList {
681681
watchRecord.object = object;
682682
}
683683

684-
void onChange(Record<_Handler> record) {
685-
super.onChange(record);
686-
}
687-
688684
void _releaseWatch() {
689685
(watchRecord as _EvalWatchRecord).remove();
690686
}

0 commit comments

Comments
 (0)