Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(angular): run platform subscriptions inside zone #28404

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/angular/common/src/providers/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ export class Platform {
});
};

proxyEvent(this.pause, doc, 'pause');
proxyEvent(this.resume, doc, 'resume');
proxyEvent(this.backButton, doc, 'ionBackButton');
proxyEvent(this.resize, this.win, 'resize');
proxyEvent(this.keyboardDidShow, this.win, 'ionKeyboardDidShow');
proxyEvent(this.keyboardDidHide, this.win, 'ionKeyboardDidHide');
proxyEvent(this.pause, doc, 'pause', zone);
proxyEvent(this.resume, doc, 'resume', zone);
proxyEvent(this.backButton, doc, 'ionBackButton', zone);
proxyEvent(this.resize, this.win, 'resize', zone);
proxyEvent(this.keyboardDidShow, this.win, 'ionKeyboardDidShow', zone);
proxyEvent(this.keyboardDidHide, this.win, 'ionKeyboardDidHide', zone);

let readyResolve: (value: string) => void;
this._readyPromise = new Promise((res) => {
Expand Down Expand Up @@ -262,12 +262,20 @@ const readQueryParam = (url: string, key: string) => {
return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null;
};

const proxyEvent = <T>(emitter: Subject<T>, el: EventTarget, eventName: string) => {
const proxyEvent = <T>(emitter: Subject<T>, el: EventTarget, eventName: string, zone: NgZone) => {
if (el) {
el.addEventListener(eventName, (ev) => {
// ?? cordova might emit "null" events
const value = ev != null ? (ev as any).detail : undefined;
emitter.next(value);
/**
* `zone.run` is required to make sure that we are running inside the Angular zone
* at all times. This is necessary since an app that has Capacitor will
* override the `document.addEventListener` with its own implementation.
* The override causes the event to no longer be in the Angular zone.
*/
zone.run(() => {
// ?? cordova might emit "null" events
const value = ev != null ? (ev as any).detail : undefined;
emitter.next(value);
});
});
}
};
Loading