@rivetkit/workflow-engine 0.0.0-06-12-chore-rivetkit-wasm-re-enable-wasm-build.4be04fe → 0.0.0-06-09-refactor-rivetkit-split-workflow-context-into-workflowcontext-workflowstepcontext.e0c9540

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1664,6 +1664,30 @@ function setEntry(storage, location, entry) {
1664
1664
  storage.history.entries.set(key, entry);
1665
1665
  }
1666
1666
 
1667
+ // src/utils.ts
1668
+ function sleep(ms) {
1669
+ return new Promise((resolve) => setTimeout(resolve, ms));
1670
+ }
1671
+ var TIMEOUT_MAX = 2147483647;
1672
+ function setLongTimeout(listener, after) {
1673
+ let timeout;
1674
+ function start(remaining) {
1675
+ if (remaining <= TIMEOUT_MAX) {
1676
+ timeout = setTimeout(listener, remaining);
1677
+ } else {
1678
+ timeout = setTimeout(() => {
1679
+ start(remaining - TIMEOUT_MAX);
1680
+ }, TIMEOUT_MAX);
1681
+ }
1682
+ }
1683
+ start(after);
1684
+ return {
1685
+ abort: () => {
1686
+ if (timeout !== void 0) clearTimeout(timeout);
1687
+ }
1688
+ };
1689
+ }
1690
+
1667
1691
  // src/context.ts
1668
1692
  var DEFAULT_MAX_RETRIES = 3;
1669
1693
  var DEFAULT_RETRY_BACKOFF_BASE = 100;
@@ -2015,34 +2039,20 @@ var WorkflowContextImpl = class _WorkflowContextImpl {
2015
2039
  * The event listener uses { once: true } to auto-remove after firing,
2016
2040
  * preventing memory leaks if this method is called multiple times.
2017
2041
  */
2018
- /**
2019
- * Wait for `ms`, rejecting early with EvictedError if the workflow is
2020
- * evicted. Both the timer and the abort listener are torn down on either
2021
- * outcome, so a completed sleep never leaves a dangling listener on the
2022
- * long-lived run abort signal.
2023
- */
2024
- async sleepOrEvict(ms) {
2025
- if (this.abortSignal.aborted) {
2026
- throw new EvictedError();
2027
- }
2028
- let timer;
2029
- let onAbort;
2030
- try {
2031
- await new Promise((resolve, reject) => {
2032
- timer = setTimeout(resolve, ms);
2033
- onAbort = () => reject(new EvictedError());
2034
- this.abortSignal.addEventListener("abort", onAbort, {
2035
- once: true
2036
- });
2037
- });
2038
- } finally {
2039
- if (timer !== void 0) {
2040
- clearTimeout(timer);
2041
- }
2042
- if (onAbort) {
2043
- this.abortSignal.removeEventListener("abort", onAbort);
2042
+ waitForEviction() {
2043
+ return new Promise((_, reject) => {
2044
+ if (this.abortSignal.aborted) {
2045
+ reject(new EvictedError());
2046
+ return;
2044
2047
  }
2045
- }
2048
+ this.abortSignal.addEventListener(
2049
+ "abort",
2050
+ () => {
2051
+ reject(new EvictedError());
2052
+ },
2053
+ { once: true }
2054
+ );
2055
+ });
2046
2056
  }
2047
2057
  // === Step ===
2048
2058
  async step(nameOrConfig, run) {
@@ -2685,7 +2695,7 @@ var WorkflowContextImpl = class _WorkflowContextImpl {
2685
2695
  return;
2686
2696
  }
2687
2697
  if (remaining < this.driver.workerPollInterval) {
2688
- await this.sleepOrEvict(remaining);
2698
+ await Promise.race([sleep(remaining), this.waitForEviction()]);
2689
2699
  this.checkEvicted();
2690
2700
  if (entry.kind.type === "sleep") {
2691
2701
  entry.kind.data.state = "completed";
@@ -3496,30 +3506,6 @@ var WorkflowContextImpl = class _WorkflowContextImpl {
3496
3506
  }
3497
3507
  };
3498
3508
 
3499
- // src/utils.ts
3500
- function sleep(ms) {
3501
- return new Promise((resolve) => setTimeout(resolve, ms));
3502
- }
3503
- var TIMEOUT_MAX = 2147483647;
3504
- function setLongTimeout(listener, after) {
3505
- let timeout;
3506
- function start(remaining) {
3507
- if (remaining <= TIMEOUT_MAX) {
3508
- timeout = setTimeout(listener, remaining);
3509
- } else {
3510
- timeout = setTimeout(() => {
3511
- start(remaining - TIMEOUT_MAX);
3512
- }, TIMEOUT_MAX);
3513
- }
3514
- }
3515
- start(after);
3516
- return {
3517
- abort: () => {
3518
- if (timeout !== void 0) clearTimeout(timeout);
3519
- }
3520
- };
3521
- }
3522
-
3523
3509
  // src/index.ts
3524
3510
  var Loop = {
3525
3511
  continue: (state) => ({
@@ -3743,41 +3729,25 @@ async function executeLiveWorkflow(workflowId, workflowFn, input, driver, messag
3743
3729
  const hasMessages = result.waitingForMessages !== void 0;
3744
3730
  const hasDeadline = result.sleepUntil !== void 0;
3745
3731
  if (hasMessages && hasDeadline) {
3746
- const iterationAbort = new AbortController();
3747
- const onRunAbort = () => iterationAbort.abort();
3748
- if (abortController.signal.aborted) {
3749
- iterationAbort.abort();
3750
- } else {
3751
- abortController.signal.addEventListener("abort", onRunAbort, {
3752
- once: true
3753
- });
3754
- }
3755
- const messagePromise = awaitWithEviction(
3756
- driver.waitForMessages(
3757
- result.waitingForMessages,
3758
- iterationAbort.signal
3759
- ),
3760
- iterationAbort.signal
3761
- );
3762
- const sleepPromise = waitForSleep(
3763
- runtime,
3764
- result.sleepUntil,
3765
- iterationAbort.signal
3766
- );
3767
- messagePromise.catch(() => {
3768
- });
3769
- sleepPromise.catch(() => {
3770
- });
3771
3732
  try {
3733
+ const messagePromise = awaitWithEviction(
3734
+ driver.waitForMessages(
3735
+ result.waitingForMessages,
3736
+ abortController.signal
3737
+ ),
3738
+ abortController.signal
3739
+ );
3740
+ const sleepPromise = waitForSleep(
3741
+ runtime,
3742
+ result.sleepUntil,
3743
+ abortController.signal
3744
+ );
3772
3745
  await Promise.race([messagePromise, sleepPromise]);
3773
3746
  } catch (error) {
3774
3747
  if (error instanceof EvictedError) {
3775
3748
  return lastResult;
3776
3749
  }
3777
3750
  throw error;
3778
- } finally {
3779
- iterationAbort.abort();
3780
- abortController.signal.removeEventListener("abort", onRunAbort);
3781
3751
  }
3782
3752
  continue;
3783
3753
  }
@@ -4220,15 +4190,15 @@ export {
4220
4190
  deleteEntriesWithPrefix,
4221
4191
  getEntry,
4222
4192
  setEntry,
4193
+ sleep,
4223
4194
  DEFAULT_MAX_RETRIES,
4224
4195
  DEFAULT_RETRY_BACKOFF_BASE,
4225
4196
  DEFAULT_RETRY_BACKOFF_MAX,
4226
4197
  DEFAULT_LOOP_HISTORY_PRUNE_INTERVAL,
4227
4198
  DEFAULT_STEP_TIMEOUT,
4228
4199
  WorkflowContextImpl,
4229
- sleep,
4230
4200
  Loop,
4231
4201
  runWorkflow,
4232
4202
  replayWorkflowFromStep
4233
4203
  };
4234
- //# sourceMappingURL=chunk-I4AXJJGO.js.map
4204
+ //# sourceMappingURL=chunk-DFNXCZ47.js.map