feat(zone): add initial implementation of VmTurnZone
This commit is contained in:
@ -83,6 +83,7 @@ export function bootstrap(appComponentType: Type, bindings=null) {
|
||||
var appInjector = _rootInjector.createChild(_injectorBindings(
|
||||
appComponentType));
|
||||
if (isPresent(bindings)) appInjector = appInjector.createChild(bindings);
|
||||
|
||||
return appInjector.asyncGet(ChangeDetector).then((cd) => {
|
||||
// TODO(rado): replace with zone.
|
||||
cd.detectChanges();
|
||||
|
66
modules/core/src/zone/vm_turn_zone.dart
Normal file
66
modules/core/src/zone/vm_turn_zone.dart
Normal file
@ -0,0 +1,66 @@
|
||||
library angular.zone;
|
||||
|
||||
import 'dart:async' as async;
|
||||
|
||||
class VmTurnZone {
|
||||
Function _onTurnStart;
|
||||
Function _onTurnDone;
|
||||
Function _onScheduleMicrotask;
|
||||
|
||||
async.Zone _outerZone;
|
||||
async.Zone _innerZone;
|
||||
|
||||
int _nestedRunCounter;
|
||||
|
||||
VmTurnZone() {
|
||||
_nestedRunCounter = 0;
|
||||
_outerZone = async.Zone.current;
|
||||
_innerZone = _outerZone.fork(specification: new async.ZoneSpecification(
|
||||
run: _onRun,
|
||||
runUnary: _onRunUnary,
|
||||
scheduleMicrotask: _onMicrotask
|
||||
));
|
||||
}
|
||||
|
||||
initCallbacks({Function onTurnStart, Function onTurnDone, Function onScheduleMicrotask}) {
|
||||
this._onTurnStart = onTurnStart;
|
||||
this._onTurnDone = onTurnDone;
|
||||
this._onScheduleMicrotask = onScheduleMicrotask;
|
||||
}
|
||||
|
||||
dynamic run(fn()) => _innerZone.run(fn);
|
||||
|
||||
dynamic runOutsideAngular(fn()) => _outerZone.run(fn);
|
||||
|
||||
|
||||
dynamic _onRunBase(async.Zone self, async.ZoneDelegate delegate, async.Zone zone, fn()) {
|
||||
_nestedRunCounter++;
|
||||
try {
|
||||
if (_nestedRunCounter == 1 && _onTurnStart != null) delegate.run(zone, _onTurnStart);
|
||||
|
||||
return fn();
|
||||
|
||||
} finally {
|
||||
_nestedRunCounter--;
|
||||
if (_nestedRunCounter == 0 && _onTurnDone != null) _finishTurn(zone, delegate);
|
||||
}
|
||||
}
|
||||
|
||||
dynamic _onRun(async.Zone self, async.ZoneDelegate delegate, async.Zone zone, fn()) =>
|
||||
_onRunBase(self, delegate, zone, () => delegate.run(zone, fn));
|
||||
|
||||
dynamic _onRunUnary(async.Zone self, async.ZoneDelegate delegate, async.Zone zone, fn(args), args) =>
|
||||
_onRunBase(self, delegate, zone, () => delegate.runUnary(zone, fn, args));
|
||||
|
||||
void _finishTurn(zone, delegate) {
|
||||
delegate.run(zone, _onTurnDone);
|
||||
}
|
||||
|
||||
_onMicrotask(async.Zone self, async.ZoneDelegate delegate, async.Zone zone, fn) {
|
||||
if (this._onScheduleMicrotask != null) {
|
||||
this._onScheduleMicrotask(fn);
|
||||
} else {
|
||||
delegate.scheduleMicrotask(zone, fn);
|
||||
}
|
||||
}
|
||||
}
|
52
modules/core/src/zone/vm_turn_zone.es6
Normal file
52
modules/core/src/zone/vm_turn_zone.es6
Normal file
@ -0,0 +1,52 @@
|
||||
import {List, ListWrapper} from 'facade/collection';
|
||||
import {normalizeBlank} from 'facade/lang';
|
||||
|
||||
export class VmTurnZone {
|
||||
_outerZone;
|
||||
_innerZone;
|
||||
|
||||
_onTurnStart:Function;
|
||||
_onTurnDone:Function;
|
||||
|
||||
_nestedRunCounter:number;
|
||||
|
||||
constructor() {
|
||||
this._nestedRunCounter = 0;
|
||||
this._onTurnStart = null;
|
||||
this._onTurnDone = null;
|
||||
|
||||
this._outerZone = window.zone;
|
||||
this._innerZone = this._outerZone.fork({
|
||||
beforeTask: () => this._beforeTask(),
|
||||
afterTask: () => this._afterTask()
|
||||
});
|
||||
}
|
||||
|
||||
initCallbacks({onTurnStart, onTurnDone, onScheduleMicrotask} = {}) {
|
||||
this._onTurnStart = normalizeBlank(onTurnStart);
|
||||
this._onTurnDone = normalizeBlank(onTurnDone);
|
||||
}
|
||||
|
||||
run(fn) {
|
||||
return this._innerZone.run(fn);
|
||||
}
|
||||
|
||||
runOutsideAngular(fn) {
|
||||
return this._outerZone.run(fn);
|
||||
}
|
||||
|
||||
|
||||
_beforeTask(){
|
||||
this._nestedRunCounter ++;
|
||||
if(this._nestedRunCounter === 1 && this._onTurnStart) {
|
||||
this._onTurnStart();
|
||||
}
|
||||
}
|
||||
|
||||
_afterTask(){
|
||||
this._nestedRunCounter --;
|
||||
if(this._nestedRunCounter === 0 && this._onTurnDone) {
|
||||
this._onTurnDone();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user