|
|
|
|
|
|
|
|
|
|
|
export type Resolver<T> = {
|
|
id: string;
|
|
completed: boolean;
|
|
resolved: boolean;
|
|
rejected: boolean;
|
|
promise: Promise<T>;
|
|
resolve: (data: T) => void;
|
|
reject: (e?: Error) => void;
|
|
timeout: number | null;
|
|
deferment?: {data?: any, timeout?: number|null, signal?: string};
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export function getResolver<T>(timeout: number = 5000): Resolver<T> {
|
|
const resolver: Partial<Resolver<T>> = {};
|
|
resolver.id = generateId(8);
|
|
resolver.completed = false;
|
|
resolver.resolved = false;
|
|
resolver.rejected = false;
|
|
resolver.promise = new Promise((resolve, reject) => {
|
|
resolver.reject = (e?: Error) => {
|
|
resolver.completed = true;
|
|
resolver.rejected = true;
|
|
reject(e);
|
|
};
|
|
resolver.resolve = (data: T) => {
|
|
resolver.completed = true;
|
|
resolver.resolved = true;
|
|
resolve(data);
|
|
};
|
|
});
|
|
resolver.timeout = setTimeout(() => {
|
|
if (!resolver.completed) {
|
|
resolver.reject!();
|
|
}
|
|
}, timeout);
|
|
return resolver as Resolver<T>;
|
|
}
|
|
|
|
|
|
const DEBOUNCE_FN_TO_PROMISE: WeakMap<Function, Promise<void>> = new WeakMap();
|
|
|
|
|
|
|
|
|
|
|
|
export function debounce(fn: Function, ms = 64) {
|
|
if (!DEBOUNCE_FN_TO_PROMISE.get(fn)) {
|
|
DEBOUNCE_FN_TO_PROMISE.set(
|
|
fn,
|
|
wait(ms).then(() => {
|
|
DEBOUNCE_FN_TO_PROMISE.delete(fn);
|
|
fn();
|
|
}),
|
|
);
|
|
}
|
|
return DEBOUNCE_FN_TO_PROMISE.get(fn);
|
|
}
|
|
|
|
|
|
export function wait(ms = 16): Promise<void> {
|
|
|
|
if (ms === 16) {
|
|
return new Promise((resolve) => {
|
|
requestAnimationFrame(() => {
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve();
|
|
}, ms);
|
|
});
|
|
}
|
|
|
|
function dec2hex(dec: number) {
|
|
return dec.toString(16).padStart(2, "0");
|
|
}
|
|
|
|
|
|
export function generateId(length: number) {
|
|
const arr = new Uint8Array(length / 2);
|
|
crypto.getRandomValues(arr);
|
|
return Array.from(arr, dec2hex).join("");
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getObjectValue(obj: any, objKey: string, def?: any) {
|
|
if (!obj || !objKey) return def;
|
|
|
|
const keys = objKey.split(".");
|
|
const key = keys.shift()!;
|
|
const found = obj[key];
|
|
if (keys.length) {
|
|
return getObjectValue(found, keys.join("."), def);
|
|
}
|
|
return found;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setObjectValue(obj: any, objKey: string, value: any, createMissingObjects = true) {
|
|
if (!obj || !objKey) return obj;
|
|
|
|
const keys = objKey.split(".");
|
|
const key = keys.shift()!;
|
|
if (obj[key] === undefined) {
|
|
if (!createMissingObjects) {
|
|
return;
|
|
}
|
|
obj[key] = {};
|
|
}
|
|
if (!keys.length) {
|
|
obj[key] = value;
|
|
} else {
|
|
if (typeof obj[key] != "object") {
|
|
obj[key] = {};
|
|
}
|
|
setObjectValue(obj[key], keys.join("."), value, createMissingObjects);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
|
|
|
|
|
|
export function moveArrayItem<T>(arr: T[], itemOrFrom: T | number, to: number) {
|
|
const from = typeof itemOrFrom === "number" ? itemOrFrom : arr.indexOf(itemOrFrom);
|
|
arr.splice(to, 0, arr.splice(from, 1)[0]!);
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeArrayItem<T>(arr: T[], itemOrIndex: T | number) {
|
|
const index = typeof itemOrIndex === "number" ? itemOrIndex : arr.indexOf(itemOrIndex);
|
|
arr.splice(index, 1);
|
|
}
|
|
|
|
|
|
|
|
|
|
export function injectCss(href: string): Promise<void> {
|
|
if (document.querySelector(`link[href^="${href}"]`)) {
|
|
return Promise.resolve();
|
|
}
|
|
return new Promise((resolve) => {
|
|
const link = document.createElement("link");
|
|
link.setAttribute("rel", "stylesheet");
|
|
link.setAttribute("type", "text/css");
|
|
const timeout = setTimeout(resolve, 1000);
|
|
link.addEventListener("load", (e) => {
|
|
clearInterval(timeout);
|
|
resolve();
|
|
});
|
|
link.href = href;
|
|
document.head.appendChild(link);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function defineProperty(instance: any, property: string, desc: PropertyDescriptor) {
|
|
const existingDesc = Object.getOwnPropertyDescriptor(instance, property);
|
|
if (existingDesc?.configurable === false) {
|
|
throw new Error(`Error: rgthree-comfy cannot define un-configurable property "${property}"`);
|
|
}
|
|
|
|
if (existingDesc?.get && desc.get) {
|
|
const descGet = desc.get;
|
|
desc.get = () => {
|
|
existingDesc.get!.apply(instance, []);
|
|
return descGet!.apply(instance, []);
|
|
};
|
|
}
|
|
if (existingDesc?.set && desc.set) {
|
|
const descSet = desc.set;
|
|
desc.set = (v: any) => {
|
|
existingDesc.set!.apply(instance, [v]);
|
|
return descSet!.apply(instance, [v]);
|
|
};
|
|
}
|
|
|
|
desc.enumerable = desc.enumerable ?? existingDesc?.enumerable ?? true;
|
|
desc.configurable = desc.configurable ?? existingDesc?.configurable ?? true;
|
|
if (!desc.get && !desc.set) {
|
|
desc.writable = desc.writable ?? existingDesc?.writable ?? true;
|
|
}
|
|
return Object.defineProperty(instance, property, desc);
|
|
}
|
|
|