Skip to content

Commit caa3a16

Browse files
committed
refactor(scope): watch "readOnly" arg renamed to "canChangeModel"
closes dart-archive#739 This changes is a BC break: - the "readOnly" parameter has been renamed to "canChangeModel", - the semantic is inversed Before: // Default to digest (readOnly = false by default) scope.watch(exp, fn); // Explicit flush scope.watch(exp, fn, readOnly: true); After: // Default to digest (canChangeModel = true by default) scope.watch(exp, fn); // Explicit flush scope.watch(exp, fn, canChangeModel: false);
1 parent 1b4f727 commit caa3a16

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

example/web/bouncing_balls.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ class BallPositionDirective {
117117
var style = element.style;
118118
style.backgroundColor = model.color;
119119
scope
120-
..watch('x', (x, _) => style.left = px(x), context: model, readOnly: true)
121-
..watch('y', (y, _) => style.top = px(y), context: model, readOnly: true);
120+
..watch('x', (x, _) => element.style.left = '${x + 10}px',
121+
context: model, canChangeModel: false)
122+
..watch('y', (y, _) => element.style.top = '${y + 10}px',
123+
context: model, canChangeModel: false);
122124
}
123125
}
124126

lib/core/scope.dart

+12-8
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,20 @@ class Scope {
192192
/**
193193
* Use [watch] to set up a watch in the [apply] cycle.
194194
*
195-
* When [readOnly] is [:true:], the watch will be executed in the [flush]
196-
* cycle. It should be used when the [reactionFn] does not change the model
197-
* and allows the [digest] phase to converge faster.
195+
* When [canChangeModel] is [:false:], the watch will be executed in the
196+
* [flush] cycle. It should be used when the [reactionFn] does not change the
197+
* model and allows speeding up the [digest] phase.
198198
*
199-
* On the opposite, [readOnly] should be set to [:false:] if the [reactionFn]
200-
* could change the model so that the watch is observed in the [digest] cycle.
199+
* On the opposite, [canChangeModel] should be set to [:true:] if the
200+
* [reactionFn] could change the model so that the watch is evaluated in the
201+
* [digest] cycle.
201202
*/
202-
Watch watch(String expression, ReactionFn reactionFn, {context,
203-
FilterMap filters, bool readOnly: false, bool collection: false}) {
203+
Watch watch(String expression, ReactionFn reactionFn, {context,
204+
FilterMap filters, bool canChangeModel: true, bool collection: false}) {
204205
assert(isAttached);
205206
assert(expression is String);
207+
assert(canChangeModel is bool);
208+
206209
Watch watch;
207210
ReactionFn fn = reactionFn;
208211
if (expression.isEmpty) {
@@ -224,7 +227,8 @@ class Scope {
224227

225228
AST ast = rootScope._astParser(expression, context: context,
226229
filters: filters, collection: collection);
227-
WatchGroup group = readOnly ? _readOnlyGroup : _readWriteGroup;
230+
231+
WatchGroup group = canChangeModel ? _readWriteGroup : _readOnlyGroup;
228232
return watch = group.watch(ast, fn);
229233
}
230234

lib/core_dom/ng_mustache.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ class NgTextMustacheDirective {
1212
FilterMap filters) {
1313
String expression = interpolate(template);
1414

15-
scope.watch(expression, _updateMarkup, readOnly: true, filters: filters);
15+
scope.watch(expression,
16+
_updateMarkup,
17+
canChangeModel: false,
18+
filters: filters);
1619
}
1720

1821
void _updateMarkup(text, previousText) {
@@ -45,7 +48,7 @@ class NgAttrMustacheDirective {
4548
_hasObservers = hasObservers;
4649
if (_watch != null) _watch.remove();
4750
_watch = scope.watch(expression, _updateMarkup, filters: filters,
48-
readOnly: !_hasObservers);
51+
canChangeModel: _hasObservers);
4952
}
5053
});
5154
}

lib/directive/ng_class.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ abstract class _NgClassBase {
173173
currentSet = _flatten(current);
174174
_handleChange(scope.context[r'$index']);
175175
},
176-
readOnly: true,
176+
canChangeModel: false,
177177
collection: true);
178178

179179
if (mode != null) {
@@ -186,7 +186,7 @@ abstract class _NgClassBase {
186186
previousSet.forEach((css) => _animate.removeClass(element, css));
187187
}
188188
}
189-
}, readOnly: true);
189+
}, canChangeModel: false);
190190
}
191191
}
192192

lib/directive/ng_style.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class NgStyleDirective {
2727
set styleExpression(String value) {
2828
_styleExpression = value;
2929
if (_watch != null) _watch.remove();
30-
_watch = _scope.watch(_styleExpression, _onStyleChange, readOnly: true,
31-
collection: true);
30+
_watch = _scope.watch(_styleExpression, _onStyleChange, collection: true,
31+
canChangeModel: false);
3232
}
3333

3434
_onStyleChange(MapChangeRecord mapChangeRecord, _) {

test/core/scope_spec.dart

+8-6
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ void main() {
862862
it(r'should apply expression with full lifecycle', (RootScope rootScope) {
863863
var log = '';
864864
var child = rootScope.createChild({"parent": rootScope.context});
865-
rootScope.watch('a', (a, _) { log += '1'; }, readOnly: true);
865+
rootScope.watch('a', (a, _) { log += '1'; }, canChangeModel: false);
866866
child.apply('parent.a = 0');
867867
expect(log).toEqual('1');
868868
});
@@ -871,7 +871,7 @@ void main() {
871871
it(r'should schedule domWrites and domReads', (RootScope rootScope) {
872872
var log = '';
873873
var child = rootScope.createChild({"parent": rootScope.context});
874-
rootScope.watch('a', (a, _) { log += '1'; }, readOnly: true);
874+
rootScope.watch('a', (a, _) { log += '1'; }, canChangeModel: false);
875875
child.apply('parent.a = 0');
876876
expect(log).toEqual('1');
877877
});
@@ -884,7 +884,7 @@ void main() {
884884
beforeEach((RootScope rootScope) {
885885
rootScope.context['log'] = () { log += 'digest;'; return null; };
886886
log = '';
887-
rootScope.watch('log()', (v, o) => null, readOnly: true);
887+
rootScope.watch('log()', (v, o) => null, canChangeModel: false);
888888
rootScope.digest();
889889
log = '';
890890
});
@@ -893,7 +893,7 @@ void main() {
893893
LoggingExceptionHandler exceptionHandler = e;
894894
var log = [];
895895
var child = rootScope.createChild({});
896-
rootScope.watch('a', (a, _) => log.add('1'), readOnly: true);
896+
rootScope.watch('a', (a, _) => log.add('1'), canChangeModel: false);
897897
rootScope.context['a'] = 0;
898898
child.apply(() { throw 'MyError'; });
899899
expect(log.join(',')).toEqual('1');
@@ -929,7 +929,8 @@ void main() {
929929
rootScope.context['logger'] = (name) { log(name); return retValue; };
930930

931931
rootScope.watch('logger("watch")', (n, v) => null);
932-
rootScope.watch('logger("flush")', (n, v) => null, readOnly: true);
932+
rootScope.watch('logger("flush")', (n, v) => null,
933+
canChangeModel: false);
933934

934935
// clear watches
935936
rootScope.digest();
@@ -1426,7 +1427,8 @@ void main() {
14261427
rootScope.domWrite(() => logger('write3'));
14271428
throw 'read1';
14281429
});
1429-
rootScope.watch('value', (_, __) => logger('observe'), readOnly: true);
1430+
rootScope.watch('value', (_, __) => logger('observe'),
1431+
canChangeModel: false);
14301432
rootScope.flush();
14311433
expect(logger).toEqual(['write1', 'write2', 'observe', 'read1', 'read2', 'write3']);
14321434
expect(exceptionHandler.errors.length).toEqual(2);

0 commit comments

Comments
 (0)