Skip to content

Commit 4d19d31

Browse files
committed
fix(footer): fixing memory leak from rapid keyboard mount/unmount
1 parent 9c10dbe commit 4d19d31

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

core/src/components/footer/footer.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class Footer implements ComponentInterface {
2222
private scrollEl?: HTMLElement;
2323
private contentScrollCallback?: () => void;
2424
private keyboardCtrl: KeyboardController | null = null;
25+
private keyboardCtrlPromise: Promise<KeyboardController> | null = null;
2526

2627
@State() private keyboardVisible = false;
2728

@@ -52,7 +53,7 @@ export class Footer implements ComponentInterface {
5253
}
5354

5455
async connectedCallback() {
55-
this.keyboardCtrl = await createKeyboardController(async (keyboardOpen, waitForResize) => {
56+
const promise = createKeyboardController(async (keyboardOpen, waitForResize) => {
5657
/**
5758
* If the keyboard is hiding, then we need to wait
5859
* for the webview to resize. Otherwise, the footer
@@ -64,11 +65,32 @@ export class Footer implements ComponentInterface {
6465

6566
this.keyboardVisible = keyboardOpen; // trigger re-render by updating state
6667
});
68+
this.keyboardCtrlPromise = promise;
69+
70+
const keyboardCtrl = await promise;
71+
72+
/**
73+
* Only assign if this is still the current promise.
74+
* Otherwise, a new connectedCallback has started or
75+
* disconnectedCallback was called, so destroy this instance.
76+
*/
77+
if (this.keyboardCtrlPromise === promise) {
78+
this.keyboardCtrl = keyboardCtrl;
79+
this.keyboardCtrlPromise = null;
80+
} else {
81+
keyboardCtrl.destroy();
82+
}
6783
}
6884

6985
disconnectedCallback() {
86+
if (this.keyboardCtrlPromise) {
87+
this.keyboardCtrlPromise.then((ctrl) => ctrl.destroy());
88+
this.keyboardCtrlPromise = null;
89+
}
90+
7091
if (this.keyboardCtrl) {
7192
this.keyboardCtrl.destroy();
93+
this.keyboardCtrl = null;
7294
}
7395
}
7496

0 commit comments

Comments
 (0)