Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 6b70d2f

Browse files
committed
add reload/unload methods
1 parent 1da514f commit 6b70d2f

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

lib/browser/register-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function patchCallbacks(target: any, targetName: string, method: string, callbac
3838
return nativeDelegate.call(target, name, opts, options);
3939
};
4040

41-
attachOriginToPatched(target[method], nativeDelegate);
41+
attachOriginToPatched(target, method, nativeDelegate);
4242
}
4343

4444
export function registerElementPatch(_global: any) {

lib/common/events.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,13 @@ export function patchEventTarget(
617617
};
618618

619619
// for native toString patch
620-
attachOriginToPatched(proto[ADD_EVENT_LISTENER], nativeAddEventListener);
621-
attachOriginToPatched(proto[REMOVE_EVENT_LISTENER], nativeRemoveEventListener);
620+
attachOriginToPatched(proto, ADD_EVENT_LISTENER, nativeAddEventListener);
621+
attachOriginToPatched(proto, REMOVE_EVENT_LISTENER, nativeRemoveEventListener);
622622
if (nativeRemoveAllListeners) {
623-
attachOriginToPatched(proto[REMOVE_ALL_LISTENERS_EVENT_LISTENER], nativeRemoveAllListeners);
623+
attachOriginToPatched(proto, REMOVE_ALL_LISTENERS_EVENT_LISTENER, nativeRemoveAllListeners);
624624
}
625625
if (nativeListeners) {
626-
attachOriginToPatched(proto[LISTENERS_EVENT_LISTENER], nativeListeners);
626+
attachOriginToPatched(proto, LISTENERS_EVENT_LISTENER, nativeListeners);
627627
}
628628
return true;
629629
}

lib/zone.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,14 +700,37 @@ const Zone: ZoneType = (function(global: any) {
700700
if (_mode === 'normal') {
701701
patches[name] = fn(global, Zone, _api);
702702
} else {
703-
patches[name] = function () {
703+
patches[name] = function() {
704704
fn(global, Zone, _api);
705705
};
706706
}
707707
performanceMeasure(perfName, perfName);
708708
}
709709
}
710710

711+
static __load() {
712+
Object.keys(patches).forEach(key => patches[key]());
713+
patchLoaded = true;
714+
}
715+
716+
static __register_patched_delegate(proto: any, property: string, origin: any) {
717+
delegates.push({proto: proto, property: property, patched: proto[property], origin: origin});
718+
}
719+
720+
static __reloadAll() {
721+
delegates.forEach(delegate => {
722+
delegate.proto[delegate.property] = delegate.patched;
723+
});
724+
patchLoaded = true;
725+
}
726+
727+
static __unloadAll() {
728+
delegates.forEach(delegate => {
729+
delegate.proto[delegate.property] = delegate.origin;
730+
});
731+
patchLoaded = false;
732+
}
733+
711734
public get parent(): AmbientZone|null {
712735
return this._parent;
713736
}
@@ -765,18 +788,28 @@ const Zone: ZoneType = (function(global: any) {
765788
public run(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any;
766789
public run<T>(
767790
callback: (...args: any[]) => T, applyThis?: any, applyArgs?: any[], source?: string): T {
791+
if (!patchLoaded && _mode === 'lazy') {
792+
Zone.__reloadAll();
793+
}
768794
_currentZoneFrame = {parent: _currentZoneFrame, zone: this};
769795
try {
770796
return this._zoneDelegate.invoke(this, callback, applyThis, applyArgs, source);
771797
} finally {
772798
_currentZoneFrame = _currentZoneFrame.parent!;
799+
if (_mode === 'lazy' && _currentZoneFrame.zone &&
800+
_currentZoneFrame.zone.name === '<root>') {
801+
Zone.__unloadAll();
802+
}
773803
}
774804
}
775805

776806
public runGuarded(callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any;
777807
public runGuarded<T>(
778808
callback: (...args: any[]) => T, applyThis: any = null, applyArgs?: any[],
779809
source?: string) {
810+
if (!patchLoaded && _mode === 'lazy') {
811+
Zone.__reloadAll();
812+
}
780813
_currentZoneFrame = {parent: _currentZoneFrame, zone: this};
781814
try {
782815
try {
@@ -788,6 +821,10 @@ const Zone: ZoneType = (function(global: any) {
788821
}
789822
} finally {
790823
_currentZoneFrame = _currentZoneFrame.parent!;
824+
if (_mode === 'lazy' && _currentZoneFrame.zone &&
825+
_currentZoneFrame.zone.name === '<root>') {
826+
Zone.__unloadAll();
827+
}
791828
}
792829
}
793830

@@ -811,6 +848,9 @@ const Zone: ZoneType = (function(global: any) {
811848
task.runCount++;
812849
const previousTask = _currentTask;
813850
_currentTask = task;
851+
if (!patchLoaded && _mode === 'lazy') {
852+
Zone.__reloadAll();
853+
}
814854
_currentZoneFrame = {parent: _currentZoneFrame, zone: this};
815855
try {
816856
if (task.type == macroTask && task.data && !task.data.isPeriodic) {
@@ -838,6 +878,10 @@ const Zone: ZoneType = (function(global: any) {
838878
}
839879
_currentZoneFrame = _currentZoneFrame.parent!;
840880
_currentTask = previousTask;
881+
if (_mode === 'lazy' && _currentZoneFrame.zone &&
882+
_currentZoneFrame.zone.name === '<root>') {
883+
Zone.__unloadAll();
884+
}
841885
}
842886
}
843887

@@ -1337,6 +1381,8 @@ const Zone: ZoneType = (function(global: any) {
13371381
eventTask: 'eventTask' = 'eventTask';
13381382

13391383
const patches: {[key: string]: any} = {};
1384+
const delegates: {proto: any, property: string, patched: any, origin: any}[] = [];
1385+
let patchLoaded = false;
13401386
const _api: _ZonePrivate = {
13411387
symbol: __symbol__,
13421388
currentZoneFrame: () => _currentZoneFrame,
@@ -1361,7 +1407,7 @@ const Zone: ZoneType = (function(global: any) {
13611407
let _currentZoneFrame: _ZoneFrame = {parent: null, zone: new Zone(null, null)};
13621408
let _currentTask: Task|null = null;
13631409
let _numberOfNestedTaskFrames = 0;
1364-
let _mode: 'lazy' | 'normal' = 'normal';
1410+
let _mode: 'lazy'|'normal' = 'normal';
13651411

13661412
function noop() {}
13671413

0 commit comments

Comments
 (0)