@typeonce/effect-machine 0.45.0 → 0.46.0

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.
@@ -236,35 +236,44 @@ const startResolved = (
236
236
  ...(descriptor === undefined ? undefined : { descriptor }),
237
237
  ...(activityKind === undefined ? undefined : { activityKind }),
238
238
  sendParent: (isCurrent, event) => isCurrent() ? scope.self.send(event) : Effect.void,
239
- onOutcome: (isCurrent, outcome, activitySessionId) => {
240
- if (outcome._tag === "Stopped" || !isCurrent()) return Effect.void
241
- if (outcome._tag === "Done") {
242
- if (onDone === undefined) {
243
- return scope.failCause(Cause.die(
244
- new Error(
245
- `Invocation "${invokeId}" completed without the required onDone handler`
239
+ // The work starts while the event that entered its state is processed,
240
+ // and its outcome is handled in a later turn, even when the work
241
+ // completes without suspending. The entered state is therefore always
242
+ // observed before the outcome leaves it, and the outcome of a state that
243
+ // was exited in between is discarded.
244
+ onOutcome: (isCurrent, outcome, activitySessionId) =>
245
+ Effect.andThen(
246
+ Effect.yieldNow,
247
+ Effect.suspend(() => {
248
+ if (outcome._tag === "Stopped" || !isCurrent()) return Effect.void
249
+ if (outcome._tag === "Done") {
250
+ if (onDone === undefined) {
251
+ return scope.failCause(Cause.die(
252
+ new Error(
253
+ `Invocation "${invokeId}" completed without the required onDone handler`
254
+ )
255
+ ))
256
+ }
257
+ return sendLifecycle(
258
+ scope,
259
+ InvocationEvent.done(path, invokeId, outcome.output),
260
+ activityKind === undefined ? undefined : activitySessionId
246
261
  )
247
- ))
248
- }
249
- return sendLifecycle(
250
- scope,
251
- InvocationEvent.done(path, invokeId, outcome.output),
252
- activityKind === undefined ? undefined : activitySessionId
253
- )
254
- }
255
- if (
256
- outcome._tag === "Failure" &&
257
- onFailure !== undefined &&
258
- (descriptor === undefined || !isFrameworkFailure(outcome.error))
259
- ) {
260
- return sendLifecycle(
261
- scope,
262
- InvocationEvent.failure(path, invokeId, outcome.error),
263
- activityKind === undefined ? undefined : activitySessionId
264
- )
265
- }
266
- return scope.failCause(outcome.cause)
267
- },
262
+ }
263
+ if (
264
+ outcome._tag === "Failure" &&
265
+ onFailure !== undefined &&
266
+ (descriptor === undefined || !isFrameworkFailure(outcome.error))
267
+ ) {
268
+ return sendLifecycle(
269
+ scope,
270
+ InvocationEvent.failure(path, invokeId, outcome.error),
271
+ activityKind === undefined ? undefined : activitySessionId
272
+ )
273
+ }
274
+ return scope.failCause(outcome.cause)
275
+ })
276
+ ),
268
277
  ...(onSnapshot === undefined ? undefined : {
269
278
  onSnapshot: (isCurrent: () => boolean, snapshot: any) =>
270
279
  isCurrent()
@@ -644,7 +644,11 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
644
644
  this.interruptRequested = false
645
645
  return true
646
646
  }).pipe(
647
- Effect.flatMap((interrupt) => interrupt ? this.interruptAndFinish(fiber) : Effect.void)
647
+ // The worker started in the sender's turn and may be waiting for
648
+ // the sender to finish, such as an invocation its transition
649
+ // interrupts, so the sender must not wait for the interruption.
650
+ // Whoever requested the stop already awaits completion.
651
+ Effect.flatMap((interrupt) => interrupt ? Effect.forkDetach(this.interruptAndFinish(fiber)) : Effect.void)
648
652
  )
649
653
  ),
650
654
  Effect.asVoid
@@ -746,10 +746,13 @@ export const startGenericInternal: StartProcess = Effect.fnUntraced(
746
746
  }
747
747
  }
748
748
 
749
+ // The worker starts in the caller's turn and runs until it first suspends,
750
+ // so a process started while an event is processed, such as an
751
+ // invocation, begins its work before that event's sender continues.
749
752
  const forkRuntime = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
750
753
  detached === true
751
- ? Effect.forkDetach(effect)
752
- : Effect.forkChild(effect)
754
+ ? Effect.forkDetach(effect, { startImmediately: true })
755
+ : Effect.forkChild(effect, { startImmediately: true })
753
756
 
754
757
  const pendingTermination = yield* Deferred.poll(termination)
755
758
  const worker = Option.isNone(pendingTermination)
@@ -790,7 +793,6 @@ export const startGenericInternal: StartProcess = Effect.fnUntraced(
790
793
  )
791
794
 
792
795
  yield* forkRuntime(runFiber)
793
- yield* Effect.yieldNow
794
796
 
795
797
  return ref
796
798
  }