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

refactor(scope): watch "readOnly" arg renamed to "canChangeModel" #817

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions example/web/bouncing_balls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ class BallPositionDirective {
var style = element.style;
style.backgroundColor = model.color;
scope
..watch('x', (x, _) => style.left = px(x), context: model, readOnly: true)
..watch('y', (y, _) => style.top = px(y), context: model, readOnly: true);
..watch('x', (x, _) => element.style.left = '${x + 10}px',
context: model, canChangeModel: false)
..watch('y', (y, _) => element.style.top = '${y + 10}px',
context: model, canChangeModel: false);
}
}

Expand Down
20 changes: 12 additions & 8 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,20 @@ class Scope {
/**
* Use [watch] to set up a watch in the [apply] cycle.
*
* When [readOnly] is [:true:], the watch will be executed in the [flush]
* cycle. It should be used when the [reactionFn] does not change the model
* and allows the [digest] phase to converge faster.
* When [canChangeModel] is [:false:], the watch will be executed in the
* [flush] cycle. It should be used when the [reactionFn] does not change the
* model and allows speeding up the [digest] phase.
*
* On the opposite, [readOnly] should be set to [:false:] if the [reactionFn]
* could change the model so that the watch is observed in the [digest] cycle.
* On the opposite, [canChangeModel] should be set to [:true:] if the
* [reactionFn] could change the model so that the watch is evaluated in the
* [digest] cycle.
*/
Watch watch(String expression, ReactionFn reactionFn, {context,
FilterMap filters, bool readOnly: false, bool collection: false}) {
Watch watch(String expression, ReactionFn reactionFn, {context,
FilterMap filters, bool canChangeModel: true, bool collection: false}) {
assert(isAttached);
assert(expression is String);
assert(canChangeModel is bool);

Watch watch;
ReactionFn fn = reactionFn;
if (expression.isEmpty) {
Expand All @@ -224,7 +227,8 @@ class Scope {

AST ast = rootScope._astParser(expression, context: context,
filters: filters, collection: collection);
WatchGroup group = readOnly ? _readOnlyGroup : _readWriteGroup;

WatchGroup group = canChangeModel ? _readWriteGroup : _readOnlyGroup;
return watch = group.watch(ast, fn);
}

Expand Down
7 changes: 5 additions & 2 deletions lib/core_dom/ng_mustache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class NgTextMustacheDirective {
FilterMap filters) {
String expression = interpolate(template);

scope.watch(expression, _updateMarkup, readOnly: true, filters: filters);
scope.watch(expression,
_updateMarkup,
canChangeModel: false,
filters: filters);
}

void _updateMarkup(text, previousText) {
Expand Down Expand Up @@ -45,7 +48,7 @@ class NgAttrMustacheDirective {
_hasObservers = hasObservers;
if (_watch != null) _watch.remove();
_watch = scope.watch(expression, _updateMarkup, filters: filters,
readOnly: !_hasObservers);
canChangeModel: _hasObservers);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/directive/ng_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ abstract class _NgClassBase {
currentSet = _flatten(current);
_handleChange(scope.context[r'$index']);
},
readOnly: true,
canChangeModel: false,
collection: true);

if (mode != null) {
Expand All @@ -186,7 +186,7 @@ abstract class _NgClassBase {
previousSet.forEach((css) => _animate.removeClass(element, css));
}
}
}, readOnly: true);
}, canChangeModel: false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/directive/ng_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class NgStyleDirective {
set styleExpression(String value) {
_styleExpression = value;
if (_watch != null) _watch.remove();
_watch = _scope.watch(_styleExpression, _onStyleChange, readOnly: true,
collection: true);
_watch = _scope.watch(_styleExpression, _onStyleChange, collection: true,
canChangeModel: false);
}

_onStyleChange(MapChangeRecord mapChangeRecord, _) {
Expand Down
14 changes: 8 additions & 6 deletions test/core/scope_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ void main() {
it(r'should apply expression with full lifecycle', (RootScope rootScope) {
var log = '';
var child = rootScope.createChild({"parent": rootScope.context});
rootScope.watch('a', (a, _) { log += '1'; }, readOnly: true);
rootScope.watch('a', (a, _) { log += '1'; }, canChangeModel: false);
child.apply('parent.a = 0');
expect(log).toEqual('1');
});
Expand All @@ -871,7 +871,7 @@ void main() {
it(r'should schedule domWrites and domReads', (RootScope rootScope) {
var log = '';
var child = rootScope.createChild({"parent": rootScope.context});
rootScope.watch('a', (a, _) { log += '1'; }, readOnly: true);
rootScope.watch('a', (a, _) { log += '1'; }, canChangeModel: false);
child.apply('parent.a = 0');
expect(log).toEqual('1');
});
Expand All @@ -884,7 +884,7 @@ void main() {
beforeEach((RootScope rootScope) {
rootScope.context['log'] = () { log += 'digest;'; return null; };
log = '';
rootScope.watch('log()', (v, o) => null, readOnly: true);
rootScope.watch('log()', (v, o) => null, canChangeModel: false);
rootScope.digest();
log = '';
});
Expand All @@ -893,7 +893,7 @@ void main() {
LoggingExceptionHandler exceptionHandler = e;
var log = [];
var child = rootScope.createChild({});
rootScope.watch('a', (a, _) => log.add('1'), readOnly: true);
rootScope.watch('a', (a, _) => log.add('1'), canChangeModel: false);
rootScope.context['a'] = 0;
child.apply(() { throw 'MyError'; });
expect(log.join(',')).toEqual('1');
Expand Down Expand Up @@ -929,7 +929,8 @@ void main() {
rootScope.context['logger'] = (name) { log(name); return retValue; };

rootScope.watch('logger("watch")', (n, v) => null);
rootScope.watch('logger("flush")', (n, v) => null, readOnly: true);
rootScope.watch('logger("flush")', (n, v) => null,
canChangeModel: false);

// clear watches
rootScope.digest();
Expand Down Expand Up @@ -1426,7 +1427,8 @@ void main() {
rootScope.domWrite(() => logger('write3'));
throw 'read1';
});
rootScope.watch('value', (_, __) => logger('observe'), readOnly: true);
rootScope.watch('value', (_, __) => logger('observe'),
canChangeModel: false);
rootScope.flush();
expect(logger).toEqual(['write1', 'write2', 'observe', 'read1', 'read2', 'write3']);
expect(exceptionHandler.errors.length).toEqual(2);
Expand Down