Skip to content

Commit 2675da0

Browse files
committed
WIP: proposal for running micro-tasks in angular scope
1 parent a3abfa0 commit 2675da0

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/core/scope.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ class RootScope extends Scope {
539539
_scopeStats)
540540
{
541541
_zone.onTurnDone = apply;
542+
_zone._onScheduleMicrotask = runAsync;
542543
_zone.onError = (e, s, ls) => _exceptionHandler(e, s);
543544
}
544545

@@ -666,11 +667,15 @@ class RootScope extends Scope {
666667

667668
// QUEUES
668669
void runAsync(fn()) {
669-
var chain = new _FunctionChain(fn);
670-
if (_runAsyncHead == null) {
671-
_runAsyncHead = _runAsyncTail = chain;
670+
if (_state == null || _state == RootScope.STATE_APPLY || _state == RootScope.STATE_DIGEST) {
671+
var chain = new _FunctionChain(fn);
672+
if (_runAsyncHead == null) {
673+
_runAsyncHead = _runAsyncTail = chain;
674+
} else {
675+
_runAsyncTail = _runAsyncTail._next = chain;
676+
}
672677
} else {
673-
_runAsyncTail = _runAsyncTail._next = chain;
678+
throw "Flush phase can not schedule microtasks.";
674679
}
675680
}
676681

test/core/zone_spec.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,23 @@ void main() {
154154
it('should call onTurnDone for a scheduleMicrotask in onTurnDone', async((Logger log) {
155155
var ran = false;
156156
zone.onTurnDone = () {
157+
log('onTurnDone(begin)');
157158
if (!ran) {
158159
scheduleMicrotask(() { ran = true; log('onTurnAsync'); });
159160
}
160-
log('onTurnDone');
161+
log('onTurnDone(end)');
162+
};
163+
zon.onSchedulMicrotask = (microTaskFn) {
164+
log('onScheduleMicrotask(begin)');
165+
microTaskFn();
166+
log('onScheduleMicrotask(end)');
161167
};
162168
zone.run(() {
163169
log('run');
164170
});
165171
microLeap();
166172

167-
expect(log.result()).toEqual('onTurnStart; run; onTurnDone; onTurnStart; onTurnAsync; onTurnDone');
173+
expect(log.result()).toEqual('onTurnStart; run; onTurnDone(begin); onScheduleMicrotask(begin); onTurnAsync; onScheduleMicrotask(begin); onTurnDone(end)');
168174
}));
169175

170176

0 commit comments

Comments
 (0)