coroutine.d.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * A Coroutine<T> is the intersection of:
  3. * 1. An Iterator that yields void, returns a T, and is not passed values with calls to next.
  4. * 2. An IterableIterator of void (since it only yields void).
  5. */
  6. type CoroutineBase<TStep, TReturn> = Iterator<TStep, TReturn, void> & IterableIterator<TStep>;
  7. /** @internal */
  8. export type Coroutine<T> = CoroutineBase<void, T>;
  9. /** @internal */
  10. export type AsyncCoroutine<T> = CoroutineBase<void | Promise<void>, T>;
  11. /** @internal */
  12. export type CoroutineStep<T> = IteratorResult<void, T>;
  13. /** @internal */
  14. export type CoroutineScheduler<T> = (coroutine: AsyncCoroutine<T>, onStep: (stepResult: CoroutineStep<T>) => void, onError: (stepError: any) => void) => void;
  15. /**
  16. * @internal
  17. */
  18. export declare function inlineScheduler<T>(coroutine: AsyncCoroutine<T>, onStep: (stepResult: CoroutineStep<T>) => void, onError: (stepError: any) => void): void;
  19. /**
  20. * @internal
  21. */
  22. export declare function createYieldingScheduler<T>(yieldAfterMS?: number): (coroutine: AsyncCoroutine<T>, onStep: (stepResult: CoroutineStep<T>) => void, onError: (stepError: any) => void) => void;
  23. /**
  24. * @internal
  25. */
  26. export declare function runCoroutine<T>(coroutine: AsyncCoroutine<T>, scheduler: CoroutineScheduler<T>, onSuccess: (result: T) => void, onError: (error: any) => void, abortSignal?: AbortSignal): void;
  27. /**
  28. * @internal
  29. */
  30. export declare function runCoroutineSync<T>(coroutine: Coroutine<T>, abortSignal?: AbortSignal): T;
  31. /**
  32. * @internal
  33. */
  34. export declare function runCoroutineAsync<T>(coroutine: AsyncCoroutine<T>, scheduler: CoroutineScheduler<T>, abortSignal?: AbortSignal): Promise<T>;
  35. /**
  36. * Given a function that returns a Coroutine<T>, produce a function with the same parameters that returns a T.
  37. * The returned function runs the coroutine synchronously.
  38. * @param coroutineFactory A function that returns a Coroutine<T>.
  39. * @param abortSignal
  40. * @returns A function that runs the coroutine synchronously.
  41. * @internal
  42. */
  43. export declare function makeSyncFunction<TParams extends unknown[], TReturn>(coroutineFactory: (...params: TParams) => Coroutine<TReturn>, abortSignal?: AbortSignal): (...params: TParams) => TReturn;
  44. /**
  45. * Given a function that returns a Coroutine<T>, product a function with the same parameters that returns a Promise<T>.
  46. * The returned function runs the coroutine asynchronously, yield control of the execution context occasionally to enable a more responsive experience.
  47. * @param coroutineFactory A function that returns a Coroutine<T>.
  48. * @param scheduler
  49. * @param abortSignal
  50. * @returns A function that runs the coroutine asynchronously.
  51. * @internal
  52. */
  53. export declare function makeAsyncFunction<TParams extends unknown[], TReturn>(coroutineFactory: (...params: TParams) => AsyncCoroutine<TReturn>, scheduler: CoroutineScheduler<TReturn>, abortSignal?: AbortSignal): (...params: TParams) => Promise<TReturn>;
  54. export {};