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

Commit

Permalink
feat(patch): load non standard api with new load module method (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed May 2, 2017
1 parent bb6aadc commit 97c03b5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 56 deletions.
67 changes: 31 additions & 36 deletions lib/browser/webapis-media-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,35 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
((_global: any) => {
// patch MediaQuery
patchMediaQuery(_global);

function patchMediaQuery(_global: any) {
if (!_global['MediaQueryList']) {
return;
}
const patchEventTargetMethods =
(Zone as any)[(Zone as any).__symbol__('patchEventTargetMethods')];
patchEventTargetMethods(
_global['MediaQueryList'].prototype, 'addListener', 'removeListener',
(self: any, args: any[]) => {
return {
useCapturing: false,
eventName: 'mediaQuery',
handler: args[0],
target: self || _global,
name: 'mediaQuery',
invokeAddFunc: function(addFnSymbol: any, delegate: any) {
if (delegate && (<Task>delegate).invoke) {
return this.target[addFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[addFnSymbol](delegate);
}
},
invokeRemoveFunc: function(removeFnSymbol: any, delegate: any) {
if (delegate && (<Task>delegate).invoke) {
return this.target[removeFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[removeFnSymbol](delegate);
}
}
};
});
Zone.__load_patch('mediaQuery', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
if (!global['MediaQueryList']) {
return;
}
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
const patchEventTargetMethods =
(Zone as any)[(Zone as any).__symbol__('patchEventTargetMethods')];
patchEventTargetMethods(
_global['MediaQueryList'].prototype, 'addListener', 'removeListener',
(self: any, args: any[]) => {
return {
useCapturing: false,
eventName: 'mediaQuery',
handler: args[0],
target: self || global,
name: 'mediaQuery',
invokeAddFunc: function(addFnSymbol: any, delegate: any) {
if (delegate && (<Task>delegate).invoke) {
return this.target[addFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[addFnSymbol](delegate);
}
},
invokeRemoveFunc: function(removeFnSymbol: any, delegate: any) {
if (delegate && (<Task>delegate).invoke) {
return this.target[removeFnSymbol]((<Task>delegate).invoke);
} else {
return this.target[removeFnSymbol](delegate);
}
}
};
});
});
27 changes: 11 additions & 16 deletions lib/browser/webapis-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
((_global: any) => {
// patch Notification
patchNotification(_global);

function patchNotification(_global: any) {
const Notification = _global['Notification'];
if (!Notification || !Notification.prototype) {
return;
}
const desc = Object.getOwnPropertyDescriptor(Notification.prototype, 'onerror');
if (!desc || !desc.configurable) {
return;
}
const patchOnProperties = (Zone as any)[(Zone as any).__symbol__('patchOnProperties')];
patchOnProperties(Notification.prototype, null);
Zone.__load_patch('notification', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
const Notification = _global['Notification'];
if (!Notification || !Notification.prototype) {
return;
}
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
const desc = Object.getOwnPropertyDescriptor(Notification.prototype, 'onerror');
if (!desc || !desc.configurable) {
return;
}
const patchOnProperties = (Zone as any)[(Zone as any).__symbol__('patchOnProperties')];
patchOnProperties(Notification.prototype, null);
});
7 changes: 3 additions & 4 deletions lib/extra/bluebird.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
(() => {
const __symbol__ = (Zone as any).__symbol__;
Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// TODO: @JiaLiPassion, we can automatically patch bluebird
// if global.Promise = Bluebird, but sometimes in nodejs,
// global.Promise is not Bluebird, and Bluebird is just be
// used by other libraries such as sequelize, so I think it is
// safe to just expose a method to patch Bluebird explicitly
(Zone as any)[__symbol__('bluebird')] = function patchBluebird(Bluebird: any) {
(Zone as any)[Zone.__symbol__('bluebird')] = function patchBluebird(Bluebird: any) {
Bluebird.setScheduler((fn: Function) => {
Zone.current.scheduleMicroTask('bluebird', fn);
});
};
})();
});

0 comments on commit 97c03b5

Please sign in to comment.