Skip to content

Commit

Permalink
fix(patch): fix angular#998, patch mediaQuery for new Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Feb 9, 2018
1 parent 42b9edb commit 4c1d4a9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
59 changes: 55 additions & 4 deletions lib/browser/webapis-media-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,60 @@
* found in the LICENSE file at https://angular.io/license
*/
Zone.__load_patch('mediaQuery', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
if (!global['MediaQueryList']) {
return;
function patchAddListener(proto: any) {
api.patchMethod(proto, 'addListener', (delegate: Function) => (self: any, args: any[]) => {
const callback = args.length > 0 ? args[0] : null;
if (typeof callback === 'function') {
const wrapperedCallback = Zone.current.wrap(callback, 'MediaQuery');
callback[api.symbol('mediaQueryCallback')] = wrapperedCallback;
return delegate.call(self, wrapperedCallback);
} else {
return delegate.apply(self, args);
}
});
}

function patchRemoveListener(proto: any) {
api.patchMethod(proto, 'removeListener', (delegate: Function) => (self: any, args: any[]) => {
const callback = args.length > 0 ? args[0] : null;
if (typeof callback === 'function') {
const wrapperedCallback = callback[api.symbol('mediaQueryCallback')];
if (wrapperedCallback) {
return delegate.call(self, wrapperedCallback);
} else {
return delegate.apply(self, args);
}
} else {
return delegate.apply(self, args);
}
});
}

if (global['MediaQueryList']) {
const proto = global['MediaQueryList'].prototype;
patchAddListener(proto);
patchRemoveListener(proto);
} else if (global['matchMedia']) {
api.patchMethod(global, 'matchMedia', (delegate: Function) => (self: any, args: any[]) => {
const mql = delegate.apply(self, args);
if (mql) {
// try to patch MediaQueryList.prototype
const proto = Object.getPrototypeOf(mql);
if (proto && proto['addListener']) {
// try to patch proto, don't need to worry about patch
// multiple times, because, api.patchEventTarget will check it
patchAddListener(proto);
patchRemoveListener(proto);
patchAddListener(mql);
patchRemoveListener(mql);
} else if (mql['addListener']) {
// proto not exists, or proto has no addListener method
// try to patch mql instance
patchAddListener(mql);
patchRemoveListener(mql);
}
}
return mql;
});
}
api.patchEventTarget(
global, [global['MediaQueryList'].prototype], {add: 'addListener', rm: 'removeListener'});
});
2 changes: 1 addition & 1 deletion test/test-env-setup-jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* found in the LICENSE file at https://angular.io/license
*/

(<any>jasmine).DEFAULT_TIMEOUT_INTERVAL = 2000;
(<any>jasmine).DEFAULT_TIMEOUT_INTERVAL = 5000;
import '../lib/jasmine/jasmine';

0 comments on commit 4c1d4a9

Please sign in to comment.