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

Commit c7c7db5

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(patch): fix #998, patch mediaQuery for new Safari (#1003)
1 parent 008fd43 commit c7c7db5

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

lib/browser/webapis-media-query.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,60 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
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+
});
1164
}
12-
api.patchEventTarget(
13-
global, [global['MediaQueryList'].prototype], {add: 'addListener', rm: 'removeListener'});
1465
});

test/test-env-setup-jasmine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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

0 commit comments

Comments
 (0)