deferred-promise.d.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type { Disposable } from "./lifetime";
  2. import type { QuickJSHandle } from "./types";
  3. import type { QuickJSRuntime } from "./runtime";
  4. import type { QuickJSContext } from "./context";
  5. export type { PromiseExecutor } from "./types";
  6. /**
  7. * QuickJSDeferredPromise wraps a QuickJS promise [[handle]] and allows
  8. * [[resolve]]ing or [[reject]]ing that promise. Use it to bridge asynchronous
  9. * code on the host to APIs inside a QuickJSContext.
  10. *
  11. * Managing the lifetime of promises is tricky. There are three
  12. * [[QuickJSHandle]]s inside of each deferred promise object: (1) the promise
  13. * itself, (2) the `resolve` callback, and (3) the `reject` callback.
  14. *
  15. * - If the promise will be fulfilled before the end of it's [[owner]]'s lifetime,
  16. * the only cleanup necessary is `deferred.handle.dispose()`, because
  17. * calling [[resolve]] or [[reject]] will dispose of both callbacks automatically.
  18. *
  19. * - As the return value of a [[VmFunctionImplementation]], return [[handle]],
  20. * and ensure that either [[resolve]] or [[reject]] will be called. No other
  21. * clean-up is necessary.
  22. *
  23. * - In other cases, call [[dispose]], which will dispose [[handle]] as well as the
  24. * QuickJS handles that back [[resolve]] and [[reject]]. For this object,
  25. * [[dispose]] is idempotent.
  26. */
  27. export declare class QuickJSDeferredPromise implements Disposable {
  28. owner: QuickJSRuntime;
  29. context: QuickJSContext;
  30. /**
  31. * A handle of the Promise instance inside the QuickJSContext.
  32. * You must dispose [[handle]] or the entire QuickJSDeferredPromise once you
  33. * are finished with it.
  34. */
  35. handle: QuickJSHandle;
  36. /**
  37. * A native promise that will resolve once this deferred is settled.
  38. */
  39. settled: Promise<void>;
  40. private resolveHandle;
  41. private rejectHandle;
  42. private onSettled;
  43. /**
  44. * Use [[QuickJSContext.newPromise]] to create a new promise instead of calling
  45. * this constructor directly.
  46. * @unstable
  47. */
  48. constructor(args: {
  49. context: QuickJSContext;
  50. promiseHandle: QuickJSHandle;
  51. resolveHandle: QuickJSHandle;
  52. rejectHandle: QuickJSHandle;
  53. });
  54. /**
  55. * Resolve [[handle]] with the given value, if any.
  56. * Calling this method after calling [[dispose]] is a no-op.
  57. *
  58. * Note that after resolving a promise, you may need to call
  59. * [[QuickJSContext.executePendingJobs]] to propagate the result to the promise's
  60. * callbacks.
  61. */
  62. resolve: (value?: QuickJSHandle) => void;
  63. /**
  64. * Reject [[handle]] with the given value, if any.
  65. * Calling this method after calling [[dispose]] is a no-op.
  66. *
  67. * Note that after rejecting a promise, you may need to call
  68. * [[QuickJSContext.executePendingJobs]] to propagate the result to the promise's
  69. * callbacks.
  70. */
  71. reject: (value?: QuickJSHandle) => void;
  72. get alive(): boolean;
  73. dispose: () => void;
  74. private disposeResolvers;
  75. }