Skip to content

Commit a4b303e

Browse files
authored
fix(angular): run platform subscriptions inside zone (#28404)
Issue number: #19539 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> When an app uses Capacitor, then the platform subscriptions will run outside of the Angular Zone. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - The platform subscriptions will run inside of the Angular Zone regardless if it uses Capacitor or not. Added an extra `zone.run` within the event listener. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `npm install @ionic/[email protected]`
1 parent 34257d6 commit a4b303e

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

Diff for: packages/angular/common/src/providers/platform.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ export class Platform {
6868
});
6969
};
7070

71-
proxyEvent(this.pause, doc, 'pause');
72-
proxyEvent(this.resume, doc, 'resume');
73-
proxyEvent(this.backButton, doc, 'ionBackButton');
74-
proxyEvent(this.resize, this.win, 'resize');
75-
proxyEvent(this.keyboardDidShow, this.win, 'ionKeyboardDidShow');
76-
proxyEvent(this.keyboardDidHide, this.win, 'ionKeyboardDidHide');
71+
proxyEvent(this.pause, doc, 'pause', zone);
72+
proxyEvent(this.resume, doc, 'resume', zone);
73+
proxyEvent(this.backButton, doc, 'ionBackButton', zone);
74+
proxyEvent(this.resize, this.win, 'resize', zone);
75+
proxyEvent(this.keyboardDidShow, this.win, 'ionKeyboardDidShow', zone);
76+
proxyEvent(this.keyboardDidHide, this.win, 'ionKeyboardDidHide', zone);
7777

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

265-
const proxyEvent = <T>(emitter: Subject<T>, el: EventTarget, eventName: string) => {
265+
const proxyEvent = <T>(emitter: Subject<T>, el: EventTarget, eventName: string, zone: NgZone) => {
266266
if (el) {
267267
el.addEventListener(eventName, (ev) => {
268-
// ?? cordova might emit "null" events
269-
const value = ev != null ? (ev as any).detail : undefined;
270-
emitter.next(value);
268+
/**
269+
* `zone.run` is required to make sure that we are running inside the Angular zone
270+
* at all times. This is necessary since an app that has Capacitor will
271+
* override the `document.addEventListener` with its own implementation.
272+
* The override causes the event to no longer be in the Angular zone.
273+
*/
274+
zone.run(() => {
275+
// ?? cordova might emit "null" events
276+
const value = ev != null ? (ev as any).detail : undefined;
277+
emitter.next(value);
278+
});
271279
});
272280
}
273281
};

0 commit comments

Comments
 (0)