[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;
const callback = args.length > 0 ? args[0] : null; api.ObjectDefineProperty(chrome.runtime.onMessage, "addListener", {
if (typeof callback === "function") { value: function (...args: any[]) {
const wrapperedCallback = Zone.current.wrap(callback, "ChromeRuntimeOnMessage"); const callback = args.length > 0 ? args[0] : null;
callback[api.symbol("chromeRuntimeOnMessageCallback")] = wrapperedCallback; if (typeof callback === "function") {
return delegate.call(self, wrapperedCallback); const wrapperedCallback = Zone.current.wrap(callback, "ChromeRuntimeOnMessage");
} else { callback[api.symbol("chromeRuntimeOnMessageCallback")] = wrapperedCallback;
return delegate.apply(self, args); return nativeAddListener.call(onMessage, wrapperedCallback);
} } else {
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;
const callback = args.length > 0 ? args[0] : null; api.ObjectDefineProperty(chrome.runtime.onMessage, "removeListener", {
if (typeof callback === "function") { value: function (...args: any[]) {
const wrapperedCallback = callback[api.symbol("chromeRuntimeOnMessageCallback")]; const callback = args.length > 0 ? args[0] : null;
if (wrapperedCallback) { if (typeof callback === "function") {
return delegate.call(self, wrapperedCallback); const wrapperedCallback = callback[api.symbol("chromeRuntimeOnMessageCallback")];
if (wrapperedCallback) {
return nativeRemoveListener.call(onMessage, wrapperedCallback);
} else {
return nativeRemoveListener.apply(onMessage, args);
}
} else { } else {
return delegate.apply(self, args); return nativeRemoveListener.apply(onMessage, args);
} }
} else { },
return delegate.apply(self, args); writable: false,
}
}); });
}); });