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

Commit

Permalink
fix(VmTurnZone): onScheduleMicrotask behaves correctly with orphaned
Browse files Browse the repository at this point in the history
scheduleMicrotasks.

BREAKING CHANGE:

Zone.defaultOnScheduleMicrotask is now named Zone.onScheduleMircotask
  • Loading branch information
jbdeboer committed May 15, 2014
1 parent c9776b4 commit a8699da
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
16 changes: 9 additions & 7 deletions lib/core/zone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class VmTurnZone {
/// an "inner" [Zone], which is a child of the outer [Zone].
async.Zone _innerZone;

ZoneScheduleMicrotask defaultOnScheduleMicrotask;

/**
* Associates with this
*
Expand All @@ -82,6 +80,7 @@ class VmTurnZone {
onError = _defaultOnError;
onTurnDone = _defaultOnTurnDone;
onTurnStart = _defaultOnTurnStart;
onScheduleMicrotask = _defaultOnScheduleMicrotask;
}

List _asyncQueue = [];
Expand Down Expand Up @@ -115,11 +114,7 @@ class VmTurnZone {

_onScheduleMicrotask(async.Zone self, async.ZoneDelegate delegate,
async.Zone zone, fn()) {
if (defaultOnScheduleMicrotask != null) {
return defaultOnScheduleMicrotask(fn);
}

_asyncQueue.add(() => delegate.run(zone, fn));
onScheduleMicrotask(() => delegate.run(zone, fn));
if (_runningInTurn == 0 && !_inFinishTurn) _finishTurn(zone, delegate);
}

Expand Down Expand Up @@ -197,6 +192,13 @@ class VmTurnZone {
ZoneOnTurnDone onTurnDone;
void _defaultOnTurnDone() => null;

/**
* Called any time a microtask is scheduled. If you override [onScheduleMicrotask], you
* are expected to call the function at some point.
*/
ZoneScheduleMicrotask onScheduleMicrotask;
void _defaultOnScheduleMicrotask(fn) => _asyncQueue.add(fn);

LongStackTrace _longStacktrace = null;

LongStackTrace _getLongStacktrace(name) {
Expand Down
38 changes: 34 additions & 4 deletions test/core/zone_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void main() {
});
log('onTurnDone(end)');
};
zone.defaultOnScheduleMicrotask = (microTaskFn) {
zone.onScheduleMicrotask = (microTaskFn) {
log('onScheduleMicrotask(begin)');
microtaskResult = microTaskFn();
log('onScheduleMicrotask(end)');
Expand All @@ -478,7 +478,7 @@ void main() {
});
log('onTurnDone(end)');
};
zone.defaultOnScheduleMicrotask = (microTaskFn) {
zone.onScheduleMicrotask = (microTaskFn) {
log('onScheduleMicrotask(begin)');
microTaskFn();
log('onScheduleMicrotask(end)');
Expand All @@ -500,7 +500,7 @@ void main() {
zone.onTurnDone = () {
log('onTurnDone');
};
zone.defaultOnScheduleMicrotask = (microTaskFn) {
zone.onScheduleMicrotask = (microTaskFn) {
log('onScheduleMicrotask(begin)');
microTaskFn();
log('onScheduleMicrotask(end)');
Expand Down Expand Up @@ -528,7 +528,7 @@ void main() {
zone.onTurnDone = () {
log('onTurnDone');
};
zone.defaultOnScheduleMicrotask = (microTaskFn) {
zone.onScheduleMicrotask = (microTaskFn) {
log('onScheduleMicrotask(begin)');
microTaskFn();
log('onScheduleMicrotask(end)');
Expand All @@ -541,6 +541,36 @@ void main() {
' onScheduleMicrotask(end); run; onTurnDone');
}));

iit('should execute microtask scheduled outside the turn', (Logger log) {
zone = new VmTurnZone();

var taskToRun = null;

zone.onTurnDone = () {
if (taskToRun != null) taskToRun();
taskToRun = null;
log('onTurnDone');
};

zone.onScheduleMicrotask = (microTaskFn) {
log('onScheduleMicrotask');
taskToRun = microTaskFn;
};

var completer;
zone.run(() {
completer = new Completer();
completer.future.then((x) => log('future'));
log('first');
});
completer.complete();

expect(log).toEqual([
'first', 'onTurnDone',
'onScheduleMicrotask', 'future', 'onTurnDone'
]);
});

});
});
}

0 comments on commit a8699da

Please sign in to comment.