Skip to content

Commit e8a5ce7

Browse files
committed
feat(scope): Deprecate Scope.watch's context parameter.
Scope.watch should no longer switch context. Instead of saying scope.watch(expression, (v, _) { }, context: o); Create a new isolate child scope scope.createChild(o).watch(expression, (v, _) { });
1 parent 6051340 commit e8a5ce7

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/core/scope.dart

+24-8
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ class Scope {
200200
* evaluates to a non-null value.
201201
* * [reactionFn]: The reaction function to execute when a change is detected in the watched
202202
* expression.
203-
* * [context]: The object against which the expression is evaluated. This defaults to the
204-
* [Scope.context] if no context is specified.
205203
* * [formatters]: If the watched expression contains formatters,
206204
* this map specifies the set of formatters that are used by the expression.
207205
* * [canChangeModel]: Specifies whether the [reactionFn] changes the model. Reaction
@@ -218,6 +216,27 @@ class Scope {
218216
assert(expression is String);
219217
assert(canChangeModel is bool);
220218

219+
// TODO(deboer): Temporary shim until all uses are fixed.
220+
if (context != null) {
221+
assert(() {
222+
try {
223+
throw [];
224+
} catch (e, s) {
225+
var msg = "WARNING: The Scope.watch's context parameter "
226+
"is deprecated.\nScope.watch was called from:\n$s";
227+
_oneTimeWarnings.putIfAbsent(msg, () => print(msg));
228+
}
229+
return true;
230+
});
231+
232+
// Create a child scope instead.
233+
return createChild(context)
234+
.watch(expression, reactionFn,
235+
formatters: formatters,
236+
canChangeModel: canChangeModel,
237+
collection: collection);
238+
}
239+
221240
Watch watch;
222241
ReactionFn fn = reactionFn;
223242
if (expression.isEmpty) {
@@ -239,12 +258,13 @@ class Scope {
239258
}
240259
}
241260

242-
AST ast = rootScope._astParser(expression, context: context,
261+
AST ast = rootScope._astParser(expression,
243262
formatters: formatters, collection: collection);
244263

245264
WatchGroup group = canChangeModel ? _readWriteGroup : _readOnlyGroup;
246265
return watch = group.watch(ast, fn);
247266
}
267+
static Map _oneTimeWarnings = {};
248268

249269
dynamic eval(expression, [Map locals]) {
250270
assert(isAttached);
@@ -1052,14 +1072,10 @@ class _AstParser {
10521072
: _visitor = new ExpressionVisitor(closureMap);
10531073

10541074
AST call(String input, {FormatterMap formatters,
1055-
bool collection: false,
1056-
Object context: null }) {
1075+
bool collection: false }) {
10571076
_visitor.formatters = formatters;
10581077
AST contextRef = _visitor.contextRef;
10591078
try {
1060-
if (context != null) {
1061-
_visitor.contextRef = new ConstantAST(context, '#${_id++}');
1062-
}
10631079
var exp = _parser(input);
10641080
return collection ? _visitor.visitCollection(exp) : _visitor.visit(exp);
10651081
} finally {

test/core/scope_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ void main() {
10211021
});
10221022

10231023

1024-
it('should watch/observe on objects other then contex', (RootScope rootScope) {
1024+
it('should watch/observe on objects other then context (DEPRECATED)', (RootScope rootScope) {
10251025
var log = '';
10261026
var map = {'a': 'A', 'b': 'B'};
10271027
rootScope.watch('a', (a, b) => log += a, context: map);

0 commit comments

Comments
 (0)