chore: upgrade to new Zone.js API v0.6.2
BREAKING CHANGE Removed deprecated API from NgZone - `NgZone.overrideOnTurnStart` - `NgZone.overrideOnTurnDone` - `NgZone.overrideOnEventDone` - `NgZone.overrideOnErrorHandler` Rename NgZone API - `NgZone.onTurnStart` => `NgZone.onUnstable` - `NgZone.onTurnDone` => `NgZone.onMicrotaskEmpty` - `NgZone.onEventDone` => `NgZone.onStable` Closes #7345
This commit is contained in:

committed by
Miško Hevery

parent
f9fb72fb0e
commit
310620fd12
@ -1,4 +1,4 @@
|
||||
import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
||||
import {NgZone, NgZoneError} from 'angular2/src/core/zone/ng_zone';
|
||||
import {
|
||||
Type,
|
||||
isBlank,
|
||||
@ -254,7 +254,9 @@ export class PlatformRef_ extends PlatformRef {
|
||||
try {
|
||||
injector = this.injector.resolveAndCreateChild(providers);
|
||||
exceptionHandler = injector.get(ExceptionHandler);
|
||||
zone.overrideOnErrorHandler((e, s) => exceptionHandler.call(e, s));
|
||||
ObservableWrapper.subscribe(zone.onError, (error: NgZoneError) => {
|
||||
exceptionHandler.call(error.error, error.stackTrace);
|
||||
});
|
||||
} catch (e) {
|
||||
if (isPresent(exceptionHandler)) {
|
||||
exceptionHandler.call(e, e.stack);
|
||||
@ -394,7 +396,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
constructor(private _platform: PlatformRef_, private _zone: NgZone, private _injector: Injector) {
|
||||
super();
|
||||
if (isPresent(this._zone)) {
|
||||
ObservableWrapper.subscribe(this._zone.onTurnDone,
|
||||
ObservableWrapper.subscribe(this._zone.onMicrotaskEmpty,
|
||||
(_) => { this._zone.run(() => { this.tick(); }); });
|
||||
}
|
||||
this._enforceNoNewChanges = assertionsEnabled();
|
||||
@ -434,16 +436,10 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
|
||||
var tickResult = PromiseWrapper.then(compRefToken, tick);
|
||||
|
||||
// THIS MUST ONLY RUN IN DART.
|
||||
// This is required to report an error when no components with a matching selector found.
|
||||
// Otherwise the promise will never be completed.
|
||||
// Doing this in JS causes an extra error message to appear.
|
||||
if (IS_DART) {
|
||||
PromiseWrapper.then(tickResult, (_) => {});
|
||||
}
|
||||
|
||||
PromiseWrapper.then(tickResult, null,
|
||||
(err, stackTrace) => completer.reject(err, stackTrace));
|
||||
PromiseWrapper.then(tickResult, null, (err, stackTrace) => {
|
||||
completer.reject(err, stackTrace);
|
||||
exceptionHandler.call(err, stackTrace);
|
||||
});
|
||||
} catch (e) {
|
||||
exceptionHandler.call(e, e.stack);
|
||||
completer.reject(e, e.stack);
|
||||
|
@ -1,9 +1,9 @@
|
||||
import {Injectable} from 'angular2/src/core/di';
|
||||
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {CONST, CONST_EXPR} from 'angular2/src/facade/lang';
|
||||
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
||||
import {CONST, CONST_EXPR, scheduleMicroTask} from 'angular2/src/facade/lang';
|
||||
import {BaseException} from 'angular2/src/facade/exceptions';
|
||||
import {NgZone} from '../zone/ng_zone';
|
||||
import {PromiseWrapper, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
import {ObservableWrapper} from 'angular2/src/facade/async';
|
||||
|
||||
|
||||
/**
|
||||
@ -15,6 +15,7 @@ import {PromiseWrapper, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
export class Testability {
|
||||
/** @internal */
|
||||
_pendingCount: number = 0;
|
||||
_isZoneStable: boolean = true;
|
||||
/**
|
||||
* Whether any work was done since the last 'whenStable' callback. This is
|
||||
* useful to detect if this could have potentially destabilized another
|
||||
@ -24,23 +25,22 @@ export class Testability {
|
||||
_didWork: boolean = false;
|
||||
/** @internal */
|
||||
_callbacks: Function[] = [];
|
||||
/** @internal */
|
||||
_isAngularEventPending: boolean = false;
|
||||
constructor(_ngZone: NgZone) { this._watchAngularEvents(_ngZone); }
|
||||
constructor(private _ngZone: NgZone) { this._watchAngularEvents(); }
|
||||
|
||||
/** @internal */
|
||||
_watchAngularEvents(_ngZone: NgZone): void {
|
||||
ObservableWrapper.subscribe(_ngZone.onTurnStart, (_) => {
|
||||
_watchAngularEvents(): void {
|
||||
ObservableWrapper.subscribe(this._ngZone.onUnstable, (_) => {
|
||||
this._didWork = true;
|
||||
this._isAngularEventPending = true;
|
||||
this._isZoneStable = false;
|
||||
});
|
||||
|
||||
_ngZone.runOutsideAngular(() => {
|
||||
ObservableWrapper.subscribe(_ngZone.onEventDone, (_) => {
|
||||
if (!_ngZone.hasPendingTimers) {
|
||||
this._isAngularEventPending = false;
|
||||
this._ngZone.runOutsideAngular(() => {
|
||||
ObservableWrapper.subscribe(this._ngZone.onStable, (_) => {
|
||||
NgZone.assertNotInAngularZone();
|
||||
scheduleMicroTask(() => {
|
||||
this._isZoneStable = true;
|
||||
this._runCallbacksIfReady();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -60,22 +60,24 @@ export class Testability {
|
||||
return this._pendingCount;
|
||||
}
|
||||
|
||||
isStable(): boolean { return this._pendingCount == 0 && !this._isAngularEventPending; }
|
||||
isStable(): boolean {
|
||||
return this._isZoneStable && this._pendingCount == 0 && !this._ngZone.hasPendingMacrotasks;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_runCallbacksIfReady(): void {
|
||||
if (!this.isStable()) {
|
||||
if (this.isStable()) {
|
||||
// Schedules the call backs in a new frame so that it is always async.
|
||||
scheduleMicroTask(() => {
|
||||
while (this._callbacks.length !== 0) {
|
||||
(this._callbacks.pop())(this._didWork);
|
||||
}
|
||||
this._didWork = false;
|
||||
});
|
||||
} else {
|
||||
// Not Ready
|
||||
this._didWork = true;
|
||||
return; // Not ready
|
||||
}
|
||||
|
||||
// Schedules the call backs in a new frame so that it is always async.
|
||||
PromiseWrapper.resolve(null).then((_) => {
|
||||
while (this._callbacks.length !== 0) {
|
||||
(this._callbacks.pop())(this._didWork);
|
||||
}
|
||||
this._didWork = false;
|
||||
});
|
||||
}
|
||||
|
||||
whenStable(callback: Function): void {
|
||||
@ -85,10 +87,6 @@ export class Testability {
|
||||
|
||||
getPendingRequestCount(): number { return this._pendingCount; }
|
||||
|
||||
// This only accounts for ngZone, and not pending counts. Use `whenStable` to
|
||||
// check for stability.
|
||||
isAngularEventPending(): boolean { return this._isAngularEventPending; }
|
||||
|
||||
findBindings(using: any, provider: string, exactMatch: boolean): any[] {
|
||||
// TODO(juliemr): implement.
|
||||
return [];
|
||||
|
@ -1,2 +1,2 @@
|
||||
// Public API for Zone
|
||||
export {NgZone, ZeroArgFunction, ErrorHandlingFn, NgZoneError} from './zone/ng_zone';
|
||||
export {NgZone, NgZoneError} from './zone/ng_zone';
|
||||
|
@ -1,394 +0,0 @@
|
||||
library angular.zone;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:stack_trace/stack_trace.dart' show Chain;
|
||||
|
||||
typedef void ZeroArgFunction();
|
||||
typedef void ErrorHandlingFn(error, stackTrace);
|
||||
|
||||
/**
|
||||
* A `Timer` wrapper that lets you specify additional functions to call when it
|
||||
* is cancelled.
|
||||
*/
|
||||
class WrappedTimer implements Timer {
|
||||
Timer _timer;
|
||||
ZeroArgFunction _onCancelCb;
|
||||
|
||||
WrappedTimer(Timer timer) {
|
||||
_timer = timer;
|
||||
}
|
||||
|
||||
void addOnCancelCb(ZeroArgFunction onCancelCb) {
|
||||
if (this._onCancelCb != null) {
|
||||
throw "On cancel cb already registered";
|
||||
}
|
||||
this._onCancelCb = onCancelCb;
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
if (this._onCancelCb != null) {
|
||||
this._onCancelCb();
|
||||
}
|
||||
_timer.cancel();
|
||||
}
|
||||
|
||||
bool get isActive => _timer.isActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores error information; delivered via [NgZone.onError] stream.
|
||||
*/
|
||||
class NgZoneError {
|
||||
/// Error object thrown.
|
||||
final error;
|
||||
/// Either long or short chain of stack traces.
|
||||
final List stackTrace;
|
||||
NgZoneError(this.error, this.stackTrace);
|
||||
}
|
||||
|
||||
/**
|
||||
* A `Zone` wrapper that lets you schedule tasks after its private microtask queue is exhausted but
|
||||
* before the next "VM turn", i.e. event loop iteration.
|
||||
*
|
||||
* This lets you freely schedule microtasks that prepare data, and set an {@link onTurnDone} handler that
|
||||
* will consume that data after it's ready but before the browser has a chance to re-render.
|
||||
*
|
||||
* A VM turn consist of a single macrotask followed 0 to many microtasks.
|
||||
*
|
||||
* The wrapper maintains an "inner" and "mount" `Zone`. The application code will executes
|
||||
* in the "inner" zone unless `runOutsideAngular` is explicitely called.
|
||||
*
|
||||
* A typical application will create a singleton `NgZone`. The mount zone is the `Zone` where the singleton has been
|
||||
* instantiated. The default `onTurnDone` runs the Angular change detection.
|
||||
*/
|
||||
class NgZone {
|
||||
ZeroArgFunction _onTurnStart;
|
||||
ZeroArgFunction _onTurnDone;
|
||||
ZeroArgFunction _onEventDone;
|
||||
ErrorHandlingFn _onErrorHandler;
|
||||
|
||||
final _onTurnStartCtrl = new StreamController.broadcast(sync: true);
|
||||
final _onTurnDoneCtrl = new StreamController.broadcast(sync: true);
|
||||
final _onEventDoneCtrl = new StreamController.broadcast(sync: true);
|
||||
final _onErrorCtrl =
|
||||
new StreamController<NgZoneError>.broadcast(sync: true);
|
||||
|
||||
// Code executed in _mountZone does not trigger the onTurnDone.
|
||||
Zone _mountZone;
|
||||
// _innerZone is the child of _mountZone. Any code executed in this zone will trigger the
|
||||
// onTurnDone hook at the end of the current VM turn.
|
||||
Zone _innerZone;
|
||||
|
||||
// Number of microtasks pending from _innerZone (& descendants)
|
||||
int _pendingMicrotasks = 0;
|
||||
// Whether some code has been executed in the _innerZone (& descendants) in the current turn
|
||||
bool _hasExecutedCodeInInnerZone = false;
|
||||
// _outerRun() call depth. 0 at the end of a macrotask
|
||||
// zone.run(() => { // top-level call
|
||||
// zone.run(() => {}); // nested call -> in-turn
|
||||
// }); // we should only check for the end of a turn once the top-level run ends
|
||||
int _nestedRun = 0;
|
||||
|
||||
bool _inVmTurnDone = false;
|
||||
|
||||
List<Timer> _pendingTimers = [];
|
||||
|
||||
/**
|
||||
* Associates with this
|
||||
*
|
||||
* - a "mount" [Zone], which is a the one that instantiated this.
|
||||
* - an "inner" [Zone], which is a child of the mount [Zone].
|
||||
*
|
||||
* @param {bool} enableLongStackTrace whether to enable long stack trace. They should only be
|
||||
* enabled in development mode as they significantly impact perf.
|
||||
*/
|
||||
NgZone({bool enableLongStackTrace}) {
|
||||
_mountZone = Zone.current;
|
||||
|
||||
if (enableLongStackTrace) {
|
||||
_innerZone = Chain.capture(() => _createInnerZone(Zone.current),
|
||||
onError: _onErrorWithLongStackTrace);
|
||||
} else {
|
||||
_innerZone = _createInnerZone(Zone.current,
|
||||
handleUncaughtError: (Zone self, ZoneDelegate parent, Zone zone,
|
||||
error, StackTrace trace) =>
|
||||
_onErrorWithoutLongStackTrace(error, trace));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called just before Angular event turn starts.
|
||||
* It is called once per browser event.
|
||||
*/
|
||||
@Deprecated('Use onTurnStart Stream instead')
|
||||
void overrideOnTurnStart(ZeroArgFunction onTurnStartFn) {
|
||||
_onTurnStart = onTurnStartFn;
|
||||
}
|
||||
|
||||
void _notifyOnTurnStart() {
|
||||
this._onTurnStartCtrl.add(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies subscribers just before Angular event turn starts.
|
||||
*
|
||||
* Emits an event once per browser task that is handled by Angular.
|
||||
*/
|
||||
Stream get onTurnStart => _onTurnStartCtrl.stream;
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called immediately after Angular processes
|
||||
* all pending microtasks.
|
||||
*/
|
||||
@Deprecated('Use onTurnDone Stream instead')
|
||||
void overrideOnTurnDone(ZeroArgFunction onTurnDoneFn) {
|
||||
_onTurnDone = onTurnDoneFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies subscribers immediately after the Angular zone is done processing
|
||||
* the current turn and any microtasks scheduled from that turn.
|
||||
*
|
||||
* Used by Angular as a signal to kick off change-detection.
|
||||
*/
|
||||
Stream get onTurnDone => _onTurnDoneCtrl.stream;
|
||||
|
||||
void _notifyOnTurnDone() {
|
||||
this._onTurnDoneCtrl.add(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called immediately after the last turn in
|
||||
* an event completes. At this point Angular will no longer attempt to
|
||||
* sync the UI. Any changes to the data model will not be reflected in the
|
||||
* DOM. `onEventDoneFn` is executed outside Angular zone.
|
||||
*
|
||||
* This hook is useful for validating application state (e.g. in a test).
|
||||
*/
|
||||
@Deprecated('Use onEventDone Stream instead')
|
||||
void overrideOnEventDone(ZeroArgFunction onEventDoneFn,
|
||||
[bool waitForAsync = false]) {
|
||||
_onEventDone = onEventDoneFn;
|
||||
|
||||
if (waitForAsync) {
|
||||
_onEventDone = () {
|
||||
if (_pendingTimers.length == 0) {
|
||||
onEventDoneFn();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies subscribers immediately after the final `onTurnDone` callback
|
||||
* before ending VM event.
|
||||
*
|
||||
* This event is useful for validating application state (e.g. in a test).
|
||||
*/
|
||||
Stream get onEventDone => _onEventDoneCtrl.stream;
|
||||
|
||||
void _notifyOnEventDone() {
|
||||
this._onEventDoneCtrl.add(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding microtasks.
|
||||
*/
|
||||
bool get hasPendingMicrotasks => _pendingMicrotasks > 0;
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding timers.
|
||||
*/
|
||||
bool get hasPendingTimers => _pendingTimers.isNotEmpty;
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding asychnronous tasks of any kind that are
|
||||
* scheduled to run within Angular zone.
|
||||
*
|
||||
* Useful as a signal of UI stability. For example, when a test reaches a
|
||||
* point when [hasPendingAsyncTasks] is `false` it might be a good time to run
|
||||
* test expectations.
|
||||
*/
|
||||
bool get hasPendingAsyncTasks => hasPendingMicrotasks || hasPendingTimers;
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called when an error is uncaught in the
|
||||
* Angular zone. The first argument is the error. The second argument is
|
||||
* the stack trace.
|
||||
*/
|
||||
@Deprecated('Use onError Stream instead')
|
||||
void overrideOnErrorHandler(ErrorHandlingFn errorHandlingFn) {
|
||||
_onErrorHandler = errorHandlingFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies subscribers whenever an error happens within the zone.
|
||||
*
|
||||
* Useful for logging.
|
||||
*/
|
||||
Stream get onError => _onErrorCtrl.stream;
|
||||
|
||||
/**
|
||||
* Runs `fn` in the inner zone and returns whatever it returns.
|
||||
*
|
||||
* In a typical app where the inner zone is the Angular zone, this allows one to make use of the
|
||||
* Angular's auto digest mechanism.
|
||||
*
|
||||
* ```
|
||||
* NgZone zone = <ref to the application zone>;
|
||||
*
|
||||
* void functionCalledFromJS() {
|
||||
* zone.run(() {
|
||||
* // auto-digest will run after this function is called from JS
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
dynamic run(fn()) {
|
||||
// Using runGuarded() is required when executing sync code with Dart otherwise handleUncaughtError()
|
||||
// would not be called on exceptions.
|
||||
// see https://code.google.com/p/dart/issues/detail?id=19566 for details.
|
||||
return _innerZone.runGuarded(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs `fn` in the mount zone and returns whatever it returns.
|
||||
*
|
||||
* In a typical app where the inner zone is the Angular zone, this allows one to escape Angular's
|
||||
* auto-digest mechanism.
|
||||
*
|
||||
* ```
|
||||
* void myFunction(NgZone zone, Element element) {
|
||||
* element.onClick.listen(() {
|
||||
* // auto-digest will run after element click.
|
||||
* });
|
||||
* zone.runOutsideAngular(() {
|
||||
* element.onMouseMove.listen(() {
|
||||
* // auto-digest will NOT run after mouse move
|
||||
* });
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
dynamic runOutsideAngular(fn()) {
|
||||
return _mountZone.run(fn);
|
||||
}
|
||||
|
||||
void _maybeStartVmTurn(ZoneDelegate parent) {
|
||||
if (!_hasExecutedCodeInInnerZone) {
|
||||
_hasExecutedCodeInInnerZone = true;
|
||||
parent.run(_innerZone, _notifyOnTurnStart);
|
||||
if (_onTurnStart != null) {
|
||||
parent.run(_innerZone, _onTurnStart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dynamic _run(Zone self, ZoneDelegate parent, Zone zone, fn()) {
|
||||
try {
|
||||
_nestedRun++;
|
||||
_maybeStartVmTurn(parent);
|
||||
return parent.run(zone, fn);
|
||||
} finally {
|
||||
_nestedRun--;
|
||||
// If there are no more pending microtasks and we are not in a recursive call, this is the end of a turn
|
||||
if (_pendingMicrotasks == 0 && _nestedRun == 0 && !_inVmTurnDone) {
|
||||
if (_hasExecutedCodeInInnerZone) {
|
||||
// Trigger onTurnDone at the end of a turn if _innerZone has executed some code
|
||||
try {
|
||||
_inVmTurnDone = true;
|
||||
_notifyOnTurnDone();
|
||||
if (_onTurnDone != null) {
|
||||
parent.run(_innerZone, _onTurnDone);
|
||||
}
|
||||
} finally {
|
||||
_inVmTurnDone = false;
|
||||
_hasExecutedCodeInInnerZone = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (_pendingMicrotasks == 0) {
|
||||
_notifyOnEventDone();
|
||||
if (_onEventDone != null) {
|
||||
runOutsideAngular(_onEventDone);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dynamic _runUnary(Zone self, ZoneDelegate parent, Zone zone, fn(arg), arg) =>
|
||||
_run(self, parent, zone, () => fn(arg));
|
||||
|
||||
dynamic _runBinary(Zone self, ZoneDelegate parent, Zone zone, fn(arg1, arg2),
|
||||
arg1, arg2) =>
|
||||
_run(self, parent, zone, () => fn(arg1, arg2));
|
||||
|
||||
void _scheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, fn) {
|
||||
_pendingMicrotasks++;
|
||||
var microtask = () {
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
_pendingMicrotasks--;
|
||||
}
|
||||
};
|
||||
parent.scheduleMicrotask(zone, microtask);
|
||||
}
|
||||
|
||||
// Called by Chain.capture() on errors when long stack traces are enabled
|
||||
void _onErrorWithLongStackTrace(error, Chain chain) {
|
||||
if (_onErrorHandler != null || _onErrorCtrl.hasListener) {
|
||||
final traces = chain.terse.traces.map((t) => t.toString()).toList();
|
||||
if (_onErrorCtrl.hasListener) {
|
||||
_onErrorCtrl.add(new NgZoneError(error, traces));
|
||||
}
|
||||
if (_onErrorHandler != null) {
|
||||
_onErrorHandler(error, traces);
|
||||
}
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Outer zone handleUnchaughtError when long stack traces are not used
|
||||
void _onErrorWithoutLongStackTrace(error, StackTrace trace) {
|
||||
if (_onErrorHandler != null || _onErrorCtrl.hasListener) {
|
||||
if (_onErrorHandler != null) {
|
||||
_onErrorHandler(error, [trace.toString()]);
|
||||
}
|
||||
if (_onErrorCtrl.hasListener) {
|
||||
_onErrorCtrl.add(new NgZoneError(error, [trace.toString()]));
|
||||
}
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Timer _createTimer(
|
||||
Zone self, ZoneDelegate parent, Zone zone, Duration duration, fn()) {
|
||||
WrappedTimer wrappedTimer;
|
||||
var cb = () {
|
||||
fn();
|
||||
_pendingTimers.remove(wrappedTimer);
|
||||
};
|
||||
Timer timer = parent.createTimer(zone, duration, cb);
|
||||
wrappedTimer = new WrappedTimer(timer);
|
||||
wrappedTimer.addOnCancelCb(() => _pendingTimers.remove(wrappedTimer));
|
||||
|
||||
_pendingTimers.add(wrappedTimer);
|
||||
return wrappedTimer;
|
||||
}
|
||||
|
||||
Zone _createInnerZone(Zone zone, {handleUncaughtError}) {
|
||||
return zone.fork(
|
||||
specification: new ZoneSpecification(
|
||||
scheduleMicrotask: _scheduleMicrotask,
|
||||
run: _run,
|
||||
runUnary: _runUnary,
|
||||
runBinary: _runBinary,
|
||||
handleUncaughtError: handleUncaughtError,
|
||||
createTimer: _createTimer),
|
||||
zoneValues: {'_innerZone': true});
|
||||
}
|
||||
}
|
@ -1,29 +1,8 @@
|
||||
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {normalizeBlank, isPresent, global, ZoneLike} from 'angular2/src/facade/lang';
|
||||
import {ObservableWrapper, EventEmitter} from 'angular2/src/facade/async';
|
||||
import {wtfLeave, wtfCreateScope, WtfScopeFn} from '../profile/profile';
|
||||
import {EventEmitter} from 'angular2/src/facade/async';
|
||||
import {NgZoneImpl, NgZoneError} from './ng_zone_impl';
|
||||
import {BaseException} from '../../facade/exceptions';
|
||||
export {NgZoneError} from './ng_zone_impl';
|
||||
|
||||
export interface NgZoneZone extends ZoneLike {
|
||||
/** @internal */
|
||||
_innerZone: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for a function with zero arguments.
|
||||
*/
|
||||
export interface ZeroArgFunction { (): void; }
|
||||
|
||||
/**
|
||||
* Function type for an error handler, which takes an error and a stack trace.
|
||||
*/
|
||||
export interface ErrorHandlingFn { (error: any, stackTrace: any): void; }
|
||||
|
||||
/**
|
||||
* Stores error information; delivered via [NgZone.onError] stream.
|
||||
*/
|
||||
export class NgZoneError {
|
||||
constructor(public error: any, public stackTrace: any) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* An injectable service for executing work inside or outside of the Angular zone.
|
||||
@ -97,206 +76,120 @@ export class NgZoneError {
|
||||
* ```
|
||||
*/
|
||||
export class NgZone {
|
||||
/** @internal */
|
||||
_runScope: WtfScopeFn = wtfCreateScope(`NgZone#run()`);
|
||||
/** @internal */
|
||||
_microtaskScope: WtfScopeFn = wtfCreateScope(`NgZone#microtask()`);
|
||||
static isInAngularZone(): boolean { return NgZoneImpl.isInAngularZone(); }
|
||||
static assertInAngularZone(): void {
|
||||
if (!NgZoneImpl.isInAngularZone()) {
|
||||
throw new BaseException('Expected to be in Angular Zone, but it is not!');
|
||||
}
|
||||
}
|
||||
static assertNotInAngularZone(): void {
|
||||
if (NgZoneImpl.isInAngularZone()) {
|
||||
throw new BaseException('Expected to not be in Angular Zone, but it is!');
|
||||
}
|
||||
}
|
||||
|
||||
// Code executed in _mountZone does not trigger the onTurnDone.
|
||||
/** @internal */
|
||||
_mountZone;
|
||||
// _innerZone is the child of _mountZone. Any code executed in this zone will trigger the
|
||||
// onTurnDone hook at the end of the current VM turn.
|
||||
/** @internal */
|
||||
_innerZone;
|
||||
private _zoneImpl: NgZoneImpl;
|
||||
|
||||
private _hasPendingMicrotasks: boolean = false;
|
||||
private _hasPendingMacrotasks: boolean = false;
|
||||
|
||||
/** @internal */
|
||||
_onTurnStart: ZeroArgFunction;
|
||||
private _isStable = true;
|
||||
/** @internal */
|
||||
_onTurnDone: ZeroArgFunction;
|
||||
private _nesting = 0;
|
||||
/** @internal */
|
||||
_onEventDone: ZeroArgFunction;
|
||||
private _onUnstable: EventEmitter<any> = new EventEmitter(false);
|
||||
/** @internal */
|
||||
_onErrorHandler: ErrorHandlingFn;
|
||||
|
||||
private _onMicrotaskEmpty: EventEmitter<any> = new EventEmitter(false);
|
||||
/** @internal */
|
||||
_onTurnStartEvents: EventEmitter<any>;
|
||||
private _onStable: EventEmitter<any> = new EventEmitter(false);
|
||||
/** @internal */
|
||||
_onTurnDoneEvents: EventEmitter<any>;
|
||||
/** @internal */
|
||||
_onEventDoneEvents: EventEmitter<any>;
|
||||
/** @internal */
|
||||
_onErrorEvents: EventEmitter<any>;
|
||||
|
||||
// Number of microtasks pending from _innerZone (& descendants)
|
||||
/** @internal */
|
||||
_pendingMicrotasks: number = 0;
|
||||
// Whether some code has been executed in the _innerZone (& descendants) in the current turn
|
||||
/** @internal */
|
||||
_hasExecutedCodeInInnerZone: boolean = false;
|
||||
// run() call depth in _mountZone. 0 at the end of a macrotask
|
||||
// zone.run(() => { // top-level call
|
||||
// zone.run(() => {}); // nested call -> in-turn
|
||||
// });
|
||||
/** @internal */
|
||||
_nestedRun: number = 0;
|
||||
|
||||
// TODO(vicb): implement this class properly for node.js environment
|
||||
// This disabled flag is only here to please cjs tests
|
||||
/** @internal */
|
||||
_disabled: boolean;
|
||||
|
||||
/** @internal */
|
||||
_inVmTurnDone: boolean = false;
|
||||
|
||||
/** @internal */
|
||||
_pendingTimeouts: number[] = [];
|
||||
private _onErrorEvents: EventEmitter<any> = new EventEmitter(false);
|
||||
|
||||
/**
|
||||
* @param {bool} enableLongStackTrace whether to enable long stack trace. They should only be
|
||||
* enabled in development mode as they significantly impact perf.
|
||||
*/
|
||||
constructor({enableLongStackTrace}) {
|
||||
if (global.zone) {
|
||||
this._disabled = false;
|
||||
this._mountZone = global.zone;
|
||||
this._innerZone = this._createInnerZone(this._mountZone, enableLongStackTrace);
|
||||
} else {
|
||||
this._disabled = true;
|
||||
this._mountZone = null;
|
||||
}
|
||||
this._onTurnStartEvents = new EventEmitter(false);
|
||||
this._onTurnDoneEvents = new EventEmitter(false);
|
||||
this._onEventDoneEvents = new EventEmitter(false);
|
||||
this._onErrorEvents = new EventEmitter(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called just before a browser task that is handled by Angular
|
||||
* executes.
|
||||
*
|
||||
* The hook is called once per browser task that is handled by Angular.
|
||||
*
|
||||
* Setting the hook overrides any previously set hook.
|
||||
*
|
||||
* @deprecated this API will be removed in the future. Use `onTurnStart` instead.
|
||||
*/
|
||||
overrideOnTurnStart(onTurnStartHook: ZeroArgFunction): void {
|
||||
this._onTurnStart = normalizeBlank(onTurnStartHook);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies subscribers just before Angular event turn starts.
|
||||
*
|
||||
* Emits an event once per browser task that is handled by Angular.
|
||||
*/
|
||||
get onTurnStart(): /* Subject */ any { return this._onTurnStartEvents; }
|
||||
|
||||
/** @internal */
|
||||
_notifyOnTurnStart(parentRun): void {
|
||||
parentRun.call(this._innerZone, () => { this._onTurnStartEvents.emit(null); });
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called immediately after Angular zone is done processing the current
|
||||
* task and any microtasks scheduled from that task.
|
||||
*
|
||||
* This is where we typically do change-detection.
|
||||
*
|
||||
* The hook is called once per browser task that is handled by Angular.
|
||||
*
|
||||
* Setting the hook overrides any previously set hook.
|
||||
*
|
||||
* @deprecated this API will be removed in the future. Use `onTurnDone` instead.
|
||||
*/
|
||||
overrideOnTurnDone(onTurnDoneHook: ZeroArgFunction): void {
|
||||
this._onTurnDone = normalizeBlank(onTurnDoneHook);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies subscribers immediately after Angular zone is done processing
|
||||
* the current turn and any microtasks scheduled from that turn.
|
||||
*
|
||||
* Used by Angular as a signal to kick off change-detection.
|
||||
*/
|
||||
get onTurnDone() { return this._onTurnDoneEvents; }
|
||||
|
||||
/** @internal */
|
||||
_notifyOnTurnDone(parentRun): void {
|
||||
parentRun.call(this._innerZone, () => { this._onTurnDoneEvents.emit(null); });
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called immediately after the `onTurnDone` callback is called and any
|
||||
* microstasks scheduled from within that callback are drained.
|
||||
*
|
||||
* `onEventDoneFn` is executed outside Angular zone, which means that we will no longer attempt to
|
||||
* sync the UI with any model changes that occur within this callback.
|
||||
*
|
||||
* This hook is useful for validating application state (e.g. in a test).
|
||||
*
|
||||
* Setting the hook overrides any previously set hook.
|
||||
*
|
||||
* @deprecated this API will be removed in the future. Use `onEventDone` instead.
|
||||
*/
|
||||
overrideOnEventDone(onEventDoneFn: ZeroArgFunction, opt_waitForAsync: boolean = false): void {
|
||||
var normalizedOnEventDone = normalizeBlank(onEventDoneFn);
|
||||
if (opt_waitForAsync) {
|
||||
this._onEventDone = () => {
|
||||
if (!this._pendingTimeouts.length) {
|
||||
normalizedOnEventDone();
|
||||
constructor({enableLongStackTrace = false}) {
|
||||
this._zoneImpl = new NgZoneImpl({
|
||||
trace: enableLongStackTrace,
|
||||
onEnter: () => {
|
||||
// console.log('ZONE.enter', this._nesting, this._isStable);
|
||||
this._nesting++;
|
||||
if (this._isStable) {
|
||||
this._isStable = false;
|
||||
this._onUnstable.emit(null);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
this._onEventDone = normalizedOnEventDone;
|
||||
}
|
||||
},
|
||||
onLeave: () => {
|
||||
this._nesting--;
|
||||
// console.log('ZONE.leave', this._nesting, this._isStable);
|
||||
this._checkStable();
|
||||
},
|
||||
setMicrotask: (hasMicrotasks: boolean) => {
|
||||
this._hasPendingMicrotasks = hasMicrotasks;
|
||||
this._checkStable();
|
||||
},
|
||||
setMacrotask: (hasMacrotasks: boolean) => { this._hasPendingMacrotasks = hasMacrotasks; },
|
||||
onError: (error: NgZoneError) => this._onErrorEvents.emit(error)
|
||||
});
|
||||
}
|
||||
|
||||
private _checkStable() {
|
||||
if (this._nesting == 0) {
|
||||
if (!this._hasPendingMicrotasks && !this._isStable) {
|
||||
try {
|
||||
// console.log('ZONE.microtaskEmpty');
|
||||
this._nesting++;
|
||||
this._onMicrotaskEmpty.emit(null);
|
||||
} finally {
|
||||
this._nesting--;
|
||||
if (!this._hasPendingMicrotasks) {
|
||||
try {
|
||||
// console.log('ZONE.stable', this._nesting, this._isStable);
|
||||
this.runOutsideAngular(() => this._onStable.emit(null));
|
||||
} finally {
|
||||
this._isStable = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Notifies subscribers immediately after the final `onTurnDone` callback
|
||||
* before ending VM event.
|
||||
*
|
||||
* This event is useful for validating application state (e.g. in a test).
|
||||
* Notifies when code enters Angular Zone. This gets fired first on VM Turn.
|
||||
*/
|
||||
get onEventDone() { return this._onEventDoneEvents; }
|
||||
get onUnstable(): EventEmitter<any> { return this._onUnstable; }
|
||||
|
||||
/** @internal */
|
||||
_notifyOnEventDone(): void {
|
||||
this.runOutsideAngular(() => { this._onEventDoneEvents.emit(null); });
|
||||
}
|
||||
/**
|
||||
* Notifies when there is no more microtasks enqueue in the current VM Turn.
|
||||
* This is a hint for Angular to do change detection, which may enqueue more microtasks.
|
||||
* For this reason this event can fire multiple times per VM Turn.
|
||||
*/
|
||||
get onMicrotaskEmpty(): EventEmitter<any> { return this._onMicrotaskEmpty; }
|
||||
|
||||
/**
|
||||
* Notifies when the last `onMicrotaskEmpty` has run and there are no more microtasks, which
|
||||
* implies we are about to relinquish VM turn.
|
||||
* This event gets called just once.
|
||||
*/
|
||||
get onStable(): EventEmitter<any> { return this._onStable; }
|
||||
|
||||
/**
|
||||
* Notify that an error has been delivered.
|
||||
*/
|
||||
get onError(): EventEmitter<any> { return this._onErrorEvents; }
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding microtasks.
|
||||
*/
|
||||
get hasPendingMicrotasks(): boolean { return this._pendingMicrotasks > 0; }
|
||||
get hasPendingMicrotasks(): boolean { return this._hasPendingMicrotasks; }
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding timers.
|
||||
* Whether there are any outstanding microtasks.
|
||||
*/
|
||||
get hasPendingTimers(): boolean { return this._pendingTimeouts.length > 0; }
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding asynchronous tasks of any kind that are
|
||||
* scheduled to run within Angular zone.
|
||||
*
|
||||
* Useful as a signal of UI stability. For example, when a test reaches a
|
||||
* point when [hasPendingAsyncTasks] is `false` it might be a good time to run
|
||||
* test expectations.
|
||||
*/
|
||||
get hasPendingAsyncTasks(): boolean { return this.hasPendingMicrotasks || this.hasPendingTimers; }
|
||||
|
||||
/**
|
||||
* Sets the zone hook that is called when an error is thrown in the Angular zone.
|
||||
*
|
||||
* Setting the hook overrides any previously set hook.
|
||||
*
|
||||
* @deprecated this API will be removed in the future. Use `onError` instead.
|
||||
*/
|
||||
overrideOnErrorHandler(errorHandler: ErrorHandlingFn) {
|
||||
this._onErrorHandler = normalizeBlank(errorHandler);
|
||||
}
|
||||
|
||||
get onError() { return this._onErrorEvents; }
|
||||
get hasPendingMacrotasks(): boolean { return this._hasPendingMacrotasks; }
|
||||
|
||||
/**
|
||||
* Executes the `fn` function synchronously within the Angular zone and returns value returned by
|
||||
@ -308,18 +201,7 @@ export class NgZone {
|
||||
* Any future tasks or microtasks scheduled from within this function will continue executing from
|
||||
* within the Angular zone.
|
||||
*/
|
||||
run(fn: () => any): any {
|
||||
if (this._disabled) {
|
||||
return fn();
|
||||
} else {
|
||||
var s = this._runScope();
|
||||
try {
|
||||
return this._innerZone.run(fn);
|
||||
} finally {
|
||||
wtfLeave(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
run(fn: () => any): any { return this._zoneImpl.runInner(fn); }
|
||||
|
||||
/**
|
||||
* Executes the `fn` function synchronously in Angular's parent zone and returns value returned by
|
||||
@ -333,130 +215,5 @@ export class NgZone {
|
||||
*
|
||||
* Use {@link #run} to reenter the Angular zone and do work that updates the application model.
|
||||
*/
|
||||
runOutsideAngular(fn: () => any): any {
|
||||
if (this._disabled) {
|
||||
return fn();
|
||||
} else {
|
||||
return this._mountZone.run(fn);
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_createInnerZone(zone, enableLongStackTrace) {
|
||||
var microtaskScope = this._microtaskScope;
|
||||
var ngZone = this;
|
||||
var errorHandling;
|
||||
|
||||
if (enableLongStackTrace) {
|
||||
errorHandling =
|
||||
StringMapWrapper.merge(global.Zone.longStackTraceZone,
|
||||
{onError: function(e) { ngZone._notifyOnError(this, e); }});
|
||||
} else {
|
||||
errorHandling = {onError: function(e) { ngZone._notifyOnError(this, e); }};
|
||||
}
|
||||
|
||||
return zone.fork(errorHandling)
|
||||
.fork({
|
||||
'$run': function(parentRun) {
|
||||
return function() {
|
||||
try {
|
||||
ngZone._nestedRun++;
|
||||
if (!ngZone._hasExecutedCodeInInnerZone) {
|
||||
ngZone._hasExecutedCodeInInnerZone = true;
|
||||
ngZone._notifyOnTurnStart(parentRun);
|
||||
if (ngZone._onTurnStart) {
|
||||
parentRun.call(ngZone._innerZone, ngZone._onTurnStart);
|
||||
}
|
||||
}
|
||||
return parentRun.apply(this, arguments);
|
||||
} finally {
|
||||
ngZone._nestedRun--;
|
||||
// If there are no more pending microtasks, we are at the end of a VM turn (or in
|
||||
// onTurnStart)
|
||||
// _nestedRun will be 0 at the end of a macrotasks (it could be > 0 when there are
|
||||
// nested calls
|
||||
// to run()).
|
||||
if (ngZone._pendingMicrotasks == 0 && ngZone._nestedRun == 0 &&
|
||||
!this._inVmTurnDone) {
|
||||
if (ngZone._hasExecutedCodeInInnerZone) {
|
||||
try {
|
||||
this._inVmTurnDone = true;
|
||||
ngZone._notifyOnTurnDone(parentRun);
|
||||
if (ngZone._onTurnDone) {
|
||||
parentRun.call(ngZone._innerZone, ngZone._onTurnDone);
|
||||
}
|
||||
} finally {
|
||||
this._inVmTurnDone = false;
|
||||
ngZone._hasExecutedCodeInInnerZone = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (ngZone._pendingMicrotasks === 0) {
|
||||
ngZone._notifyOnEventDone();
|
||||
if (isPresent(ngZone._onEventDone)) {
|
||||
ngZone.runOutsideAngular(ngZone._onEventDone);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
'$scheduleMicrotask': function(parentScheduleMicrotask) {
|
||||
return function(fn) {
|
||||
ngZone._pendingMicrotasks++;
|
||||
var microtask = function() {
|
||||
var s = microtaskScope();
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
ngZone._pendingMicrotasks--;
|
||||
wtfLeave(s);
|
||||
}
|
||||
};
|
||||
parentScheduleMicrotask.call(this, microtask);
|
||||
};
|
||||
},
|
||||
'$setTimeout': function(parentSetTimeout) {
|
||||
return function(fn: Function, delay: number, ...args) {
|
||||
var id;
|
||||
var cb = function() {
|
||||
fn();
|
||||
ListWrapper.remove(ngZone._pendingTimeouts, id);
|
||||
};
|
||||
id = parentSetTimeout.call(this, cb, delay, args);
|
||||
ngZone._pendingTimeouts.push(id);
|
||||
return id;
|
||||
};
|
||||
},
|
||||
'$clearTimeout': function(parentClearTimeout) {
|
||||
return function(id: number) {
|
||||
parentClearTimeout.call(this, id);
|
||||
ListWrapper.remove(ngZone._pendingTimeouts, id);
|
||||
};
|
||||
},
|
||||
_innerZone: true
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_notifyOnError(zone, e): void {
|
||||
if (isPresent(this._onErrorHandler) || ObservableWrapper.hasSubscribers(this._onErrorEvents)) {
|
||||
var trace = [normalizeBlank(e.stack)];
|
||||
|
||||
while (zone && zone.constructedAtException) {
|
||||
trace.push(zone.constructedAtException.get());
|
||||
zone = zone.parent;
|
||||
}
|
||||
if (ObservableWrapper.hasSubscribers(this._onErrorEvents)) {
|
||||
ObservableWrapper.callEmit(this._onErrorEvents, new NgZoneError(e, trace));
|
||||
}
|
||||
if (isPresent(this._onErrorHandler)) {
|
||||
this._onErrorHandler(e, trace);
|
||||
}
|
||||
} else {
|
||||
console.log('## _notifyOnError ##');
|
||||
console.log(e.stack);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
runOutsideAngular(fn: () => any): any { return this._zoneImpl.runOuter(fn); }
|
||||
}
|
||||
|
222
modules/angular2/src/core/zone/ng_zone_impl.dart
Normal file
222
modules/angular2/src/core/zone/ng_zone_impl.dart
Normal file
@ -0,0 +1,222 @@
|
||||
library angular.zone;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:stack_trace/stack_trace.dart' show Chain;
|
||||
|
||||
typedef void ZeroArgFunction();
|
||||
typedef void ErrorHandlingFn(error, stackTrace);
|
||||
|
||||
/**
|
||||
* A `Timer` wrapper that lets you specify additional functions to call when it
|
||||
* is cancelled.
|
||||
*/
|
||||
class WrappedTimer implements Timer {
|
||||
Timer _timer;
|
||||
ZeroArgFunction _onCancelCb;
|
||||
|
||||
WrappedTimer(Timer timer) {
|
||||
_timer = timer;
|
||||
}
|
||||
|
||||
void addOnCancelCb(ZeroArgFunction onCancelCb) {
|
||||
if (this._onCancelCb != null) {
|
||||
throw "On cancel cb already registered";
|
||||
}
|
||||
this._onCancelCb = onCancelCb;
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
if (this._onCancelCb != null) {
|
||||
this._onCancelCb();
|
||||
}
|
||||
_timer.cancel();
|
||||
}
|
||||
|
||||
bool get isActive => _timer.isActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores error information; delivered via [NgZone.onError] stream.
|
||||
*/
|
||||
class NgZoneError {
|
||||
/// Error object thrown.
|
||||
final error;
|
||||
/// Either long or short chain of stack traces.
|
||||
final List stackTrace;
|
||||
NgZoneError(this.error, this.stackTrace);
|
||||
}
|
||||
|
||||
/**
|
||||
* A `Zone` wrapper that lets you schedule tasks after its private microtask queue is exhausted but
|
||||
* before the next "VM turn", i.e. event loop iteration.
|
||||
*
|
||||
* This lets you freely schedule microtasks that prepare data, and set an {@link onMicrotaskEmpty} handler that
|
||||
* will consume that data after it's ready but before the browser has a chance to re-render.
|
||||
*
|
||||
* A VM turn consist of a single macrotask followed 0 to many microtasks.
|
||||
*
|
||||
* The wrapper maintains an "inner" and "mount" `Zone`. The application code will executes
|
||||
* in the "inner" zone unless `runOutsideAngular` is explicitely called.
|
||||
*
|
||||
* A typical application will create a singleton `NgZone`. The mount zone is the `Zone` where the singleton has been
|
||||
* instantiated. The default `onMicrotaskEmpty` runs the Angular change detection.
|
||||
*/
|
||||
class NgZoneImpl {
|
||||
static bool isInAngularZone() {
|
||||
return Zone.current['isAngularZone'] == true;
|
||||
}
|
||||
|
||||
// Number of microtasks pending from _innerZone (& descendants)
|
||||
int _pendingMicrotasks = 0;
|
||||
List<Timer> _pendingTimers = [];
|
||||
Function onEnter;
|
||||
Function onLeave;
|
||||
Function setMicrotask;
|
||||
Function setMacrotask;
|
||||
Function onError;
|
||||
|
||||
Zone _outerZone;
|
||||
Zone _innerZone;
|
||||
/**
|
||||
* Associates with this
|
||||
*
|
||||
* - a "mount" [Zone], which is a the one that instantiated this.
|
||||
* - an "inner" [Zone], which is a child of the mount [Zone].
|
||||
*
|
||||
* @param {bool} trace whether to enable long stack trace. They should only be
|
||||
* enabled in development mode as they significantly impact perf.
|
||||
*/
|
||||
NgZoneImpl({
|
||||
bool trace,
|
||||
Function this.onEnter,
|
||||
Function this.onLeave,
|
||||
Function this.setMicrotask,
|
||||
Function this.setMacrotask,
|
||||
Function this.onError
|
||||
}) {
|
||||
_outerZone = Zone.current;
|
||||
|
||||
if (trace) {
|
||||
_innerZone = Chain.capture(
|
||||
() => _createInnerZone(Zone.current),
|
||||
onError: _onErrorWithLongStackTrace
|
||||
);
|
||||
} else {
|
||||
_innerZone = _createInnerZone(
|
||||
Zone.current,
|
||||
handleUncaughtError: _onErrorWithoutLongStackTrace
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Zone _createInnerZone(Zone zone, {handleUncaughtError}) {
|
||||
return zone.fork(
|
||||
specification: new ZoneSpecification(
|
||||
scheduleMicrotask: _scheduleMicrotask,
|
||||
run: _run,
|
||||
runUnary: _runUnary,
|
||||
runBinary: _runBinary,
|
||||
handleUncaughtError: handleUncaughtError,
|
||||
createTimer: _createTimer),
|
||||
zoneValues: {'isAngularZone': true}
|
||||
);
|
||||
}
|
||||
|
||||
dynamic runInner(fn()) {
|
||||
return _innerZone.runGuarded(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs `fn` in the mount zone and returns whatever it returns.
|
||||
*
|
||||
* In a typical app where the inner zone is the Angular zone, this allows one to escape Angular's
|
||||
* auto-digest mechanism.
|
||||
*
|
||||
* ```
|
||||
* void myFunction(NgZone zone, Element element) {
|
||||
* element.onClick.listen(() {
|
||||
* // auto-digest will run after element click.
|
||||
* });
|
||||
* zone.runOutsideAngular(() {
|
||||
* element.onMouseMove.listen(() {
|
||||
* // auto-digest will NOT run after mouse move
|
||||
* });
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
dynamic runOuter(fn()) {
|
||||
return _outerZone.run(fn);
|
||||
}
|
||||
|
||||
dynamic _run(Zone self, ZoneDelegate parent, Zone zone, fn()) {
|
||||
try {
|
||||
onEnter();
|
||||
return parent.run(zone, fn);
|
||||
} finally {
|
||||
onLeave();
|
||||
}
|
||||
}
|
||||
|
||||
dynamic _runUnary(Zone self, ZoneDelegate parent, Zone zone, fn(arg), arg) =>
|
||||
_run(self, parent, zone, () => fn(arg));
|
||||
|
||||
dynamic _runBinary(Zone self, ZoneDelegate parent, Zone zone, fn(arg1, arg2),
|
||||
arg1, arg2) =>
|
||||
_run(self, parent, zone, () => fn(arg1, arg2));
|
||||
|
||||
void _scheduleMicrotask(Zone self, ZoneDelegate parent, Zone zone, fn) {
|
||||
if (_pendingMicrotasks == 0) {
|
||||
setMicrotask(true);
|
||||
}
|
||||
_pendingMicrotasks++;
|
||||
var microtask = () {
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
_pendingMicrotasks--;
|
||||
if (_pendingMicrotasks == 0) {
|
||||
setMicrotask(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
parent.scheduleMicrotask(zone, microtask);
|
||||
}
|
||||
|
||||
// Called by Chain.capture() on errors when long stack traces are enabled
|
||||
void _onErrorWithLongStackTrace(error, Chain chain) {
|
||||
final traces = chain.terse.traces.map((t) => t.toString()).toList();
|
||||
onError(new NgZoneError(error, traces));
|
||||
}
|
||||
|
||||
// Outer zone handleUnchaughtError when long stack traces are not used
|
||||
void _onErrorWithoutLongStackTrace(Zone self, ZoneDelegate parent, Zone zone,
|
||||
error, StackTrace trace)
|
||||
{
|
||||
onError(new NgZoneError(error, [trace.toString()]));
|
||||
}
|
||||
|
||||
Timer _createTimer(
|
||||
Zone self, ZoneDelegate parent, Zone zone, Duration duration, fn()) {
|
||||
WrappedTimer wrappedTimer;
|
||||
var cb = () {
|
||||
try {
|
||||
fn();
|
||||
} finally {
|
||||
_pendingTimers.remove(wrappedTimer);
|
||||
setMacrotask(_pendingTimers.isNotEmpty);
|
||||
}
|
||||
};
|
||||
Timer timer = parent.createTimer(zone, duration, cb);
|
||||
wrappedTimer = new WrappedTimer(timer);
|
||||
wrappedTimer.addOnCancelCb(() {
|
||||
_pendingTimers.remove(wrappedTimer);
|
||||
setMacrotask(_pendingTimers.isNotEmpty);
|
||||
});
|
||||
|
||||
_pendingTimers.add(wrappedTimer);
|
||||
setMacrotask(true);
|
||||
return wrappedTimer;
|
||||
}
|
||||
|
||||
}
|
96
modules/angular2/src/core/zone/ng_zone_impl.ts
Normal file
96
modules/angular2/src/core/zone/ng_zone_impl.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import {global} from 'angular2/src/facade/lang';
|
||||
|
||||
/**
|
||||
* Stores error information; delivered via [NgZone.onError] stream.
|
||||
*/
|
||||
export class NgZoneError {
|
||||
constructor(public error: any, public stackTrace: any) {}
|
||||
}
|
||||
|
||||
|
||||
export class NgZoneImpl implements ZoneSpec {
|
||||
static isInAngularZone(): boolean { return Zone.current.get('isAngularZone') === true; }
|
||||
|
||||
public name: string = 'angular';
|
||||
public properties: {[k: string]: string} = <any>{'isAngularZone': true};
|
||||
|
||||
private outer: Zone;
|
||||
private inner: Zone;
|
||||
|
||||
private onEnter: () => void;
|
||||
private onLeave: () => void;
|
||||
private setMicrotask: (hasMicrotasks: boolean) => void;
|
||||
private setMacrotask: (hasMacrotasks: boolean) => void;
|
||||
private onError: (error: NgZoneError) => void;
|
||||
|
||||
constructor({trace, onEnter, onLeave, setMicrotask, setMacrotask, onError}: {
|
||||
trace: boolean,
|
||||
onEnter: () => void,
|
||||
onLeave: () => void,
|
||||
setMicrotask: (hasMicrotasks: boolean) => void,
|
||||
setMacrotask: (hasMacrotasks: boolean) => void,
|
||||
onError: (error: NgZoneError) => void
|
||||
}) {
|
||||
this.onEnter = onEnter;
|
||||
this.onLeave = onLeave;
|
||||
this.setMicrotask = setMicrotask;
|
||||
this.setMacrotask = setMacrotask;
|
||||
this.onError = onError;
|
||||
|
||||
if (global.Zone) {
|
||||
this.outer = this.inner = Zone.current;
|
||||
if (Zone['wtfZoneSpec']) {
|
||||
this.inner = this.inner.fork(Zone['wtfZoneSpec']);
|
||||
}
|
||||
if (trace) {
|
||||
this.inner = this.inner.fork(Zone['longStackTraceZoneSpec']);
|
||||
}
|
||||
this.inner = this.inner.fork(this);
|
||||
} else {
|
||||
throw new Error('Angular2 needs to be run with Zone.js polyfill.');
|
||||
}
|
||||
}
|
||||
|
||||
onInvokeTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task, applyThis: any,
|
||||
applyArgs: any): any {
|
||||
try {
|
||||
this.onEnter();
|
||||
return delegate.invokeTask(target, task, applyThis, applyArgs);
|
||||
} finally {
|
||||
this.onLeave();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
onInvoke(delegate: ZoneDelegate, current: Zone, target: Zone, callback: Function, applyThis: any,
|
||||
applyArgs: any[], source: string): any {
|
||||
try {
|
||||
this.onEnter();
|
||||
return delegate.invoke(target, callback, applyThis, applyArgs, source);
|
||||
} finally {
|
||||
this.onLeave();
|
||||
}
|
||||
}
|
||||
|
||||
onHasTask(delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
|
||||
delegate.hasTask(target, hasTaskState);
|
||||
if (current == target) {
|
||||
// We are only interested in hasTask events which originate from our zone
|
||||
// (A child hasTask event is not interesting to us)
|
||||
if (hasTaskState.change == 'microTask') {
|
||||
this.setMicrotask(hasTaskState.microTask);
|
||||
} else if (hasTaskState.change == 'macroTask') {
|
||||
this.setMacrotask(hasTaskState.macroTask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onHandleError(delegate: ZoneDelegate, current: Zone, target: Zone, error: any): boolean {
|
||||
delegate.handleError(target, error);
|
||||
this.onError(new NgZoneError(error, error.stack));
|
||||
return false;
|
||||
}
|
||||
|
||||
runInner(fn: () => any): any { return this.inner.runGuarded(fn); };
|
||||
runOuter(fn: () => any): any { return this.outer.run(fn); };
|
||||
}
|
@ -3,7 +3,7 @@ library angular.core.facade.lang;
|
||||
export 'dart:core' show Type, RegExp, print, DateTime;
|
||||
import 'dart:math' as math;
|
||||
import 'dart:convert' as convert;
|
||||
import 'dart:async' show Future;
|
||||
import 'dart:async' show Future, Zone;
|
||||
|
||||
String getTypeNameForDebugging(Type type) => type.toString();
|
||||
|
||||
@ -20,6 +20,10 @@ class CONST {
|
||||
|
||||
const IS_DART = true;
|
||||
|
||||
scheduleMicroTask(Function fn) {
|
||||
Zone.current.scheduleMicrotask(fn);
|
||||
}
|
||||
|
||||
bool isPresent(Object obj) => obj != null;
|
||||
bool isBlank(Object obj) => obj == null;
|
||||
bool isString(Object obj) => obj is String;
|
||||
|
@ -1,14 +1,3 @@
|
||||
// Zones are TC-39 standards-track so users could choose a different implementation
|
||||
// Rather than import {Zone} from 'zone.js' we define an interface
|
||||
// so that any library that structurally matches may be used with Angular 2.
|
||||
export interface ZoneLike {
|
||||
fork(locals?: any): ZoneLike;
|
||||
run(fn: any, applyTo?: any, applyWith?: any): any;
|
||||
}
|
||||
export interface ZoneLikeConstructor {
|
||||
longStackTraceZone: { [key: string]: any; };
|
||||
}
|
||||
|
||||
export interface BrowserNodeGlobal {
|
||||
Object: typeof Object;
|
||||
Array: typeof Array;
|
||||
@ -20,8 +9,7 @@ export interface BrowserNodeGlobal {
|
||||
Math: any; // typeof Math;
|
||||
assert(condition: any): void;
|
||||
Reflect: any;
|
||||
zone: ZoneLike;
|
||||
Zone: ZoneLikeConstructor;
|
||||
Zone: typeof Zone;
|
||||
getAngularTestability: Function;
|
||||
getAllAngularTestabilities: Function;
|
||||
getAllAngularRootElements: Function;
|
||||
@ -46,6 +34,10 @@ if (typeof window === 'undefined') {
|
||||
globalScope = <any>window;
|
||||
}
|
||||
|
||||
export function scheduleMicroTask(fn: Function) {
|
||||
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
|
||||
}
|
||||
|
||||
export const IS_DART = false;
|
||||
|
||||
// Need to declare a new variable for global here since TypeScript
|
||||
|
@ -8,18 +8,15 @@ import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
@Injectable()
|
||||
export class MockNgZone extends NgZone {
|
||||
/** @internal */
|
||||
_mockOnEventDone: EventEmitter<any>;
|
||||
private _mockOnStable: EventEmitter<any> = new EventEmitter(false);
|
||||
|
||||
constructor() {
|
||||
super({enableLongStackTrace: false});
|
||||
this._mockOnEventDone = new EventEmitter<any>(false);
|
||||
}
|
||||
constructor() { super({enableLongStackTrace: false}); }
|
||||
|
||||
get onEventDone() { return this._mockOnEventDone; }
|
||||
get onStable() { return this._mockOnStable; }
|
||||
|
||||
run(fn: Function): any { return fn(); }
|
||||
|
||||
runOutsideAngular(fn: Function): any { return fn(); }
|
||||
|
||||
simulateZoneExit(): void { ObservableWrapper.callNext(this.onEventDone, null); }
|
||||
simulateZoneExit(): void { ObservableWrapper.callNext(this.onStable, null); }
|
||||
}
|
||||
|
@ -271,9 +271,9 @@ export class UnresolvedInstruction extends Instruction {
|
||||
if (isPresent(this.component)) {
|
||||
return PromiseWrapper.resolve(this.component);
|
||||
}
|
||||
return this._resolver().then((resolution: Instruction) => {
|
||||
this.child = resolution.child;
|
||||
return this.component = resolution.component;
|
||||
return this._resolver().then((instruction: Instruction) => {
|
||||
this.child = isPresent(instruction) ? instruction.child : null;
|
||||
return this.component = isPresent(instruction) ? instruction.component : null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,58 @@
|
||||
import {global} from 'angular2/src/facade/lang';
|
||||
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
||||
import {BaseException} from 'angular2/src/facade/exceptions';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {NgZoneZone} from 'angular2/src/core/zone/ng_zone';
|
||||
|
||||
var _scheduler;
|
||||
var _microtasks: Function[] = [];
|
||||
var _pendingPeriodicTimers: number[] = [];
|
||||
var _pendingTimers: number[] = [];
|
||||
|
||||
interface FakeAsyncZone extends NgZoneZone {
|
||||
_inFakeAsyncZone: boolean;
|
||||
class FakeAsyncZoneSpec implements ZoneSpec {
|
||||
static assertInZone(): void {
|
||||
if (!Zone.current.get('inFakeAsyncZone')) {
|
||||
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
||||
}
|
||||
}
|
||||
|
||||
name: string = 'fakeAsync';
|
||||
|
||||
properties: {[key: string]: any} = {'inFakeAsyncZone': true};
|
||||
|
||||
onScheduleTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): Task {
|
||||
switch (task.type) {
|
||||
case 'microTask':
|
||||
_microtasks.push(task.invoke);
|
||||
break;
|
||||
case 'macroTask':
|
||||
switch (task.source) {
|
||||
case 'setTimeout':
|
||||
task.data['handleId'] = _setTimeout(task.invoke, task.data['delay'], task.data['args']);
|
||||
break;
|
||||
case 'setInterval':
|
||||
task.data['handleId'] =
|
||||
_setInterval(task.invoke, task.data['delay'], task.data['args']);
|
||||
break;
|
||||
default:
|
||||
task = delegate.scheduleTask(target, task);
|
||||
}
|
||||
break;
|
||||
case 'eventTask':
|
||||
task = delegate.scheduleTask(target, task);
|
||||
break;
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
onCancelTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): any {
|
||||
switch (task.source) {
|
||||
case 'setTimeout':
|
||||
return _clearTimeout(task.data['handleId']);
|
||||
case 'setInterval':
|
||||
return _clearInterval(task.data['handleId']);
|
||||
default:
|
||||
return delegate.scheduleTask(target, task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -27,18 +70,11 @@ interface FakeAsyncZone extends NgZoneZone {
|
||||
* @returns {Function} The function wrapped to be executed in the fakeAsync zone
|
||||
*/
|
||||
export function fakeAsync(fn: Function): Function {
|
||||
if ((<FakeAsyncZone>global.zone)._inFakeAsyncZone) {
|
||||
if (Zone.current.get('inFakeAsyncZone')) {
|
||||
throw new Error('fakeAsync() calls can not be nested');
|
||||
}
|
||||
|
||||
var fakeAsyncZone = <FakeAsyncZone>global.zone.fork({
|
||||
setTimeout: _setTimeout,
|
||||
clearTimeout: _clearTimeout,
|
||||
setInterval: _setInterval,
|
||||
clearInterval: _clearInterval,
|
||||
scheduleMicrotask: _scheduleMicrotask,
|
||||
_inFakeAsyncZone: true
|
||||
});
|
||||
var fakeAsyncZone = Zone.current.fork(new FakeAsyncZoneSpec());
|
||||
|
||||
return function(...args) {
|
||||
// TODO(tbosch): This class should already be part of the jasmine typings but it is not...
|
||||
@ -97,7 +133,7 @@ export function clearPendingTimers(): void {
|
||||
* @param {number} millis Number of millisecond, defaults to 0
|
||||
*/
|
||||
export function tick(millis: number = 0): void {
|
||||
_assertInFakeAsyncZone();
|
||||
FakeAsyncZoneSpec.assertInZone();
|
||||
flushMicrotasks();
|
||||
_scheduler.tick(millis);
|
||||
}
|
||||
@ -106,14 +142,14 @@ export function tick(millis: number = 0): void {
|
||||
* Flush any pending microtasks.
|
||||
*/
|
||||
export function flushMicrotasks(): void {
|
||||
_assertInFakeAsyncZone();
|
||||
FakeAsyncZoneSpec.assertInZone();
|
||||
while (_microtasks.length > 0) {
|
||||
var microtask = ListWrapper.removeAt(_microtasks, 0);
|
||||
microtask();
|
||||
}
|
||||
}
|
||||
|
||||
function _setTimeout(fn: Function, delay: number, ...args): number {
|
||||
function _setTimeout(fn: Function, delay: number, args: any[]): number {
|
||||
var cb = _fnAndFlush(fn);
|
||||
var id = _scheduler.scheduleFunction(cb, delay, args);
|
||||
_pendingTimers.push(id);
|
||||
@ -145,16 +181,6 @@ function _fnAndFlush(fn: Function): Function {
|
||||
}
|
||||
}
|
||||
|
||||
function _scheduleMicrotask(microtask: Function): void {
|
||||
_microtasks.push(microtask);
|
||||
}
|
||||
|
||||
function _dequeueTimer(id: number): Function {
|
||||
return function() { ListWrapper.remove(_pendingTimers, id); }
|
||||
}
|
||||
|
||||
function _assertInFakeAsyncZone(): void {
|
||||
if (!global.zone || !(<FakeAsyncZone>global.zone)._inFakeAsyncZone) {
|
||||
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,8 @@ export 'testing_internal_core.dart'
|
||||
xdescribe;
|
||||
|
||||
import 'package:angular2/platform/testing/browser.dart';
|
||||
import 'package:angular2/src/facade/collection.dart' show StringMapWrapper;
|
||||
import "package:angular2/src/core/zone/ng_zone.dart" show NgZone;
|
||||
|
||||
export 'test_injector.dart' show inject;
|
||||
|
||||
@ -62,4 +64,6 @@ void ddescribe(name, fn) {
|
||||
void xdescribe(name, fn) {
|
||||
testSetup();
|
||||
core.xdescribe(name, fn);
|
||||
}
|
||||
}
|
||||
|
||||
bool isInInnerZone() => NgZone.isInAngularZone();
|
||||
|
@ -1,12 +1,11 @@
|
||||
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
||||
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {global, isFunction, Math} from 'angular2/src/facade/lang';
|
||||
import {NgZoneZone} from 'angular2/src/core/zone/ng_zone';
|
||||
|
||||
import {provide} from 'angular2/core';
|
||||
|
||||
import {TestInjector, getTestInjector, FunctionWithParamTokens, inject} from './test_injector';
|
||||
import {getTestInjector, FunctionWithParamTokens, inject} from './test_injector';
|
||||
import {browserDetection} from './utils';
|
||||
import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
||||
|
||||
export {inject} from './test_injector';
|
||||
|
||||
@ -256,7 +255,3 @@ export class SpyObject {
|
||||
return newSpy;
|
||||
}
|
||||
}
|
||||
|
||||
export function isInInnerZone(): boolean {
|
||||
return (<NgZoneZone>global.zone)._innerZone === true;
|
||||
}
|
||||
|
@ -5,22 +5,21 @@ import {isPresent, isString, RegExpWrapper, StringWrapper, RegExp} from 'angular
|
||||
|
||||
@Injectable()
|
||||
export class Log {
|
||||
/** @internal */
|
||||
_result: any[];
|
||||
logItems: any[];
|
||||
|
||||
constructor() { this._result = []; }
|
||||
constructor() { this.logItems = []; }
|
||||
|
||||
add(value): void { this._result.push(value); }
|
||||
add(value): void { this.logItems.push(value); }
|
||||
|
||||
fn(value) {
|
||||
return (a1: any = null, a2: any = null, a3: any = null, a4: any = null, a5: any = null) => {
|
||||
this._result.push(value);
|
||||
this.logItems.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
clear(): void { this._result = []; }
|
||||
clear(): void { this.logItems = []; }
|
||||
|
||||
result(): string { return this._result.join("; "); }
|
||||
result(): string { return this.logItems.join("; "); }
|
||||
}
|
||||
|
||||
export var browserDetection: BrowserDetection = null;
|
||||
|
@ -338,7 +338,7 @@ export class UpgradeAdapter {
|
||||
'$rootScope',
|
||||
(injector: angular.IInjectorService, rootScope: angular.IRootScopeService) => {
|
||||
ng1Injector = injector;
|
||||
ObservableWrapper.subscribe(ngZone.onTurnDone,
|
||||
ObservableWrapper.subscribe(ngZone.onMicrotaskEmpty,
|
||||
(_) => ngZone.runOutsideAngular(() => rootScope.$apply()));
|
||||
ng1compilePromise =
|
||||
UpgradeNg1ComponentAdapterBuilder.resolve(this.downgradedComponents, injector);
|
||||
|
@ -46,7 +46,7 @@ abstract class GenericMessageBusSink implements MessageBusSink {
|
||||
void attachToZone(NgZone zone) {
|
||||
_zone = zone;
|
||||
_zone.runOutsideAngular(() {
|
||||
_zone.onEventDone.listen((_) {
|
||||
_zone.onStable.listen((_) {
|
||||
if (_messageBuffer.length > 0) {
|
||||
sendMessages(_messageBuffer);
|
||||
_messageBuffer.clear();
|
||||
|
@ -22,7 +22,7 @@ export class PostMessageBusSink implements MessageBusSink {
|
||||
attachToZone(zone: NgZone): void {
|
||||
this._zone = zone;
|
||||
this._zone.runOutsideAngular(() => {
|
||||
ObservableWrapper.subscribe(this._zone.onEventDone, (_) => { this._handleOnEventDone(); });
|
||||
ObservableWrapper.subscribe(this._zone.onStable, (_) => { this._handleOnEventDone(); });
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user