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

Commit

Permalink
fix(scope): allow watching an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and mhevery committed Mar 25, 2014
1 parent 8944f0d commit bd0d4ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 15 additions & 11 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,21 @@ class Scope {
assert(expression is String);
Watch watch;
ReactionFn fn = reactionFn;
if (expression.startsWith('::')) {
expression = expression.substring(2);
fn = (value, last) {
if (value != null) {
watch.remove();
return reactionFn(value, last);
}
};
} else if (expression.startsWith(':')) {
expression = expression.substring(1);
fn = (value, last) => value == null ? null : reactionFn(value, last);
if (expression.isEmpty) {
expression = '""';
} else {
if (expression.startsWith('::')) {
expression = expression.substring(2);
fn = (value, last) {
if (value != null) {
watch.remove();
return reactionFn(value, last);
}
};
} else if (expression.startsWith(':')) {
expression = expression.substring(1);
fn = (value, last) => value == null ? null : reactionFn(value, last);
}
}

AST ast = rootScope._astParser(expression, context: context,
Expand Down
4 changes: 3 additions & 1 deletion test/core/scope_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ void main() {
it('should watch literals', (Logger logger, Map context, RootScope rootScope) {
context['a'] = 1;
rootScope
..watch('', (value, previous) => logger(value))
..watch('""', (value, previous) => logger(value))
..watch('1', (value, previous) => logger(value))
..watch('"str"', (value, previous) => logger(value))
..watch('[a, 2, 3]', (value, previous) => logger(value))
..watch('{a:a, b:2}', (value, previous) => logger(value))
..digest();
expect(logger).toEqual([1, 'str', [1, 2, 3], {'a': 1, 'b': 2}]);
expect(logger).toEqual(['', '', 1, 'str', [1, 2, 3], {'a': 1, 'b': 2}]);
logger.clear();
context['a'] = 3;
rootScope.digest();
Expand Down

0 comments on commit bd0d4ff

Please sign in to comment.