@typeonce/effect-machine 0.44.0 → 0.45.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.
@@ -39,6 +39,29 @@ import { ChildMachineLogicTypeId } from "./symbols.js"
39
39
  /** @internal */
40
40
  export const activeSnapshotObserver: unique symbol = Symbol.for("effect/Machine/activeSnapshotObserver")
41
41
 
42
+ /**
43
+ * Synchronous snapshot observation offered by local process references.
44
+ *
45
+ * The observer runs whenever the reference's snapshot changes, before the
46
+ * operation that changed it returns, and reads the latest value from
47
+ * `snapshot`. Unlike `changes`, it never coalesces or defers notification, so
48
+ * synchronous stores can mirror a local machine within the caller's turn.
49
+ * References without this capability, such as remote ones, are observed
50
+ * through `changes`.
51
+ *
52
+ * @internal
53
+ */
54
+ export const snapshotObserver: unique symbol = Symbol.for("effect/Machine/snapshotObserver")
55
+
56
+ /** @internal */
57
+ export interface SnapshotObservable {
58
+ readonly [snapshotObserver]: (observer: () => void) => () => void
59
+ }
60
+
61
+ /** @internal */
62
+ export const isSnapshotObservable = (value: unknown): value is SnapshotObservable =>
63
+ typeof value === "object" && value !== null && snapshotObserver in value
64
+
42
65
  /** @internal */
43
66
  const sendParentOverride: unique symbol = Symbol.for("effect/Machine/sendParentOverride")
44
67
 
@@ -245,6 +268,8 @@ export interface CompiledProcessContext<State, Event> {
245
268
  readonly ownedChildren: OwnedChildRuntime
246
269
  readonly poll: () => Option.Option<Event>
247
270
  readonly pollMessage: () => Option.Option<ProcessMessage<Event>>
271
+ /** Marks a message delivered without the mailbox as the one in flight. */
272
+ readonly beginMessage: (message: ProcessMessage<Event>) => void
248
273
  readonly state: () => State
249
274
  readonly completeMessage: (delivery: AcknowledgedDelivery<State>) => void
250
275
  readonly commit: (state: State) => Effect.Effect<void> | undefined
@@ -273,12 +298,29 @@ type CompiledProcessDrain<State, Event, Error, Requirements, Output> =
273
298
  context: ProcessContext<State, Event>
274
299
  ) => Effect.Effect<Option.Option<Output>, Error, Requirements>
275
300
  }
276
- | {
277
- readonly _tag: "Owned"
278
- readonly run: (
279
- context: CompiledProcessContext<State, Event>
280
- ) => Effect.Effect<Option.Option<Output>, Error, Requirements>
281
- }
301
+ | OwnedCompiledProcessDrain<State, Event, Error, Requirements, Output>
302
+
303
+ /**
304
+ * A compiled drain that owns its planning loop.
305
+ *
306
+ * `runSync`, when present, drains in the caller without an Effect, starting
307
+ * with a message the idle runtime delivered directly instead of queueing: it
308
+ * commits messages whose transitions need no effectful work and returns the
309
+ * drain continuing from the first message that does, equivalent to `run` from
310
+ * that message, or `undefined` once the mailbox is empty.
311
+ *
312
+ * @internal
313
+ */
314
+ export interface OwnedCompiledProcessDrain<State, Event, Error, Requirements, Output> {
315
+ readonly _tag: "Owned"
316
+ readonly run: (
317
+ context: CompiledProcessContext<State, Event>
318
+ ) => Effect.Effect<Option.Option<Output>, Error, Requirements>
319
+ readonly runSync?: (
320
+ context: CompiledProcessContext<State, Event>,
321
+ message: ProcessMessage<Event>
322
+ ) => Effect.Effect<Option.Option<Output>, Error, Requirements> | undefined
323
+ }
282
324
 
283
325
  /**
284
326
  * The complete capability descriptor consumed by the compact process runtime.
@@ -356,6 +398,72 @@ export const closeCompactMailbox = (mailbox: CompactProcessMailbox<unknown>): vo
356
398
  mailbox.index = 0
357
399
  }
358
400
 
401
+ /**
402
+ * Mailbox for general process logic. An offer resumes a suspended receiver in
403
+ * the sender's turn, so a process waiting for its next message handles it
404
+ * before the send returns unless that handling suspends.
405
+ */
406
+ export interface ProcessMailbox<Event> extends CompactProcessMailbox<Event> {
407
+ readonly receivers: Set<(effect: Effect.Effect<void>) => void>
408
+ }
409
+
410
+ export const makeProcessMailbox = <Event>(): ProcessMailbox<Event> => ({
411
+ items: undefined,
412
+ index: 0,
413
+ closed: false,
414
+ receivers: new Set()
415
+ })
416
+
417
+ /**
418
+ * Enqueues a message, runs `onAccepted`, and then resumes waiting receivers.
419
+ * Returns `false` without enqueueing when the mailbox is closed.
420
+ */
421
+ export const offerProcessMailbox = <Event>(
422
+ mailbox: ProcessMailbox<Event>,
423
+ message: ProcessMessage<Event>,
424
+ onAccepted?: () => void
425
+ ): boolean => {
426
+ if (mailbox.closed) {
427
+ return false
428
+ }
429
+ offerCompactMailbox(mailbox, message)
430
+ onAccepted?.()
431
+ while (mailbox.items !== undefined && mailbox.receivers.size > 0) {
432
+ const receiver = mailbox.receivers.values().next().value!
433
+ mailbox.receivers.delete(receiver)
434
+ receiver(Effect.void)
435
+ }
436
+ return true
437
+ }
438
+
439
+ export const receiveProcessMailbox = <Event>(
440
+ mailbox: ProcessMailbox<Event>
441
+ ): Effect.Effect<ProcessMessage<Event>> =>
442
+ Effect.suspend(() => {
443
+ const message = pollCompactMailbox(mailbox)
444
+ if (Option.isSome(message)) {
445
+ return Effect.succeed(message.value)
446
+ }
447
+ if (mailbox.closed) {
448
+ return Effect.interrupt
449
+ }
450
+ return Effect.callback<void>((resume) => {
451
+ mailbox.receivers.add(resume)
452
+ return Effect.sync(() => {
453
+ mailbox.receivers.delete(resume)
454
+ })
455
+ }).pipe(Effect.andThen(receiveProcessMailbox(mailbox)))
456
+ })
457
+
458
+ export const closeProcessMailbox = (mailbox: ProcessMailbox<unknown>): void => {
459
+ closeCompactMailbox(mailbox)
460
+ const receivers = Array.from(mailbox.receivers)
461
+ mailbox.receivers.clear()
462
+ for (const receiver of receivers) {
463
+ receiver(Effect.interrupt)
464
+ }
465
+ }
466
+
359
467
  export interface ProcessLogic<
360
468
  State,
361
469
  Event,