@@ -154,6 +154,7 @@ interface Zone {
154
154
* @returns {any } The value for the key, or `undefined` if not found.
155
155
*/
156
156
get ( key : string ) : any ;
157
+
157
158
/**
158
159
* Returns a Zone which defines a `key`.
159
160
*
@@ -163,13 +164,15 @@ interface Zone {
163
164
* @returns {Zone } The Zone which defines the `key`, `null` if not found.
164
165
*/
165
166
getZoneWith ( key : string ) : Zone | null ;
167
+
166
168
/**
167
169
* Used to create a child zone.
168
170
*
169
171
* @param zoneSpec A set of rules which the child zone should follow.
170
172
* @returns {Zone } A new child zone.
171
173
*/
172
174
fork ( zoneSpec : ZoneSpec ) : Zone ;
175
+
173
176
/**
174
177
* Wraps a callback function in a new function which will properly restore the current zone upon
175
178
* invocation.
@@ -184,6 +187,7 @@ interface Zone {
184
187
* @returns {function(): * } A function which will invoke the `callback` through [Zone.runGuarded].
185
188
*/
186
189
wrap < F extends Function > ( callback : F , source : string ) : F ;
190
+
187
191
/**
188
192
* Invokes a function in a given zone.
189
193
*
@@ -196,6 +200,7 @@ interface Zone {
196
200
* @returns {any } Value from the `callback` function.
197
201
*/
198
202
run < T > ( callback : Function , applyThis ?: any , applyArgs ?: any [ ] , source ?: string ) : T ;
203
+
199
204
/**
200
205
* Invokes a function in a given zone and catches any exceptions.
201
206
*
@@ -211,6 +216,7 @@ interface Zone {
211
216
* @returns {any } Value from the `callback` function.
212
217
*/
213
218
runGuarded < T > ( callback : Function , applyThis ?: any , applyArgs ?: any [ ] , source ?: string ) : T ;
219
+
214
220
/**
215
221
* Execute the Task by restoring the [Zone.currentTask] in the Task's zone.
216
222
*
@@ -303,7 +309,7 @@ interface ZoneType {
303
309
root : Zone ;
304
310
305
311
/** @internal */
306
- __load_patch ( name : string , fn : _PatchFn ) : void ;
312
+ __load_patch ( name : string , fn : _PatchFn , preload ?: boolean ) : void ;
307
313
308
314
/** @internal */
309
315
__symbol__ ( name : string ) : string ;
@@ -488,14 +494,22 @@ interface ZoneSpec {
488
494
*/
489
495
interface ZoneDelegate {
490
496
zone : Zone ;
497
+
491
498
fork ( targetZone : Zone , zoneSpec : ZoneSpec ) : Zone ;
499
+
492
500
intercept ( targetZone : Zone , callback : Function , source : string ) : Function ;
501
+
493
502
invoke ( targetZone : Zone , callback : Function , applyThis ?: any , applyArgs ?: any [ ] , source ?: string ) :
494
503
any ;
504
+
495
505
handleError ( targetZone : Zone , error : any ) : boolean ;
506
+
496
507
scheduleTask ( targetZone : Zone , task : Task ) : Task ;
508
+
497
509
invokeTask ( targetZone : Zone , task : Task , applyThis ?: any , applyArgs ?: any [ ] ) : any ;
510
+
498
511
cancelTask ( targetZone : Zone , task : Task ) : any ;
512
+
499
513
hasTask ( targetZone : Zone , isEmpty : HasTaskState ) : void ;
500
514
}
501
515
@@ -637,12 +651,15 @@ type AmbientZoneDelegate = ZoneDelegate;
637
651
const Zone : ZoneType = ( function ( global : any ) {
638
652
const performance : { mark ( name : string ) : void ; measure ( name : string , label : string ) : void ; } =
639
653
global [ 'performance' ] ;
654
+
640
655
function mark ( name : string ) {
641
656
performance && performance [ 'mark' ] && performance [ 'mark' ] ( name ) ;
642
657
}
658
+
643
659
function performanceMeasure ( name : string , label : string ) {
644
660
performance && performance [ 'measure' ] && performance [ 'measure' ] ( name , label ) ;
645
661
}
662
+
646
663
mark ( 'Zone' ) ;
647
664
if ( global [ 'Zone' ] ) {
648
665
// if global['Zone'] already exists (maybe zone.js was already loaded or
@@ -666,7 +683,7 @@ const Zone: ZoneType = (function(global: any) {
666
683
static __symbol__ : ( name : string ) => string = __symbol__ ;
667
684
668
685
static assertZonePatched ( ) {
669
- if ( global [ 'Promise' ] !== patches [ 'ZoneAwarePromise' ] ) {
686
+ if ( patchLoaded && global [ 'Promise' ] !== patchedPromise ) {
670
687
throw new Error (
671
688
'Zone.js has detected that ZoneAwarePromise `(window|global).Promise` ' +
672
689
'has been overwritten.\n' +
@@ -692,42 +709,92 @@ const Zone: ZoneType = (function(global: any) {
692
709
return _currentTask ;
693
710
}
694
711
695
- static __load_patch ( name : string , fn : _PatchFn ) : void {
712
+ static get mode ( ) : 'lazy' | 'normal' {
713
+ return _mode ;
714
+ }
715
+
716
+ static set mode ( newMode : 'lazy' | 'normal' ) {
717
+ _mode = newMode ;
718
+ }
719
+
720
+ static __load_patch ( name : string , fn : _PatchFn , preload = false ) : void {
696
721
if ( patches . hasOwnProperty ( name ) ) {
697
722
throw Error ( 'Already loaded patch: ' + name ) ;
698
723
} else if ( ! global [ '__Zone_disable_' + name ] ) {
699
724
const perfName = 'Zone:' + name ;
700
725
mark ( perfName ) ;
701
- if ( _mode === 'normal' ) {
726
+ if ( _mode === 'normal' || preload ) {
702
727
patches [ name ] = fn ( global , Zone , _api ) ;
703
728
} else {
704
729
patches [ name ] = function ( ) {
705
- fn ( global , Zone , _api ) ;
730
+ return fn ( global , Zone , _api ) ;
706
731
} ;
707
732
}
708
733
performanceMeasure ( perfName , perfName ) ;
709
734
}
710
735
}
711
736
712
737
static __load ( ) {
713
- Object . keys ( patches ) . forEach ( key => patches [ key ] ( ) ) ;
738
+ if ( patchLoaded ) {
739
+ return ;
740
+ }
741
+ Object . keys ( patches ) . forEach ( key => {
742
+ const loadPatchFn = patches [ key ] ;
743
+ if ( typeof loadPatchFn === 'function' ) {
744
+ const r = loadPatchFn ( ) ;
745
+ if ( key === 'ZoneAwarePromise' ) {
746
+ patchedPromise = r ;
747
+ }
748
+ }
749
+ } ) ;
714
750
patchLoaded = true ;
715
751
}
716
752
717
- static __register_patched_delegate ( proto : any , property : string , origin : any ) {
718
- delegates . push ( { proto : proto , property : property , patched : proto [ property ] , origin : origin } ) ;
753
+ static __register_patched_delegate (
754
+ proto : any , property : string , origin : any , isPropertyDesc = false ) {
755
+ if ( ! isPropertyDesc ) {
756
+ delegates . push ( {
757
+ proto : proto ,
758
+ property : property ,
759
+ patched : proto [ property ] ,
760
+ origin : origin ,
761
+ isPropertyDesc : isPropertyDesc
762
+ } ) ;
763
+ } else {
764
+ delegates . push ( {
765
+ proto : proto ,
766
+ property : property ,
767
+ patched : Object . getOwnPropertyDescriptor ( proto , property ) ,
768
+ origin : origin ,
769
+ isPropertyDesc : isPropertyDesc
770
+ } ) ;
771
+ }
719
772
}
720
773
721
774
static __reloadAll ( ) {
775
+ if ( patchLoaded ) {
776
+ return ;
777
+ }
722
778
delegates . forEach ( delegate => {
723
- delegate . proto [ delegate . property ] = delegate . patched ;
779
+ if ( delegate . isPropertyDesc ) {
780
+ Object . defineProperty ( delegate . proto , delegate . property , delegate . patched ) ;
781
+ } else {
782
+ delegate . proto [ delegate . property ] = delegate . patched ;
783
+ }
724
784
} ) ;
725
785
patchLoaded = true ;
726
786
}
727
787
728
788
static __unloadAll ( ) {
789
+ if ( ! patchLoaded ) {
790
+ return ;
791
+ }
729
792
delegates . forEach ( delegate => {
730
- delegate . proto [ delegate . property ] = delegate . origin ;
793
+ if ( delegate . isPropertyDesc ) {
794
+ Object . defineProperty ( delegate . proto , delegate . property , delegate . origin ) ;
795
+ } else {
796
+ delegate . proto [ delegate . property ] = delegate . origin ;
797
+ }
731
798
} ) ;
732
799
patchLoaded = false ;
733
800
}
@@ -797,8 +864,7 @@ const Zone: ZoneType = (function(global: any) {
797
864
return this . _zoneDelegate . invoke ( this , callback , applyThis , applyArgs , source ) ;
798
865
} finally {
799
866
_currentZoneFrame = _currentZoneFrame . parent ! ;
800
- if ( _mode === 'lazy' && _currentZoneFrame . zone &&
801
- _currentZoneFrame . zone . name === '<root>' ) {
867
+ if ( _mode === 'lazy' && _currentZoneFrame . zone && ! _currentZoneFrame . zone . parent ) {
802
868
Zone . __unloadAll ( ) ;
803
869
}
804
870
}
@@ -822,8 +888,7 @@ const Zone: ZoneType = (function(global: any) {
822
888
}
823
889
} finally {
824
890
_currentZoneFrame = _currentZoneFrame . parent ! ;
825
- if ( _mode === 'lazy' && _currentZoneFrame . zone &&
826
- _currentZoneFrame . zone . name === '<root>' ) {
891
+ if ( _mode === 'lazy' && _currentZoneFrame . zone && ! _currentZoneFrame . zone . parent ) {
827
892
Zone . __unloadAll ( ) ;
828
893
}
829
894
}
@@ -879,8 +944,7 @@ const Zone: ZoneType = (function(global: any) {
879
944
}
880
945
_currentZoneFrame = _currentZoneFrame . parent ! ;
881
946
_currentTask = previousTask ;
882
- if ( _mode === 'lazy' && _currentZoneFrame . zone &&
883
- _currentZoneFrame . zone . name === '<root>' ) {
947
+ if ( _mode === 'lazy' && _currentZoneFrame . zone && ! _currentZoneFrame . zone . parent ) {
884
948
Zone . __unloadAll ( ) ;
885
949
}
886
950
}
@@ -1330,6 +1394,8 @@ const Zone: ZoneType = (function(global: any) {
1330
1394
if ( ! nativeMicroTaskQueuePromise ) {
1331
1395
if ( global [ symbolPromise ] ) {
1332
1396
nativeMicroTaskQueuePromise = global [ symbolPromise ] . resolve ( 0 ) ;
1397
+ } else if ( _mode === 'lazy' && patchedPromise !== global [ 'Promise' ] ) {
1398
+ nativeMicroTaskQueuePromise = global [ 'Promise' ] . resolve ( 0 ) ;
1333
1399
}
1334
1400
}
1335
1401
if ( nativeMicroTaskQueuePromise ) {
@@ -1382,7 +1448,9 @@ const Zone: ZoneType = (function(global: any) {
1382
1448
eventTask : 'eventTask' = 'eventTask' ;
1383
1449
1384
1450
const patches : { [ key : string ] : any } = { } ;
1385
- const delegates : { proto : any , property : string , patched : any , origin : any } [ ] = [ ] ;
1451
+ let patchedPromise : any ;
1452
+ const delegates :
1453
+ { proto : any , property : string , patched : any , origin : any , isPropertyDesc : boolean } [ ] = [ ] ;
1386
1454
let patchLoaded = false ;
1387
1455
const _api : _ZonePrivate = {
1388
1456
symbol : __symbol__ ,
@@ -1409,7 +1477,7 @@ const Zone: ZoneType = (function(global: any) {
1409
1477
let _currentZoneFrame : _ZoneFrame = { parent : null , zone : new Zone ( null , null ) } ;
1410
1478
let _currentTask : Task | null = null ;
1411
1479
let _numberOfNestedTaskFrames = 0 ;
1412
- let _mode : 'lazy' | 'normal' = 'normal' ;
1480
+ let _mode : 'lazy' | 'normal' = global [ '__zone_symbol__load_mode' ] || 'normal' ;
1413
1481
1414
1482
function noop ( ) { }
1415
1483
0 commit comments