|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 | Zone.__load_patch('mediaQuery', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
|
9 |
| - if (!global['MediaQueryList']) { |
10 |
| - return; |
| 9 | + function patchAddListener(proto: any) { |
| 10 | + api.patchMethod(proto, 'addListener', (delegate: Function) => (self: any, args: any[]) => { |
| 11 | + const callback = args.length > 0 ? args[0] : null; |
| 12 | + if (typeof callback === 'function') { |
| 13 | + const wrapperedCallback = Zone.current.wrap(callback, 'MediaQuery'); |
| 14 | + callback[api.symbol('mediaQueryCallback')] = wrapperedCallback; |
| 15 | + return delegate.call(self, wrapperedCallback); |
| 16 | + } else { |
| 17 | + return delegate.apply(self, args); |
| 18 | + } |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + function patchRemoveListener(proto: any) { |
| 23 | + api.patchMethod(proto, 'removeListener', (delegate: Function) => (self: any, args: any[]) => { |
| 24 | + const callback = args.length > 0 ? args[0] : null; |
| 25 | + if (typeof callback === 'function') { |
| 26 | + const wrapperedCallback = callback[api.symbol('mediaQueryCallback')]; |
| 27 | + if (wrapperedCallback) { |
| 28 | + return delegate.call(self, wrapperedCallback); |
| 29 | + } else { |
| 30 | + return delegate.apply(self, args); |
| 31 | + } |
| 32 | + } else { |
| 33 | + return delegate.apply(self, args); |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + if (global['MediaQueryList']) { |
| 39 | + const proto = global['MediaQueryList'].prototype; |
| 40 | + patchAddListener(proto); |
| 41 | + patchRemoveListener(proto); |
| 42 | + } else if (global['matchMedia']) { |
| 43 | + api.patchMethod(global, 'matchMedia', (delegate: Function) => (self: any, args: any[]) => { |
| 44 | + const mql = delegate.apply(self, args); |
| 45 | + if (mql) { |
| 46 | + // try to patch MediaQueryList.prototype |
| 47 | + const proto = Object.getPrototypeOf(mql); |
| 48 | + if (proto && proto['addListener']) { |
| 49 | + // try to patch proto, don't need to worry about patch |
| 50 | + // multiple times, because, api.patchEventTarget will check it |
| 51 | + patchAddListener(proto); |
| 52 | + patchRemoveListener(proto); |
| 53 | + patchAddListener(mql); |
| 54 | + patchRemoveListener(mql); |
| 55 | + } else if (mql['addListener']) { |
| 56 | + // proto not exists, or proto has no addListener method |
| 57 | + // try to patch mql instance |
| 58 | + patchAddListener(mql); |
| 59 | + patchRemoveListener(mql); |
| 60 | + } |
| 61 | + } |
| 62 | + return mql; |
| 63 | + }); |
11 | 64 | }
|
12 |
| - api.patchEventTarget( |
13 |
| - global, [global['MediaQueryList'].prototype], {add: 'addListener', rm: 'removeListener'}); |
14 | 65 | });
|
0 commit comments