Make the DialogManager more ESM-y #15

Merged
Oliver merged 1 commit from chore/esmify-DialogManager into main 2025-11-22 03:56:58 +00:00
Showing only changes of commit 41034854eb - Show all commits

View file

@ -1,17 +1,18 @@
import { Ask } from "../apps/Ask.mjs";
export class DialogManager {
/** @type {Map<string, Promise>} */
static #promises = new Map();
static #dialogs = new Map();
/** @type {Map<string, Promise>} */
const promises = new Map();
static async close(id) {
this.#dialogs.get(id)?.close();
this.#dialogs.delete(id);
this.#promises.delete(id);
};
/** @type {Map<string, ApplicationV2>} */
const dialogs = new Map();
/**
export function close(id) {
dialogs.get(id)?.close();
dialogs.delete(id);
promises.delete(id);
};
/**
* Asks the user to provide a simple piece of information, this is primarily
* intended to be used within macros so that it can have better info gathering
* as needed. This returns an object of input keys/labels to the value the user
@ -22,13 +23,13 @@ export class DialogManager {
* @param {AskOptions} opts
* @returns {AskResult}
*/
static async ask(
export async function ask(
data,
{
onlyOneWaiting = true,
alwaysUseAnswerObject = true,
} = {},
) {
) {
if (!data.id) {
return {
state: `errored`,
@ -44,13 +45,13 @@ export class DialogManager {
const id = data.id;
// Don't do multi-thread waiting
if (this.#dialogs.has(id)) {
const app = this.#dialogs.get(id);
if (dialogs.has(id)) {
const app = dialogs.get(id);
app.bringToFront();
if (onlyOneWaiting) {
return { state: `fronted` };
} else {
return this.#promises.get(id);
return promises.get(id);
};
};
@ -88,21 +89,26 @@ export class DialogManager {
...data,
alwaysUseAnswerObject,
onClose: () => {
this.#dialogs.delete(id);
this.#promises.delete(id);
dialogs.delete(id);
promises.delete(id);
resolve({ state: `prompted` });
},
onConfirm: (answers) => resolve({ state: `prompted`, answers }),
});
app.render({ force: true });
this.#dialogs.set(id, app);
dialogs.set(id, app);
});
this.#promises.set(id, promise);
promises.set(id, promise);
return promise;
};
static get size() {
return this.#dialogs.size;
};
};
export function size() {
return dialogs.size;
};
export const DialogManager = {
close,
ask,
size,
};