[PM-4401] Fix zone.js patch compatibility issues in safari (#6633)

* [PM-4401] fix: zone.js patch compatibility issues in safari

* Update apps/browser/src/platform/polyfills/zone-patch-chrome-runtime.ts

Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>

---------

Co-authored-by: Oscar Hinton <Hinton@users.noreply.github.com>
Co-authored-by: SmithThe4th <gsmith@bitwarden.com>
This commit is contained in:
Andreas Coroiu 2023-10-19 20:21:53 +02:00 committed by GitHub
parent e357819251
commit 790d666929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 20 deletions

View File

@ -2,35 +2,43 @@
* Monkey patch `chrome.runtime.onMessage` event listeners to run in the Angular zone. * Monkey patch `chrome.runtime.onMessage` event listeners to run in the Angular zone.
*/ */
Zone.__load_patch("ChromeRuntimeOnMessage", (global: any, Zone: ZoneType, api: _ZonePrivate) => { Zone.__load_patch("ChromeRuntimeOnMessage", (global: any, Zone: ZoneType, api: _ZonePrivate) => {
const onMessage = global.chrome.runtime.onMessage;
if (typeof global?.chrome?.runtime?.onMessage === "undefined") { if (typeof global?.chrome?.runtime?.onMessage === "undefined") {
return; return;
} }
const onMessage = global.chrome.runtime.onMessage;
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
api.patchMethod(onMessage, "addListener", (delegate: Function) => (self: any, args: any[]) => { const nativeAddListener = onMessage.addListener as Function;
api.ObjectDefineProperty(chrome.runtime.onMessage, "addListener", {
value: function (...args: any[]) {
const callback = args.length > 0 ? args[0] : null; const callback = args.length > 0 ? args[0] : null;
if (typeof callback === "function") { if (typeof callback === "function") {
const wrapperedCallback = Zone.current.wrap(callback, "ChromeRuntimeOnMessage"); const wrapperedCallback = Zone.current.wrap(callback, "ChromeRuntimeOnMessage");
callback[api.symbol("chromeRuntimeOnMessageCallback")] = wrapperedCallback; callback[api.symbol("chromeRuntimeOnMessageCallback")] = wrapperedCallback;
return delegate.call(self, wrapperedCallback); return nativeAddListener.call(onMessage, wrapperedCallback);
} else { } else {
return delegate.apply(self, args); return nativeAddListener.apply(onMessage, args);
} }
},
writable: false,
}); });
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
api.patchMethod(onMessage, "removeListener", (delegate: Function) => (self: any, args: any[]) => { const nativeRemoveListener = onMessage.removeListener as Function;
api.ObjectDefineProperty(chrome.runtime.onMessage, "removeListener", {
value: function (...args: any[]) {
const callback = args.length > 0 ? args[0] : null; const callback = args.length > 0 ? args[0] : null;
if (typeof callback === "function") { if (typeof callback === "function") {
const wrapperedCallback = callback[api.symbol("chromeRuntimeOnMessageCallback")]; const wrapperedCallback = callback[api.symbol("chromeRuntimeOnMessageCallback")];
if (wrapperedCallback) { if (wrapperedCallback) {
return delegate.call(self, wrapperedCallback); return nativeRemoveListener.call(onMessage, wrapperedCallback);
} else { } else {
return delegate.apply(self, args); return nativeRemoveListener.apply(onMessage, args);
} }
} else { } else {
return delegate.apply(self, args); return nativeRemoveListener.apply(onMessage, args);
} }
},
writable: false,
}); });
}); });