refactor(VmTurnZone): renamed to NgZone

BREAKING CHANGE:

VmTurnZone has been renamed to NgZone.

- The public API has not chnanged,
- The "outer" zone is now named "mount" zone (private to NgZone).
This commit is contained in:
Victor Berchet 2015-05-07 15:38:57 +02:00
parent c75e216871
commit e11c20541a
11 changed files with 83 additions and 83 deletions

View File

@ -12,7 +12,7 @@ import {TemplateResolver} from './compiler/template_resolver';
import {DirectiveMetadataReader} from './compiler/directive_metadata_reader';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
import {ShadowDomStrategy} from 'angular2/src/render/dom/shadow_dom/shadow_dom_strategy';
import {EmulatedUnscopedShadowDomStrategy} from 'angular2/src/render/dom/shadow_dom/emulated_unscoped_shadow_dom_strategy';
@ -77,10 +77,10 @@ function _injectorBindings(appComponentType): List<Binding> {
bind(appComponentType).toFactory((ref) => ref.instance,
[appComponentRefToken]),
bind(LifeCycle).toFactory((exceptionHandler) => new LifeCycle(exceptionHandler, null, assertionsEnabled()),[ExceptionHandler]),
bind(EventManager).toFactory((zone) => {
bind(EventManager).toFactory((ngZone) => {
var plugins = [new HammerGesturesPlugin(), new KeyEventsPlugin(), new DomEventsPlugin()];
return new EventManager(plugins, zone);
}, [VmTurnZone]),
return new EventManager(plugins, ngZone);
}, [NgZone]),
bind(ShadowDomStrategy).toFactory(
(styleUrlResolver, doc) => new EmulatedUnscopedShadowDomStrategy(styleUrlResolver, doc.head),
[StyleUrlResolver, DOCUMENT_TOKEN]),
@ -123,7 +123,7 @@ function _injectorBindings(appComponentType): List<Binding> {
];
}
function _createVmZone(givenReporter:Function): VmTurnZone {
function _createNgZone(givenReporter:Function): NgZone {
var defaultErrorReporter = (exception, stackTrace) => {
var longStackTrace = ListWrapper.join(stackTrace, "\n\n-----async gap-----\n");
DOM.logError(`${exception}\n\n${longStackTrace}`);
@ -132,7 +132,7 @@ function _createVmZone(givenReporter:Function): VmTurnZone {
var reporter = isPresent(givenReporter) ? givenReporter : defaultErrorReporter;
var zone = new VmTurnZone({enableLongStackTrace: assertionsEnabled()});
var zone = new NgZone({enableLongStackTrace: assertionsEnabled()});
zone.initCallbacks({onErrorHandler: reporter});
return zone;
}
@ -247,7 +247,7 @@ export function bootstrap(appComponentType: Type,
BrowserDomAdapter.makeCurrent();
var bootstrapProcess = PromiseWrapper.completer();
var zone = _createVmZone(errorReporter);
var zone = _createNgZone(errorReporter);
zone.run(() => {
// TODO(rado): prepopulate template cache, so applications with only
// index.html and main.js are possible.
@ -295,11 +295,11 @@ export class ApplicationRef {
}
}
function _createAppInjector(appComponentType: Type, bindings: List<Binding>, zone: VmTurnZone): Injector {
function _createAppInjector(appComponentType: Type, bindings: List<Binding>, zone: NgZone): Injector {
if (isBlank(_rootInjector)) _rootInjector = Injector.resolveAndCreate(_rootBindings);
var mergedBindings = isPresent(bindings) ?
ListWrapper.concat(_injectorBindings(appComponentType), bindings) :
_injectorBindings(appComponentType);
ListWrapper.push(mergedBindings, bind(VmTurnZone).toValue(zone));
ListWrapper.push(mergedBindings, bind(NgZone).toValue(zone));
return _rootInjector.resolveAndCreateChild(mergedBindings);
}

View File

@ -1,6 +1,6 @@
import {Injectable} from 'angular2/src/di/annotations_impl';
import {ChangeDetector} from 'angular2/change_detection';
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {ExceptionHandler} from 'angular2/src/core/exception_handler';
import {isPresent} from 'angular2/src/facade/lang';
@ -46,7 +46,7 @@ export class LifeCycle {
/**
* @private
*/
registerWith(zone:VmTurnZone, changeDetector:ChangeDetector = null) {
registerWith(zone:NgZone, changeDetector:ChangeDetector = null) {
if (isPresent(changeDetector)) {
this._changeDetector=changeDetector;
}

View File

@ -1,6 +1,6 @@
// TODO(vicb): implement this class properly
// The current stub implementation is only here to please cjs tests
export class VmTurnZone {
export class NgZone {
constructor({enableLongStackTrace}) {
}

View File

@ -12,20 +12,20 @@ import 'package:stack_trace/stack_trace.dart' show Chain;
*
* A VM turn consist of a single macrotask followed 0 to many microtasks.
*
* The wrapper maintains an "inner" and "outer" `Zone`. The application code will executes
* 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 `VmTurnZone`. The outer `Zone` is a fork of the root
* `Zone`. The default `onTurnDone` runs the Angular change detection.
* 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 VmTurnZone {
class NgZone {
Function _onTurnStart;
Function _onTurnDone;
Function _onErrorHandler;
// Code executed in _outerZone does not trigger the onTurnDone.
Zone _outerZone;
// _innerZone is the child of _outerZone. Any code executed in this zone will trigger the
// 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;
@ -42,14 +42,14 @@ class VmTurnZone {
/**
* Associates with this
*
* - an "outer" [Zone], which is a the one that created this.
* - an "inner" [Zone], which is a child of the outer [Zone].
* - 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.
*/
VmTurnZone({bool enableLongStackTrace}) {
_outerZone = Zone.current;
NgZone({bool enableLongStackTrace}) {
_mountZone = Zone.current;
if (enableLongStackTrace) {
_innerZone = Chain.capture(
@ -68,7 +68,7 @@ class VmTurnZone {
* Initializes the zone hooks.
*
* The given error handler should re-throw the passed exception. Otherwise, exceptions will not
* propagate outside of the [VmTurnZone] and can alter the application execution flow.
* propagate outside of the [NgZone] and can alter the application execution flow.
* Not re-throwing could be used to help testing the code or advanced use cases.
*
* @param {Function} onTurnStart called before code executes in the inner zone for each VM turn
@ -88,7 +88,7 @@ class VmTurnZone {
* Angular's auto digest mechanism.
*
* ```
* VmTurnZone zone = <ref to the application zone>;
* NgZone zone = <ref to the application zone>;
*
* void functionCalledFromJS() {
* zone.run(() {
@ -105,13 +105,13 @@ class VmTurnZone {
}
/**
* Runs `fn` in the outer zone and returns whatever it returns.
* 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(VmTurnZone zone, Element element) {
* void myFunction(NgZone zone, Element element) {
* element.onClick.listen(() {
* // auto-digest will run after element click.
* });
@ -124,7 +124,7 @@ class VmTurnZone {
* ```
*/
dynamic runOutsideAngular(fn()) {
return _outerZone.run(fn);
return _mountZone.run(fn);
}
void _maybeStartVmTurn(ZoneDelegate parent) {

View File

@ -4,18 +4,18 @@ import {normalizeBlank, isPresent, global} from 'angular2/src/facade/lang';
/**
* A wrapper around zones that lets you schedule tasks after it has executed a task.
*
* The wrapper maintains an "inner" and "outer" `Zone`. The application code will executes
* The wrapper maintains an "inner" and an "mount" `Zone`. The application code will executes
* in the "inner" zone unless `runOutsideAngular` is explicitely called.
*
* A typical application will create a singleton `VmTurnZone`. The outer `Zone` is a fork of the root
* A typical application will create a singleton `NgZone`. The outer `Zone` is a fork of the root
* `Zone`. The default `onTurnDone` runs the Angular change detection.
*
* @exportedAs angular2/core
*/
export class VmTurnZone {
// Code executed in _outerZone does not trigger the onTurnDone.
_outerZone;
// _innerZone is the child of _outerZone. Any code executed in this zone will trigger the
export class NgZone {
// Code executed in _mountZone does not trigger the onTurnDone.
_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.
_innerZone;
@ -23,11 +23,11 @@ export class VmTurnZone {
_onTurnDone:Function;
_onErrorHandler:Function;
// Number of microtasks pending from _outerZone (& descendants)
// Number of microtasks pending from _innerZone (& descendants)
_pendingMicrotask: number;
// Whether some code has been executed in the _innerZone (& descendants) in the current turn
_hasExecutedCodeInInnerZone: boolean;
// run() call depth in _outerZone. 0 at the end of a macrotask
// run() call depth in _mountZone. 0 at the end of a macrotask
// zone.run(() => { // top-level call
// zone.run(() => {}); // nested call -> in-turn
// });
@ -36,8 +36,8 @@ export class VmTurnZone {
/**
* Associates with this
*
* - an "outer" zone, which is a child of the one that created this.
* - an "inner" zone, which is a child of the outer zone.
* - a "root" zone, which the one that instantiated this.
* - an "inner" zone, which is a child of the root zone.
*
* @param {bool} enableLongStackTrace whether to enable long stack trace. They should only be
* enabled in development mode as they significantly impact perf.
@ -51,8 +51,8 @@ export class VmTurnZone {
this._hasExecutedCodeInInnerZone = false;
this._nestedRun = 0;
this._outerZone = global.zone;
this._innerZone = this._createInnerZone(this._outerZone, enableLongStackTrace)
this._mountZone = global.zone;
this._innerZone = this._createInnerZone(this._mountZone, enableLongStackTrace)
}
/**
@ -75,7 +75,7 @@ export class VmTurnZone {
* Angular's auto digest mechanism.
*
* ```
* var zone: VmTurnZone = [ref to the application zone];
* var zone: NgZone = [ref to the application zone];
*
* zone.run(() => {
* // the change detection will run after this function and the microtasks it enqueues have executed.
@ -93,7 +93,7 @@ export class VmTurnZone {
* auto-digest mechanism.
*
* ```
* var zone: VmTurnZone = [ref to the application zone];
* var zone: NgZone = [ref to the application zone];
*
* zone.runOusideAngular(() => {
* element.onClick(() => {
@ -103,23 +103,23 @@ export class VmTurnZone {
* ```
*/
runOutsideAngular(fn) {
return this._outerZone.run(fn);
return this._mountZone.run(fn);
}
_createInnerZone(zone, enableLongStackTrace) {
var vmTurnZone = this;
var ngZone = this;
var errorHandling;
if (enableLongStackTrace) {
errorHandling = StringMapWrapper.merge(Zone.longStackTraceZone, {
onError: function (e) {
vmTurnZone._onError(this, e)
ngZone._onError(this, e)
}
});
} else {
errorHandling = {
onError: function (e) {
vmTurnZone._onError(this, e)
ngZone._onError(this, e)
}
};
}
@ -130,25 +130,25 @@ export class VmTurnZone {
'$run': function(parentRun) {
return function() {
try {
vmTurnZone._nestedRun++;
if (!vmTurnZone._hasExecutedCodeInInnerZone) {
vmTurnZone._hasExecutedCodeInInnerZone = true;
if (vmTurnZone._onTurnStart) {
parentRun.call(vmTurnZone._innerZone, vmTurnZone._onTurnStart);
ngZone._nestedRun++;
if (!ngZone._hasExecutedCodeInInnerZone) {
ngZone._hasExecutedCodeInInnerZone = true;
if (ngZone._onTurnStart) {
parentRun.call(ngZone._innerZone, ngZone._onTurnStart);
}
}
return parentRun.apply(this, arguments);
} finally {
vmTurnZone._nestedRun--;
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 (vmTurnZone._pendingMicrotasks == 0 && vmTurnZone._nestedRun == 0) {
if (vmTurnZone._onTurnDone && vmTurnZone._hasExecutedCodeInInnerZone) {
if (ngZone._pendingMicrotasks == 0 && ngZone._nestedRun == 0) {
if (ngZone._onTurnDone && ngZone._hasExecutedCodeInInnerZone) {
try {
parentRun.call(vmTurnZone._innerZone, vmTurnZone._onTurnDone);
parentRun.call(ngZone._innerZone, ngZone._onTurnDone);
} finally {
vmTurnZone._hasExecutedCodeInInnerZone = false;
ngZone._hasExecutedCodeInInnerZone = false;
}
}
}
@ -157,12 +157,12 @@ export class VmTurnZone {
},
'$scheduleMicrotask': function(parentScheduleMicrotask) {
return function(fn) {
vmTurnZone._pendingMicrotasks++;
ngZone._pendingMicrotasks++;
var microtask = function() {
try {
fn();
} finally {
vmTurnZone._pendingMicrotasks--;
ngZone._pendingMicrotasks--;
}
};
parentScheduleMicrotask.call(this, microtask);

View File

@ -1,12 +1,12 @@
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
export class MockVmTurnZone extends VmTurnZone {
export class MockNgZone extends NgZone {
constructor() {
super({enableLongStackTrace: false});
}
run(fn) {
fn();
return fn();
}
runOutsideAngular(fn) {

View File

@ -1,15 +1,15 @@
import {isBlank, BaseException, isPresent, StringWrapper} from 'angular2/src/facade/lang';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
var BUBBLE_SYMBOL = '^';
export class EventManager {
_plugins: List<EventManagerPlugin>;
_zone: VmTurnZone;
_zone: NgZone;
constructor(plugins: List<EventManagerPlugin>, zone: VmTurnZone) {
constructor(plugins: List<EventManagerPlugin>, zone: NgZone) {
this._zone = zone;
this._plugins = plugins;
for (var i = 0; i < plugins.length; i++) {
@ -29,7 +29,7 @@ export class EventManager {
return plugin.addGlobalEventListener(target, withoutBubbleSymbol, handler, withoutBubbleSymbol != eventName);
}
getZone(): VmTurnZone {
getZone(): NgZone {
return this._zone;
}
@ -93,7 +93,7 @@ export class DomEventsPlugin extends EventManagerPlugin {
});
}
_getOutsideHandler(shouldSupportBubble: boolean, element, handler: Function, zone: VmTurnZone) {
_getOutsideHandler(shouldSupportBubble: boolean, element, handler: Function, zone: NgZone) {
return shouldSupportBubble ?
DomEventsPlugin.bubbleCallback(element, handler, zone) :
DomEventsPlugin.sameElementCallback(element, handler, zone);

View File

@ -16,7 +16,7 @@ import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mappe
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver';
import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner';
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {DOM} from 'angular2/src/dom/dom_adapter';
@ -24,7 +24,7 @@ import {EventManager, DomEventsPlugin} from 'angular2/src/render/dom/events/even
import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock';
import {MockXHR} from 'angular2/src/mock/xhr_mock';
import {MockVmTurnZone} from 'angular2/src/mock/vm_turn_zone_mock';
import {MockNgZone} from 'angular2/src/mock/ng_zone_mock';
import {TestBed} from './test_bed';
@ -102,13 +102,13 @@ function _getAppBindings() {
StyleUrlResolver,
StyleInliner,
TestBed,
bind(VmTurnZone).toClass(MockVmTurnZone),
bind(NgZone).toClass(MockNgZone),
bind(EventManager).toFactory((zone) => {
var plugins = [
new DomEventsPlugin(),
];
return new EventManager(plugins, zone);
}, [VmTurnZone]),
}, [NgZone]),
];
}

View File

@ -17,7 +17,7 @@ import {PromiseWrapper} from 'angular2/src/facade/async';
import {ListWrapper} from 'angular2/src/facade/collection';
import {BaseException} from 'angular2/src/facade/lang';
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
// Schedules a macrotask (using a timer)
function macroTask(fn: Function): void {
@ -40,10 +40,10 @@ function logError(error, stackTrace) {
}
export function main() {
describe("VmTurnZone", () => {
describe("NgZone", () => {
function createZone(enableLongStackTrace) {
var zone = new VmTurnZone({enableLongStackTrace: enableLongStackTrace});
var zone = new NgZone({enableLongStackTrace: enableLongStackTrace});
zone.initCallbacks({
onTurnStart: _log.fn('onTurnStart'),
onTurnDone: _log.fn('onTurnDone')

View File

@ -1,6 +1,6 @@
import {describe, ddescribe, it, iit, xit, xdescribe, expect, beforeEach, el} from 'angular2/test_lib';
import {EventManager, EventManagerPlugin, DomEventsPlugin} from 'angular2/src/render/dom/events/event_manager';
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
import {List, ListWrapper, Map, MapWrapper} from 'angular2/src/facade/collection';
import {DOM} from 'angular2/src/dom/dom_adapter';
@ -17,7 +17,7 @@ export function main() {
var element = el('<div></div>');
var handler = (e) => e;
var plugin = new FakeEventManagerPlugin(['click']);
var manager = new EventManager([plugin, domEventPlugin], new FakeVmTurnZone());
var manager = new EventManager([plugin, domEventPlugin], new FakeNgZone());
manager.addEventListener(element, 'click', handler);
expect(MapWrapper.get(plugin._nonBubbleEventHandlers, 'click')).toBe(handler);
});
@ -26,7 +26,7 @@ export function main() {
var element = el('<div></div>');
var handler = (e) => e;
var plugin = new FakeEventManagerPlugin(['click']);
var manager = new EventManager([plugin, domEventPlugin], new FakeVmTurnZone());
var manager = new EventManager([plugin, domEventPlugin], new FakeNgZone());
manager.addEventListener(element, '^click', handler);
expect(MapWrapper.get(plugin._bubbleEventHandlers, 'click')).toBe(handler);
});
@ -37,7 +37,7 @@ export function main() {
var dblClickHandler = (e) => e;
var plugin1= new FakeEventManagerPlugin(['dblclick']);
var plugin2 = new FakeEventManagerPlugin(['click', 'dblclick']);
var manager = new EventManager([plugin1, plugin2], new FakeVmTurnZone());
var manager = new EventManager([plugin1, plugin2], new FakeNgZone());
manager.addEventListener(element, 'click', clickHandler);
manager.addEventListener(element, 'dblclick', dblClickHandler);
expect(MapWrapper.contains(plugin1._nonBubbleEventHandlers, 'click')).toBe(false);
@ -49,7 +49,7 @@ export function main() {
it('should throw when no plugin can handle the event', () => {
var element = el('<div></div>');
var plugin = new FakeEventManagerPlugin(['dblclick']);
var manager = new EventManager([plugin], new FakeVmTurnZone());
var manager = new EventManager([plugin], new FakeNgZone());
expect(() => manager.addEventListener(element, 'click', null))
.toThrowError('No event manager plugin found for event click');
});
@ -60,7 +60,7 @@ export function main() {
var dispatchedEvent = DOM.createMouseEvent('click');
var receivedEvent = null;
var handler = (e) => { receivedEvent = e; };
var manager = new EventManager([domEventPlugin], new FakeVmTurnZone());
var manager = new EventManager([domEventPlugin], new FakeNgZone());
manager.addEventListener(element, 'click', handler);
DOM.dispatchEvent(child, dispatchedEvent);
@ -76,7 +76,7 @@ export function main() {
var dispatchedEvent = DOM.createMouseEvent('click');
var receivedEvent = null;
var handler = (e) => { receivedEvent = e; };
var manager = new EventManager([domEventPlugin], new FakeVmTurnZone());
var manager = new EventManager([domEventPlugin], new FakeNgZone());
manager.addEventListener(element, '^click', handler);
DOM.dispatchEvent(child, dispatchedEvent);
@ -89,7 +89,7 @@ export function main() {
var dispatchedEvent = DOM.createMouseEvent('click');
var receivedEvent = null;
var handler = (e) => { receivedEvent = e; };
var manager = new EventManager([domEventPlugin], new FakeVmTurnZone());
var manager = new EventManager([domEventPlugin], new FakeNgZone());
var remover = manager.addGlobalEventListener("document", '^click', handler);
DOM.dispatchEvent(element, dispatchedEvent);
@ -130,7 +130,7 @@ class FakeEventManagerPlugin extends EventManagerPlugin {
}
}
class FakeVmTurnZone extends VmTurnZone {
class FakeNgZone extends NgZone {
constructor() {
super({enableLongStackTrace: false});
}

View File

@ -22,7 +22,7 @@ module.exports = function makeNodeTree(destinationPath) {
include: ['angular2/**', 'benchpress/**', 'rtts_assert/**', '**/e2e_test/**'],
exclude: [
// the following code and tests are not compatible with CJS/node environment
'angular2/src/core/zone/vm_turn_zone.es6',
'angular2/src/core/zone/ng_zone.es6',
'angular2/test/core/zone/**'
]
});