Skip to content

Commit

Permalink
Prevent race in dialog box (#23758)
Browse files Browse the repository at this point in the history
Co-authored-by: Petar Petrov <MindFreeze@users.noreply.github.com>
Co-authored-by: Wendelin <12148533+wendevlin@users.noreply.github.com>
  • Loading branch information
3 people authored and piitaya committed Jan 23, 2025
1 parent ac98672 commit c6f0c62
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/dialogs/generic/dialog-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ class DialogBox extends LitElement {

@query("ha-md-dialog") private _dialog?: HaMdDialog;

private _closePromise?: Promise<void>;

private _closeResolve?: () => void;

public async showDialog(params: DialogBoxParams): Promise<void> {
if (this._closePromise) {
await this._closePromise;
}
this._params = params;
}

Expand Down Expand Up @@ -132,21 +139,24 @@ class DialogBox extends LitElement {

private _dismiss(): void {
this._closeState = "canceled";
this._closeDialog();
this._cancel();
this._closeDialog();
}

private _confirm(): void {
this._closeState = "confirmed";
this._closeDialog();
if (this._params!.confirm) {
this._params!.confirm(this._textField?.value);
}
this._closeDialog();
}

private _closeDialog() {
fireEvent(this, "dialog-closed", { dialog: this.localName });
this._dialog?.close();
this._closePromise = new Promise((resolve) => {
this._closeResolve = resolve;
});
}

private _dialogClosed() {
Expand All @@ -156,6 +166,8 @@ class DialogBox extends LitElement {
}
this._closeState = undefined;
this._params = undefined;
this._closeResolve?.();
this._closeResolve = undefined;
}

static get styles(): CSSResultGroup {
Expand Down

0 comments on commit c6f0c62

Please sign in to comment.