From 97c03b533d83c71f3ac00cc5fe9474833037f7f6 Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Wed, 3 May 2017 03:09:02 +0900 Subject: [PATCH] feat(patch): load non standard api with new load module method (#764) --- lib/browser/webapis-media-query.ts | 67 +++++++++++++---------------- lib/browser/webapis-notification.ts | 27 +++++------- lib/extra/bluebird.ts | 7 ++- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/lib/browser/webapis-media-query.ts b/lib/browser/webapis-media-query.ts index 2e85c64cc..e37dbfc61 100644 --- a/lib/browser/webapis-media-query.ts +++ b/lib/browser/webapis-media-query.ts @@ -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 && (delegate).invoke) { - return this.target[addFnSymbol]((delegate).invoke); - } else { - return this.target[addFnSymbol](delegate); - } - }, - invokeRemoveFunc: function(removeFnSymbol: any, delegate: any) { - if (delegate && (delegate).invoke) { - return this.target[removeFnSymbol]((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); \ No newline at end of file + 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 && (delegate).invoke) { + return this.target[addFnSymbol]((delegate).invoke); + } else { + return this.target[addFnSymbol](delegate); + } + }, + invokeRemoveFunc: function(removeFnSymbol: any, delegate: any) { + if (delegate && (delegate).invoke) { + return this.target[removeFnSymbol]((delegate).invoke); + } else { + return this.target[removeFnSymbol](delegate); + } + } + }; + }); +}); \ No newline at end of file diff --git a/lib/browser/webapis-notification.ts b/lib/browser/webapis-notification.ts index ab01863f5..3dfad3b37 100644 --- a/lib/browser/webapis-notification.ts +++ b/lib/browser/webapis-notification.ts @@ -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); +}); \ No newline at end of file diff --git a/lib/extra/bluebird.ts b/lib/extra/bluebird.ts index 2ba34118c..093c88378 100644 --- a/lib/extra/bluebird.ts +++ b/lib/extra/bluebird.ts @@ -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); }); }; -})(); +}); \ No newline at end of file