repackaging: all the file moves
This commit is contained in:
227
modules/@angular/core/src/zone/ng_zone.ts
Normal file
227
modules/@angular/core/src/zone/ng_zone.ts
Normal file
@ -0,0 +1,227 @@
|
||||
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';
|
||||
|
||||
|
||||
/**
|
||||
* An injectable service for executing work inside or outside of the Angular zone.
|
||||
*
|
||||
* The most common use of this service is to optimize performance when starting a work consisting of
|
||||
* one or more asynchronous tasks that don't require UI updates or error handling to be handled by
|
||||
* Angular. Such tasks can be kicked off via {@link #runOutsideAngular} and if needed, these tasks
|
||||
* can reenter the Angular zone via {@link #run}.
|
||||
*
|
||||
* <!-- TODO: add/fix links to:
|
||||
* - docs explaining zones and the use of zones in Angular and change-detection
|
||||
* - link to runOutsideAngular/run (throughout this file!)
|
||||
* -->
|
||||
*
|
||||
* ### Example ([live demo](http://plnkr.co/edit/lY9m8HLy7z06vDoUaSN2?p=preview))
|
||||
* ```
|
||||
* import {Component, View, NgZone} from 'angular2/core';
|
||||
* import {NgIf} from 'angular2/common';
|
||||
*
|
||||
* @Component({
|
||||
* selector: 'ng-zone-demo'.
|
||||
* template: `
|
||||
* <h2>Demo: NgZone</h2>
|
||||
*
|
||||
* <p>Progress: {{progress}}%</p>
|
||||
* <p *ngIf="progress >= 100">Done processing {{label}} of Angular zone!</p>
|
||||
*
|
||||
* <button (click)="processWithinAngularZone()">Process within Angular zone</button>
|
||||
* <button (click)="processOutsideOfAngularZone()">Process outside of Angular zone</button>
|
||||
* `,
|
||||
* directives: [NgIf]
|
||||
* })
|
||||
* export class NgZoneDemo {
|
||||
* progress: number = 0;
|
||||
* label: string;
|
||||
*
|
||||
* constructor(private _ngZone: NgZone) {}
|
||||
*
|
||||
* // Loop inside the Angular zone
|
||||
* // so the UI DOES refresh after each setTimeout cycle
|
||||
* processWithinAngularZone() {
|
||||
* this.label = 'inside';
|
||||
* this.progress = 0;
|
||||
* this._increaseProgress(() => console.log('Inside Done!'));
|
||||
* }
|
||||
*
|
||||
* // Loop outside of the Angular zone
|
||||
* // so the UI DOES NOT refresh after each setTimeout cycle
|
||||
* processOutsideOfAngularZone() {
|
||||
* this.label = 'outside';
|
||||
* this.progress = 0;
|
||||
* this._ngZone.runOutsideAngular(() => {
|
||||
* this._increaseProgress(() => {
|
||||
* // reenter the Angular zone and display done
|
||||
* this._ngZone.run(() => {console.log('Outside Done!') });
|
||||
* }}));
|
||||
* }
|
||||
*
|
||||
*
|
||||
* _increaseProgress(doneCallback: () => void) {
|
||||
* this.progress += 1;
|
||||
* console.log(`Current progress: ${this.progress}%`);
|
||||
*
|
||||
* if (this.progress < 100) {
|
||||
* window.setTimeout(() => this._increaseProgress(doneCallback)), 10)
|
||||
* } else {
|
||||
* doneCallback();
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export class NgZone {
|
||||
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!');
|
||||
}
|
||||
}
|
||||
|
||||
private _zoneImpl: NgZoneImpl;
|
||||
|
||||
private _hasPendingMicrotasks: boolean = false;
|
||||
private _hasPendingMacrotasks: boolean = false;
|
||||
|
||||
/** @internal */
|
||||
private _isStable = true;
|
||||
/** @internal */
|
||||
private _nesting = 0;
|
||||
/** @internal */
|
||||
private _onUnstable: EventEmitter<any> = new EventEmitter(false);
|
||||
/** @internal */
|
||||
private _onMicrotaskEmpty: EventEmitter<any> = new EventEmitter(false);
|
||||
/** @internal */
|
||||
private _onStable: EventEmitter<any> = new EventEmitter(false);
|
||||
/** @internal */
|
||||
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 = 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);
|
||||
}
|
||||
},
|
||||
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 when code enters Angular Zone. This gets fired first on VM Turn.
|
||||
*/
|
||||
get onUnstable(): EventEmitter<any> { return this._onUnstable; }
|
||||
|
||||
/**
|
||||
* 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._hasPendingMicrotasks; }
|
||||
|
||||
/**
|
||||
* Whether there are any outstanding microtasks.
|
||||
*/
|
||||
get hasPendingMacrotasks(): boolean { return this._hasPendingMacrotasks; }
|
||||
|
||||
/**
|
||||
* Executes the `fn` function synchronously within the Angular zone and returns value returned by
|
||||
* the function.
|
||||
*
|
||||
* Running functions via `run` allows you to reenter Angular zone from a task that was executed
|
||||
* outside of the Angular zone (typically started via {@link #runOutsideAngular}).
|
||||
*
|
||||
* Any future tasks or microtasks scheduled from within this function will continue executing from
|
||||
* within the Angular zone.
|
||||
*
|
||||
* If a synchronous error happens it will be rethrown and not reported via `onError`.
|
||||
*/
|
||||
run(fn: () => any): any { return this._zoneImpl.runInner(fn); }
|
||||
|
||||
/**
|
||||
* Same as #run, except that synchronous errors are caught and forwarded
|
||||
* via `onError` and not rethrown.
|
||||
*/
|
||||
runGuarded(fn: () => any): any { return this._zoneImpl.runInnerGuarded(fn); }
|
||||
|
||||
/**
|
||||
* Executes the `fn` function synchronously in Angular's parent zone and returns value returned by
|
||||
* the function.
|
||||
*
|
||||
* Running functions via `runOutsideAngular` allows you to escape Angular's zone and do work that
|
||||
* doesn't trigger Angular change-detection or is subject to Angular's error handling.
|
||||
*
|
||||
* Any future tasks or microtasks scheduled from within this function will continue executing from
|
||||
* outside of the Angular zone.
|
||||
*
|
||||
* Use {@link #run} to reenter the Angular zone and do work that updates the application model.
|
||||
*/
|
||||
runOutsideAngular(fn: () => any): any { return this._zoneImpl.runOuter(fn); }
|
||||
}
|
226
modules/@angular/core/src/zone/ng_zone_impl.dart
Normal file
226
modules/@angular/core/src/zone/ng_zone_impl.dart
Normal file
@ -0,0 +1,226 @@
|
||||
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 runInnerGuarded(fn()) {
|
||||
return _innerZone.runGuarded(fn);
|
||||
}
|
||||
|
||||
dynamic runInner(fn()) {
|
||||
return _innerZone.run(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;
|
||||
}
|
||||
|
||||
}
|
100
modules/@angular/core/src/zone/ng_zone_impl.ts
Normal file
100
modules/@angular/core/src/zone/ng_zone_impl.ts
Normal file
@ -0,0 +1,100 @@
|
||||
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 {
|
||||
static isInAngularZone(): boolean { return Zone.current.get('isAngularZone') === true; }
|
||||
|
||||
/** @internal */
|
||||
private outer: Zone;
|
||||
/** @internal */
|
||||
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 (Zone) {
|
||||
this.outer = this.inner = Zone.current;
|
||||
if (Zone['wtfZoneSpec']) {
|
||||
this.inner = this.inner.fork(Zone['wtfZoneSpec']);
|
||||
}
|
||||
if (trace && Zone['longStackTraceZoneSpec']) {
|
||||
this.inner = this.inner.fork(Zone['longStackTraceZoneSpec']);
|
||||
}
|
||||
this.inner = this.inner.fork({
|
||||
name: 'angular',
|
||||
properties:<any>{'isAngularZone': true},
|
||||
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;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw new Error('Angular2 needs to be run with Zone.js polyfill.');
|
||||
}
|
||||
}
|
||||
|
||||
runInner(fn: () => any): any { return this.inner.run(fn); };
|
||||
runInnerGuarded(fn: () => any): any { return this.inner.runGuarded(fn); };
|
||||
runOuter(fn: () => any): any { return this.outer.run(fn); };
|
||||
}
|
Reference in New Issue
Block a user