import { Future, LazyValue, Result } from '@ephox/katamari'; import * as DomEvent from '../events/DomEvent'; import type { EventArgs } from '../events/Types'; import type { SugarElement } from '../node/SugarElement'; type WorkDone = (res: Result) => void; type Worker = (callback: WorkDone) => void; type TaskConstructor = (worker: Worker) => T; const w = (fType: TaskConstructor, element: SugarElement, eventType: string, timeout: number) => fType((callback) => { const listener = DomEvent.bind(element, eventType, (event) => { clearTimeout(time); listener.unbind(); callback(Result.value(event)); }); const time = setTimeout(() => { listener.unbind(); callback(Result.error('Event ' + eventType + ' did not fire within ' + timeout + 'ms')); }, timeout); }); const cWaitFor = (element: SugarElement, eventType: string, timeout: number): LazyValue> => w(LazyValue.nu, element, eventType, timeout); const waitFor = (element: SugarElement, eventType: string, timeout: number): Future> => w>>(Future.nu, element, eventType, timeout); export { cWaitFor, waitFor };