deferred-promise.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.QuickJSDeferredPromise = void 0;
  4. /**
  5. * QuickJSDeferredPromise wraps a QuickJS promise [[handle]] and allows
  6. * [[resolve]]ing or [[reject]]ing that promise. Use it to bridge asynchronous
  7. * code on the host to APIs inside a QuickJSContext.
  8. *
  9. * Managing the lifetime of promises is tricky. There are three
  10. * [[QuickJSHandle]]s inside of each deferred promise object: (1) the promise
  11. * itself, (2) the `resolve` callback, and (3) the `reject` callback.
  12. *
  13. * - If the promise will be fulfilled before the end of it's [[owner]]'s lifetime,
  14. * the only cleanup necessary is `deferred.handle.dispose()`, because
  15. * calling [[resolve]] or [[reject]] will dispose of both callbacks automatically.
  16. *
  17. * - As the return value of a [[VmFunctionImplementation]], return [[handle]],
  18. * and ensure that either [[resolve]] or [[reject]] will be called. No other
  19. * clean-up is necessary.
  20. *
  21. * - In other cases, call [[dispose]], which will dispose [[handle]] as well as the
  22. * QuickJS handles that back [[resolve]] and [[reject]]. For this object,
  23. * [[dispose]] is idempotent.
  24. */
  25. class QuickJSDeferredPromise {
  26. /**
  27. * Use [[QuickJSContext.newPromise]] to create a new promise instead of calling
  28. * this constructor directly.
  29. * @unstable
  30. */
  31. constructor(args) {
  32. /**
  33. * Resolve [[handle]] with the given value, if any.
  34. * Calling this method after calling [[dispose]] is a no-op.
  35. *
  36. * Note that after resolving a promise, you may need to call
  37. * [[QuickJSContext.executePendingJobs]] to propagate the result to the promise's
  38. * callbacks.
  39. */
  40. this.resolve = (value) => {
  41. if (!this.resolveHandle.alive) {
  42. return;
  43. }
  44. this.context
  45. .unwrapResult(this.context.callFunction(this.resolveHandle, this.context.undefined, value || this.context.undefined))
  46. .dispose();
  47. this.disposeResolvers();
  48. this.onSettled();
  49. };
  50. /**
  51. * Reject [[handle]] with the given value, if any.
  52. * Calling this method after calling [[dispose]] is a no-op.
  53. *
  54. * Note that after rejecting a promise, you may need to call
  55. * [[QuickJSContext.executePendingJobs]] to propagate the result to the promise's
  56. * callbacks.
  57. */
  58. this.reject = (value) => {
  59. if (!this.rejectHandle.alive) {
  60. return;
  61. }
  62. this.context
  63. .unwrapResult(this.context.callFunction(this.rejectHandle, this.context.undefined, value || this.context.undefined))
  64. .dispose();
  65. this.disposeResolvers();
  66. this.onSettled();
  67. };
  68. this.dispose = () => {
  69. if (this.handle.alive) {
  70. this.handle.dispose();
  71. }
  72. this.disposeResolvers();
  73. };
  74. this.context = args.context;
  75. this.owner = args.context.runtime;
  76. this.handle = args.promiseHandle;
  77. this.settled = new Promise((resolve) => {
  78. this.onSettled = resolve;
  79. });
  80. this.resolveHandle = args.resolveHandle;
  81. this.rejectHandle = args.rejectHandle;
  82. }
  83. get alive() {
  84. return this.handle.alive || this.resolveHandle.alive || this.rejectHandle.alive;
  85. }
  86. disposeResolvers() {
  87. if (this.resolveHandle.alive) {
  88. this.resolveHandle.dispose();
  89. }
  90. if (this.rejectHandle.alive) {
  91. this.rejectHandle.dispose();
  92. }
  93. }
  94. }
  95. exports.QuickJSDeferredPromise = QuickJSDeferredPromise;
  96. //# sourceMappingURL=deferred-promise.js.map