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

Commit

Permalink
feat(scope): implement scope.$disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Nov 25, 2013
1 parent c6e38af commit 7e6e32d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Scope implements Map {
List<Function> _outerAsyncQueue;
Scope _nextSibling, _prevSibling, _childHead, _childTail;
bool _skipAutoDigest = false;
bool _dirty = true;
bool _disabled = false;

Scope(ExceptionHandler this._exceptionHandler, Parser this._parser,
ScopeDigestTTL ttl, NgZone this._zone, Profiler this._perf):
Expand Down Expand Up @@ -157,6 +157,13 @@ class Scope implements Map {
return new Scope._child(this, isolate, lazy, _perf);
}

/**
* *EXPERIMENTAL:* This feature is experimental. We reserve the right to change or delete it.
*
* A dissabled scope will not be part of the [$digest] cycle until it is re-enabled.
*/
set $disabled(value) => this._disabled = value;
get $disabled => this._disabled;

$watch(watchExp, [Function listener, String watchStr]) {
if (watchStr == null) {
Expand Down Expand Up @@ -299,13 +306,15 @@ class Scope implements Map {
}

/**
* *EXPERIMENTAL:* This feature is experimental. We reserve the right to change or delete it.
*
* Marks a scope as dirty. If the scope is lazy (see [$new]) then the scope will be included
* in the next [$digest].
*
* NOTE: This has no effect for non-lazy scopes.
*/
$dirty() {
this._dirty = true;
this._disabled = false;
}

$digest() {
Expand Down Expand Up @@ -372,7 +381,7 @@ class Scope implements Map {
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $broadcast
var childHead = current._childHead;
while (childHead != null && !childHead._dirty) {
while (childHead != null && childHead._disabled) {
childHead = childHead._nextSibling;
}
if (childHead == null) {
Expand All @@ -387,7 +396,7 @@ class Scope implements Map {
}
}
} else {
if (childHead._lazy) childHead._dirty = false;
if (childHead._lazy) childHead._disabled = true;
next = childHead;
}
} while ((current = next) != null);
Expand Down
27 changes: 27 additions & 0 deletions test/core/scope_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,33 @@ main() {
expect(log).toEqual('lazy;eager;eager;eager;lazy;eager;');
});
});

describe('disabled digest', () {
var rootScope, childScope;

beforeEach(inject((Scope root) {
rootScope = root;
childScope = root.$new();
}));

it('should disable digest', () {
var log = '';
childScope.$watch(() {log += 'digest;';});

rootScope.$digest();
expect(log).toEqual('digest;digest;');

childScope.$disabled = true;
expect(childScope.$disabled).toEqual(true);
rootScope.$digest();
expect(log).toEqual('digest;digest;');

childScope.$disabled = false;
expect(childScope.$disabled).toEqual(false);
rootScope.$digest();
expect(log).toEqual('digest;digest;digest;');
});
});
});


Expand Down

0 comments on commit 7e6e32d

Please sign in to comment.