mirror of
https://github.com/fromchat-messenger/web.git
synced 2026-09-22 19:15:08 +03:00
Clean up
This commit is contained in:
@@ -43,4 +43,34 @@ export function ub64(s: string): Uint8Array {
|
||||
|
||||
export function id<T extends Element = HTMLElement>(id: string): T {
|
||||
return document.getElementById(id) as unknown as T
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the specified callback after `click` or `touchstart` event is triggered.
|
||||
*
|
||||
* @param action The action to perform after interaction
|
||||
* @returns A function to clean up the event listeners.
|
||||
*/
|
||||
export function doAfterInteraction<T>(action?: () => (T | Promise<T>)): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
function doAfterInteractionInner() {
|
||||
document.removeEventListener("click", doAfterInteractionInner);
|
||||
document.removeEventListener("touchstart", doAfterInteractionInner);
|
||||
try {
|
||||
const result = action?.();
|
||||
if (result instanceof Promise) {
|
||||
result.then(resolve);
|
||||
} else {
|
||||
resolve(result as T);
|
||||
}
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("click", doAfterInteractionInner);
|
||||
document.addEventListener("touchstart", doAfterInteractionInner);
|
||||
|
||||
setTimeout(reject, 10000);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user