Make the DialogManager more ESM-y

This commit is contained in:
Oliver 2025-11-21 19:56:07 -07:00
parent 382ca50bb5
commit 41034854eb

View file

@ -1,14 +1,15 @@
import { Ask } from "../apps/Ask.mjs"; import { Ask } from "../apps/Ask.mjs";
export class DialogManager {
/** @type {Map<string, Promise>} */ /** @type {Map<string, Promise>} */
static #promises = new Map(); const promises = new Map();
static #dialogs = new Map();
static async close(id) { /** @type {Map<string, ApplicationV2>} */
this.#dialogs.get(id)?.close(); const dialogs = new Map();
this.#dialogs.delete(id);
this.#promises.delete(id); export function close(id) {
dialogs.get(id)?.close();
dialogs.delete(id);
promises.delete(id);
}; };
/** /**
@ -22,7 +23,7 @@ export class DialogManager {
* @param {AskOptions} opts * @param {AskOptions} opts
* @returns {AskResult} * @returns {AskResult}
*/ */
static async ask( export async function ask(
data, data,
{ {
onlyOneWaiting = true, onlyOneWaiting = true,
@ -44,13 +45,13 @@ export class DialogManager {
const id = data.id; const id = data.id;
// Don't do multi-thread waiting // Don't do multi-thread waiting
if (this.#dialogs.has(id)) { if (dialogs.has(id)) {
const app = this.#dialogs.get(id); const app = dialogs.get(id);
app.bringToFront(); app.bringToFront();
if (onlyOneWaiting) { if (onlyOneWaiting) {
return { state: `fronted` }; return { state: `fronted` };
} else { } else {
return this.#promises.get(id); return promises.get(id);
}; };
}; };
@ -88,21 +89,26 @@ export class DialogManager {
...data, ...data,
alwaysUseAnswerObject, alwaysUseAnswerObject,
onClose: () => { onClose: () => {
this.#dialogs.delete(id); dialogs.delete(id);
this.#promises.delete(id); promises.delete(id);
resolve({ state: `prompted` }); resolve({ state: `prompted` });
}, },
onConfirm: (answers) => resolve({ state: `prompted`, answers }), onConfirm: (answers) => resolve({ state: `prompted`, answers }),
}); });
app.render({ force: true }); app.render({ force: true });
this.#dialogs.set(id, app); dialogs.set(id, app);
}); });
this.#promises.set(id, promise); promises.set(id, promise);
return promise; return promise;
}; };
static get size() { export function size() {
return this.#dialogs.size; return dialogs.size;
}; };
export const DialogManager = {
close,
ask,
size,
}; };