@@ -19,19 +19,23 @@ export class NgZone {
1919 // onTurnDone hook at the end of the current VM turn.
2020 _innerZone ;
2121
22- _onTurnStart :Function ;
23- _onTurnDone :Function ;
24- _onErrorHandler :Function ;
22+ _onTurnStart : ( ) => void ;
23+ _onTurnDone : ( ) => void ;
24+ _onErrorHandler : ( error , stack ) => void ;
2525
2626 // Number of microtasks pending from _innerZone (& descendants)
27- _pendingMicrotask : number ;
27+ _pendingMicrotasks : number ;
2828 // Whether some code has been executed in the _innerZone (& descendants) in the current turn
2929 _hasExecutedCodeInInnerZone : boolean ;
3030 // run() call depth in _mountZone. 0 at the end of a macrotask
3131 // zone.run(() => { // top-level call
3232 // zone.run(() => {}); // nested call -> in-turn
3333 // });
3434 _nestedRun : number ;
35+
36+ // TODO(vicb): implement this class properly for node.js environment
37+ // This disabled flag is only here to please cjs tests
38+ _disabled : boolean ;
3539
3640 /**
3741 * Associates with this
@@ -50,19 +54,31 @@ export class NgZone {
5054 this . _pendingMicrotasks = 0 ;
5155 this . _hasExecutedCodeInInnerZone = false ;
5256 this . _nestedRun = 0 ;
53-
54- this . _mountZone = global . zone ;
55- this . _innerZone = this . _createInnerZone ( this . _mountZone , enableLongStackTrace )
57+
58+ if ( global . zone ) {
59+ this . _disabled = false ;
60+ this . _mountZone = global . zone ;
61+ this . _innerZone = this . _createInnerZone ( this . _mountZone , enableLongStackTrace )
62+ } else {
63+ this . _disabled = true ;
64+ this . _mountZone = null ;
65+ }
5666 }
5767
5868 /**
5969 * Initializes the zone hooks.
6070 *
61- * @param {Function } onTurnStart called before code executes in the inner zone for each VM turn
62- * @param {Function } onTurnDone called at the end of a VM turn if code has executed in the inner zone
63- * @param {Function } onErrorHandler called when an exception is thrown by a macro or micro task
71+ * @param {() => void } onTurnStart called before code executes in the inner zone for each VM turn
72+ * @param {() => void } onTurnDone called at the end of a VM turn if code has executed in the inner
73+ * zone
74+ * @param {(error, stack) => void } onErrorHandler called when an exception is thrown by a macro or
75+ * micro task
6476 */
65- initCallbacks ( { onTurnStart, onTurnDone, onErrorHandler} = { } ) {
77+ initCallbacks ( { onTurnStart, onTurnDone, onErrorHandler} : {
78+ onTurnStart ?: ( ) => void ,
79+ onTurnDone ?: ( ) => void ,
80+ onErrorHandler ?: ( error , stack ) => void
81+ } = { } ) {
6682 this . _onTurnStart = normalizeBlank ( onTurnStart ) ;
6783 this . _onTurnDone = normalizeBlank ( onTurnDone ) ;
6884 this . _onErrorHandler = normalizeBlank ( onErrorHandler ) ;
@@ -78,12 +94,17 @@ export class NgZone {
7894 * var zone: NgZone = [ref to the application zone];
7995 *
8096 * zone.run(() => {
81- * // the change detection will run after this function and the microtasks it enqueues have executed.
97+ * // the change detection will run after this function and the microtasks it enqueues have
98+ * executed.
8299 * });
83100 * ```
84101 */
85- run ( fn ) {
86- return this . _innerZone . run ( fn ) ;
102+ run ( fn ) {
103+ if ( this . _disabled ) {
104+ return fn ( ) ;
105+ } else {
106+ return this . _innerZone . run ( fn ) ;
107+ }
87108 }
88109
89110 /**
@@ -102,30 +123,28 @@ export class NgZone {
102123 * });
103124 * ```
104125 */
105- runOutsideAngular ( fn ) {
106- return this . _mountZone . run ( fn ) ;
126+ runOutsideAngular ( fn ) {
127+ if ( this . _disabled ) {
128+ return fn ( ) ;
129+ } else {
130+ return this . _mountZone . run ( fn ) ;
131+ }
107132 }
108133
109134 _createInnerZone ( zone , enableLongStackTrace ) {
110135 var ngZone = this ;
111136 var errorHandling ;
112137
113138 if ( enableLongStackTrace ) {
114- errorHandling = StringMapWrapper . merge ( Zone . longStackTraceZone , {
115- onError : function ( e ) {
116- ngZone . _onError ( this , e )
117- }
118- } ) ;
139+ errorHandling = StringMapWrapper . merge ( Zone . longStackTraceZone ,
140+ { onError : function ( e ) { ngZone . _onError ( this , e ) } } ) ;
119141 } else {
120142 errorHandling = {
121- onError : function ( e ) {
122- ngZone . _onError ( this , e )
123- }
143+ onError : function ( e ) { ngZone . _onError ( this , e ) }
124144 } ;
125145 }
126146
127- return zone
128- . fork ( errorHandling )
147+ return zone . fork ( errorHandling )
129148 . fork ( {
130149 '$run' : function ( parentRun ) {
131150 return function ( ) {
@@ -140,8 +159,10 @@ export class NgZone {
140159 return parentRun . apply ( this , arguments ) ;
141160 } finally {
142161 ngZone . _nestedRun -- ;
143- // If there are no more pending microtasks, we are at the end of a VM turn (or in onTurnStart)
144- // _nestedRun will be 0 at the end of a macrotasks (it could be > 0 when there are nested calls
162+ // If there are no more pending microtasks, we are at the end of a VM turn (or in
163+ // onTurnStart)
164+ // _nestedRun will be 0 at the end of a macrotasks (it could be > 0 when there are
165+ // nested calls
145166 // to run()).
146167 if ( ngZone . _pendingMicrotasks == 0 && ngZone . _nestedRun == 0 ) {
147168 if ( ngZone . _onTurnDone && ngZone . _hasExecutedCodeInInnerZone ) {
0 commit comments