import { WAIT_UNTIL_ACTION } from '../store/middleware/waitUntilAction'; import type { WaitActionArgs } from '../store/middleware/waitUntilAction'; import type { ThunkDispatch } from 'redux-thunk'; import type { AnyAction } from 'redux'; import type { State } from '../types/redux'; export function waitUntil({ predicate, run }: WaitActionArgs) { return { type: WAIT_UNTIL_ACTION, predicate, run, }; } export async function waitUntilWithTimeout( dispatch: ThunkDispatch, waitActionArgs: (resolve: (value?: T) => void) => WaitActionArgs, timeout = 30000, ): Promise { let waitDone = false; const waitPromise = new Promise(resolve => { dispatch(waitUntil(waitActionArgs(resolve))); }); const timeoutPromise = new Promise(resolve => { setTimeout(() => { if (waitDone) { resolve(); } else { console.warn('Wait Action timed out'); resolve(null); } }, timeout); }); const result = await Promise.race([ waitPromise .then(result => { waitDone = true; return result; }) .catch(null), timeoutPromise, ]); return result; }