Skip to content

Commit 83b2cee

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 8fa8de9 commit 83b2cee

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

example/web/bouncing_balls.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ class BallPositionDirective {
109109
set position(BallModel model) {
110110
element.style.backgroundColor = model.color;
111111
scope
112-
..watch('x', (x, _) => element.style.left = '${x + 10}px', context: model, readOnly: true)
113-
..watch('y', (y, _) => element.style.top = '${y + 10}px', context: model, readOnly: true);
112+
..watch('x', (x, _) => element.style.left = '${x + 10}px',
113+
context: model, canChangeModel: false)
114+
..watch('y', (y, _) => element.style.top = '${y + 10}px',
115+
context: model, canChangeModel: false);
114116
}
115117
}
116118

lib/core/scope.dart

+9-7
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,19 @@ 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
*/
202203
Watch watch(String expression, ReactionFn reactionFn, {Object context,
203-
FilterMap filters, bool readOnly: false, bool collection: false}) {
204+
FilterMap filters, bool canChangeModel: true, bool collection: false}) {
204205
assert(isAttached);
205206
assert(expression is String);
207+
assert(canChangeModel is bool);
206208

207209
Watch watch;
208210
ReactionFn fn = reactionFn;
@@ -223,7 +225,7 @@ class Scope {
223225
AST ast = rootScope._astParser(expression, context: context,
224226
filters: filters, collection: collection);
225227

226-
WatchGroup group = readOnly ? _readOnlyGroup : _readWriteGroup;
228+
WatchGroup group = canChangeModel ? _readWriteGroup : _readOnlyGroup;
227229
return watch = group.watch(ast, fn);
228230
}
229231

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) {
@@ -42,7 +45,7 @@ class NgAttrMustacheDirective {
4245
_hasObservers = hasObservers;
4346
if (_watch != null) _watch.remove();
4447
_watch = scope.watch(expression, _updateMarkup, filters: filters,
45-
readOnly: !_hasObservers);
48+
canChangeModel: _hasObservers);
4649
}
4750
});
4851
}

lib/directive/ng_class.dart

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

180180
if (mode != null) {
@@ -187,7 +187,7 @@ abstract class _NgClassBase {
187187
previousSet.forEach((css) => _animate.removeClass(element, css));
188188
}
189189
}
190-
}, readOnly: true);
190+
}, canChangeModel: false);
191191
}
192192
}
193193

lib/directive/ng_style.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class NgStyleDirective {
2828
_styleExpression = value;
2929
if (_watch != null) _watch.remove();
3030
_watch = _scope.watch(_styleExpression, _onStyleChange, collection: true,
31-
readOnly: 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();
@@ -1413,7 +1414,8 @@ void main() {
14131414
rootScope.domWrite(() => logger('write3'));
14141415
throw 'read1';
14151416
});
1416-
rootScope.watch('value', (_, __) => logger('observe'), readOnly: true);
1417+
rootScope.watch('value', (_, __) => logger('observe'),
1418+
canChangeModel: false);
14171419
rootScope.flush();
14181420
expect(logger).toEqual(['write1', 'write2', 'observe', 'read1', 'read2', 'write3']);
14191421
expect(exceptionHandler.errors.length).toEqual(2);

0 commit comments

Comments
 (0)