state.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. export * as State from "./state"
  2. import { Effect, Scope, Semaphore } from "effect"
  3. import type { Draft, Objectish } from "immer"
  4. /**
  5. * A replayable transform applied to an editor during rebuild.
  6. *
  7. * Transforms are intentionally synchronous and mutation-shaped: domain editors
  8. * hide the draft representation while preserving concise plugin/config code.
  9. */
  10. export type Transform<Editor> = (editor: Editor) => void
  11. export type MakeEditor<State extends Objectish, Editor> = (draft: Draft<State>) => Editor
  12. export interface Options<State extends Objectish, Editor> {
  13. /** Creates the base value for initial state and every scoped-transform rebuild. */
  14. readonly initial: () => State
  15. /** Wraps the mutable draft in a domain-specific editor. */
  16. readonly editor: MakeEditor<State, Editor>
  17. /**
  18. * Completes every committed edit.
  19. *
  20. * For rebuilds, this runs after all active transforms have been replayed and
  21. * before the rebuilt state becomes visible. For direct updates, this runs
  22. * after the current state has already been edited. The optional reason is
  23. * caller-defined metadata for exceptional update origins.
  24. */
  25. readonly finalize?: (editor: Editor, reason?: string) => Effect.Effect<void>
  26. }
  27. export interface Interface<State extends Objectish, Editor> {
  28. readonly get: () => State
  29. /**
  30. * Registers a scoped transform slot and returns the slot updater.
  31. *
  32. * Acquiring the slot has no visible effect until the returned updater is
  33. * called. Each updater call replaces that slot's transform, then rebuilds the
  34. * materialized state from `initial()` by replaying all active transforms in
  35. * registration order. Closing the owning Scope removes the slot and rebuilds.
  36. */
  37. readonly transform: () => Effect.Effect<(transform: Transform<Editor>) => Effect.Effect<void>, never, Scope.Scope>
  38. /** Registers and applies a replayable transform in the current Scope. */
  39. readonly update: (update: Transform<Editor>) => Effect.Effect<void, never, Scope.Scope>
  40. /**
  41. * Mutates the current materialized state directly, once.
  42. *
  43. * This is not replayable transform state: a later rebuild starts again
  44. * from `initial()` plus active transforms, so direct edits must be reserved
  45. * for current-state adjustments that are intentionally outside the transform
  46. * fold.
  47. */
  48. readonly mutate: (update: (editor: Editor) => Effect.Effect<void>, reason?: string) => Effect.Effect<void>
  49. }
  50. export function create<State extends Objectish, Editor>(options: Options<State, Editor>): Interface<State, Editor> {
  51. let state = options.initial()
  52. let transforms: { update: Transform<Editor> }[] = []
  53. const semaphore = Semaphore.makeUnsafe(1)
  54. const commit = Effect.fn("State.commit")(function* (next: State, reason?: string) {
  55. const api = options.editor(next as Draft<State>)
  56. if (options.finalize) yield* options.finalize(api, reason)
  57. state = next
  58. })
  59. const rebuild = Effect.fnUntraced(function* () {
  60. const next = options.initial()
  61. const api = options.editor(next as Draft<State>)
  62. for (const transform of transforms)
  63. yield* Effect.sync(() => transform.update(api)).pipe(Effect.withSpan("State.rebuild.update", {}))
  64. yield* commit(next)
  65. })
  66. const result: Interface<State, Editor> = {
  67. get: () => state,
  68. transform: Effect.fn("State.transform")(function* () {
  69. const scope = yield* Scope.Scope
  70. return yield* Effect.uninterruptible(
  71. Effect.gen(function* () {
  72. const transform = { update: (_editor: Editor) => {} }
  73. transforms = [...transforms, transform]
  74. yield* Scope.addFinalizer(
  75. scope,
  76. semaphore.withPermit(
  77. Effect.sync(() => {
  78. transforms = transforms.filter((item) => item !== transform)
  79. }).pipe(Effect.andThen(rebuild())),
  80. ),
  81. )
  82. return (update: Transform<Editor>) =>
  83. Effect.uninterruptible(
  84. semaphore.withPermit(
  85. Effect.sync(() => {
  86. transform.update = update
  87. }).pipe(Effect.andThen(rebuild())),
  88. ),
  89. )
  90. }),
  91. )
  92. }),
  93. update: Effect.fn("State.update")(function* (update) {
  94. const transform = yield* result.transform()
  95. yield* transform(update)
  96. }),
  97. mutate: Effect.fn("State.mutate")(function* (update, reason) {
  98. const api = options.editor(state as Draft<State>)
  99. yield* update(api)
  100. if (options.finalize) yield* options.finalize(api, reason)
  101. }, semaphore.withPermit),
  102. }
  103. return result
  104. }