@playfast/reform 0.0.1

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.
Files changed (204) hide show
  1. package/README.md +64 -0
  2. package/dist/cjs/boundary/boundary.js +86 -0
  3. package/dist/cjs/calc/asyncCalc.js +128 -0
  4. package/dist/cjs/calc/asyncData.js +37 -0
  5. package/dist/cjs/calc/calc.js +58 -0
  6. package/dist/cjs/calc/calcFamily.js +127 -0
  7. package/dist/cjs/channel/channel.js +142 -0
  8. package/dist/cjs/compose/composition.js +50 -0
  9. package/dist/cjs/compose/host.js +8 -0
  10. package/dist/cjs/compose/props.js +14 -0
  11. package/dist/cjs/compose/provide.js +30 -0
  12. package/dist/cjs/compose/slot.js +27 -0
  13. package/dist/cjs/compose/ui.js +61 -0
  14. package/dist/cjs/definition/definition.js +46 -0
  15. package/dist/cjs/event/event.js +36 -0
  16. package/dist/cjs/event/eventGroup.js +7 -0
  17. package/dist/cjs/feature/feature.js +102 -0
  18. package/dist/cjs/index.js +116 -0
  19. package/dist/cjs/internal/capture.js +14 -0
  20. package/dist/cjs/internal/ctx.js +2 -0
  21. package/dist/cjs/internal/errors.js +62 -0
  22. package/dist/cjs/internal/inspect.js +36 -0
  23. package/dist/cjs/internal/queryDriver.js +138 -0
  24. package/dist/cjs/internal/reuse.js +71 -0
  25. package/dist/cjs/internal/scheduler.js +73 -0
  26. package/dist/cjs/internal/seeds.js +19 -0
  27. package/dist/cjs/internal/sources.js +61 -0
  28. package/dist/cjs/internal/store.js +77 -0
  29. package/dist/cjs/internal/track.js +22 -0
  30. package/dist/cjs/package.json +4 -0
  31. package/dist/cjs/procedure/procedure.js +52 -0
  32. package/dist/cjs/reducer/reducer.js +64 -0
  33. package/dist/cjs/remote/remoteState.js +307 -0
  34. package/dist/cjs/runtime/bus.js +25 -0
  35. package/dist/cjs/runtime/loop.js +119 -0
  36. package/dist/cjs/scene/scene.js +36 -0
  37. package/dist/cjs/state/state.js +47 -0
  38. package/dist/cjs/state/stateFamily.js +101 -0
  39. package/dist/cjs/state/stateGroup.js +47 -0
  40. package/dist/cjs/state/token.js +23 -0
  41. package/dist/cjs/ui/node.js +2 -0
  42. package/dist/cjs/ui/trigger.js +2 -0
  43. package/dist/dts/boundary/boundary.d.ts +72 -0
  44. package/dist/dts/boundary/boundary.d.ts.map +1 -0
  45. package/dist/dts/calc/asyncCalc.d.ts +91 -0
  46. package/dist/dts/calc/asyncCalc.d.ts.map +1 -0
  47. package/dist/dts/calc/asyncData.d.ts +55 -0
  48. package/dist/dts/calc/asyncData.d.ts.map +1 -0
  49. package/dist/dts/calc/calc.d.ts +57 -0
  50. package/dist/dts/calc/calc.d.ts.map +1 -0
  51. package/dist/dts/calc/calcFamily.d.ts +57 -0
  52. package/dist/dts/calc/calcFamily.d.ts.map +1 -0
  53. package/dist/dts/channel/channel.d.ts +115 -0
  54. package/dist/dts/channel/channel.d.ts.map +1 -0
  55. package/dist/dts/compose/composition.d.ts +72 -0
  56. package/dist/dts/compose/composition.d.ts.map +1 -0
  57. package/dist/dts/compose/host.d.ts +17 -0
  58. package/dist/dts/compose/host.d.ts.map +1 -0
  59. package/dist/dts/compose/props.d.ts +13 -0
  60. package/dist/dts/compose/props.d.ts.map +1 -0
  61. package/dist/dts/compose/provide.d.ts +22 -0
  62. package/dist/dts/compose/provide.d.ts.map +1 -0
  63. package/dist/dts/compose/slot.d.ts +49 -0
  64. package/dist/dts/compose/slot.d.ts.map +1 -0
  65. package/dist/dts/compose/ui.d.ts +50 -0
  66. package/dist/dts/compose/ui.d.ts.map +1 -0
  67. package/dist/dts/definition/definition.d.ts +33 -0
  68. package/dist/dts/definition/definition.d.ts.map +1 -0
  69. package/dist/dts/event/event.d.ts +33 -0
  70. package/dist/dts/event/event.d.ts.map +1 -0
  71. package/dist/dts/event/eventGroup.d.ts +9 -0
  72. package/dist/dts/event/eventGroup.d.ts.map +1 -0
  73. package/dist/dts/feature/feature.d.ts +220 -0
  74. package/dist/dts/feature/feature.d.ts.map +1 -0
  75. package/dist/dts/index.d.ts +43 -0
  76. package/dist/dts/index.d.ts.map +1 -0
  77. package/dist/dts/internal/capture.d.ts +28 -0
  78. package/dist/dts/internal/capture.d.ts.map +1 -0
  79. package/dist/dts/internal/ctx.d.ts +12 -0
  80. package/dist/dts/internal/ctx.d.ts.map +1 -0
  81. package/dist/dts/internal/errors.d.ts +69 -0
  82. package/dist/dts/internal/errors.d.ts.map +1 -0
  83. package/dist/dts/internal/inspect.d.ts +17 -0
  84. package/dist/dts/internal/inspect.d.ts.map +1 -0
  85. package/dist/dts/internal/queryDriver.d.ts +65 -0
  86. package/dist/dts/internal/queryDriver.d.ts.map +1 -0
  87. package/dist/dts/internal/reuse.d.ts +10 -0
  88. package/dist/dts/internal/reuse.d.ts.map +1 -0
  89. package/dist/dts/internal/scheduler.d.ts +47 -0
  90. package/dist/dts/internal/scheduler.d.ts.map +1 -0
  91. package/dist/dts/internal/seeds.d.ts +17 -0
  92. package/dist/dts/internal/seeds.d.ts.map +1 -0
  93. package/dist/dts/internal/sources.d.ts +39 -0
  94. package/dist/dts/internal/sources.d.ts.map +1 -0
  95. package/dist/dts/internal/store.d.ts +47 -0
  96. package/dist/dts/internal/store.d.ts.map +1 -0
  97. package/dist/dts/internal/track.d.ts +33 -0
  98. package/dist/dts/internal/track.d.ts.map +1 -0
  99. package/dist/dts/procedure/procedure.d.ts +40 -0
  100. package/dist/dts/procedure/procedure.d.ts.map +1 -0
  101. package/dist/dts/reducer/reducer.d.ts +44 -0
  102. package/dist/dts/reducer/reducer.d.ts.map +1 -0
  103. package/dist/dts/remote/remoteState.d.ts +119 -0
  104. package/dist/dts/remote/remoteState.d.ts.map +1 -0
  105. package/dist/dts/runtime/bus.d.ts +27 -0
  106. package/dist/dts/runtime/bus.d.ts.map +1 -0
  107. package/dist/dts/runtime/loop.d.ts +45 -0
  108. package/dist/dts/runtime/loop.d.ts.map +1 -0
  109. package/dist/dts/scene/scene.d.ts +44 -0
  110. package/dist/dts/scene/scene.d.ts.map +1 -0
  111. package/dist/dts/state/state.d.ts +37 -0
  112. package/dist/dts/state/state.d.ts.map +1 -0
  113. package/dist/dts/state/stateFamily.d.ts +79 -0
  114. package/dist/dts/state/stateFamily.d.ts.map +1 -0
  115. package/dist/dts/state/stateGroup.d.ts +36 -0
  116. package/dist/dts/state/stateGroup.d.ts.map +1 -0
  117. package/dist/dts/state/token.d.ts +30 -0
  118. package/dist/dts/state/token.d.ts.map +1 -0
  119. package/dist/dts/ui/node.d.ts +9 -0
  120. package/dist/dts/ui/node.d.ts.map +1 -0
  121. package/dist/dts/ui/trigger.d.ts +7 -0
  122. package/dist/dts/ui/trigger.d.ts.map +1 -0
  123. package/dist/esm/boundary/boundary.js +83 -0
  124. package/dist/esm/boundary/boundary.js.map +1 -0
  125. package/dist/esm/calc/asyncCalc.js +95 -0
  126. package/dist/esm/calc/asyncCalc.js.map +1 -0
  127. package/dist/esm/calc/asyncData.js +34 -0
  128. package/dist/esm/calc/asyncData.js.map +1 -0
  129. package/dist/esm/calc/calc.js +58 -0
  130. package/dist/esm/calc/calc.js.map +1 -0
  131. package/dist/esm/calc/calcFamily.js +124 -0
  132. package/dist/esm/calc/calcFamily.js.map +1 -0
  133. package/dist/esm/channel/channel.js +136 -0
  134. package/dist/esm/channel/channel.js.map +1 -0
  135. package/dist/esm/compose/composition.js +46 -0
  136. package/dist/esm/compose/composition.js.map +1 -0
  137. package/dist/esm/compose/host.js +5 -0
  138. package/dist/esm/compose/host.js.map +1 -0
  139. package/dist/esm/compose/props.js +11 -0
  140. package/dist/esm/compose/props.js.map +1 -0
  141. package/dist/esm/compose/provide.js +28 -0
  142. package/dist/esm/compose/provide.js.map +1 -0
  143. package/dist/esm/compose/slot.js +23 -0
  144. package/dist/esm/compose/slot.js.map +1 -0
  145. package/dist/esm/compose/ui.js +57 -0
  146. package/dist/esm/compose/ui.js.map +1 -0
  147. package/dist/esm/definition/definition.js +42 -0
  148. package/dist/esm/definition/definition.js.map +1 -0
  149. package/dist/esm/event/event.js +30 -0
  150. package/dist/esm/event/event.js.map +1 -0
  151. package/dist/esm/event/eventGroup.js +4 -0
  152. package/dist/esm/event/eventGroup.js.map +1 -0
  153. package/dist/esm/feature/feature.js +98 -0
  154. package/dist/esm/feature/feature.js.map +1 -0
  155. package/dist/esm/index.js +45 -0
  156. package/dist/esm/index.js.map +1 -0
  157. package/dist/esm/internal/capture.js +11 -0
  158. package/dist/esm/internal/capture.js.map +1 -0
  159. package/dist/esm/internal/ctx.js +2 -0
  160. package/dist/esm/internal/ctx.js.map +1 -0
  161. package/dist/esm/internal/errors.js +54 -0
  162. package/dist/esm/internal/errors.js.map +1 -0
  163. package/dist/esm/internal/inspect.js +32 -0
  164. package/dist/esm/internal/inspect.js.map +1 -0
  165. package/dist/esm/internal/queryDriver.js +134 -0
  166. package/dist/esm/internal/queryDriver.js.map +1 -0
  167. package/dist/esm/internal/reuse.js +68 -0
  168. package/dist/esm/internal/reuse.js.map +1 -0
  169. package/dist/esm/internal/scheduler.js +69 -0
  170. package/dist/esm/internal/scheduler.js.map +1 -0
  171. package/dist/esm/internal/seeds.js +17 -0
  172. package/dist/esm/internal/seeds.js.map +1 -0
  173. package/dist/esm/internal/sources.js +59 -0
  174. package/dist/esm/internal/sources.js.map +1 -0
  175. package/dist/esm/internal/store.js +73 -0
  176. package/dist/esm/internal/store.js.map +1 -0
  177. package/dist/esm/internal/track.js +18 -0
  178. package/dist/esm/internal/track.js.map +1 -0
  179. package/dist/esm/package.json +4 -0
  180. package/dist/esm/procedure/procedure.js +50 -0
  181. package/dist/esm/procedure/procedure.js.map +1 -0
  182. package/dist/esm/reducer/reducer.js +63 -0
  183. package/dist/esm/reducer/reducer.js.map +1 -0
  184. package/dist/esm/remote/remoteState.js +270 -0
  185. package/dist/esm/remote/remoteState.js.map +1 -0
  186. package/dist/esm/runtime/bus.js +20 -0
  187. package/dist/esm/runtime/bus.js.map +1 -0
  188. package/dist/esm/runtime/loop.js +116 -0
  189. package/dist/esm/runtime/loop.js.map +1 -0
  190. package/dist/esm/scene/scene.js +31 -0
  191. package/dist/esm/scene/scene.js.map +1 -0
  192. package/dist/esm/state/state.js +43 -0
  193. package/dist/esm/state/state.js.map +1 -0
  194. package/dist/esm/state/stateFamily.js +96 -0
  195. package/dist/esm/state/stateFamily.js.map +1 -0
  196. package/dist/esm/state/stateGroup.js +46 -0
  197. package/dist/esm/state/stateGroup.js.map +1 -0
  198. package/dist/esm/state/token.js +20 -0
  199. package/dist/esm/state/token.js.map +1 -0
  200. package/dist/esm/ui/node.js +2 -0
  201. package/dist/esm/ui/node.js.map +1 -0
  202. package/dist/esm/ui/trigger.js +2 -0
  203. package/dist/esm/ui/trigger.js.map +1 -0
  204. package/package.json +33 -0
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeQueryDriver = exports.bumpRevision = exports.revisionZero = exports.RevisionSchema = exports.Revision = exports.gatedFlag = void 0;
4
+ const effect_1 = require("effect");
5
+ const asyncData_js_1 = require("../calc/asyncData.js");
6
+ const reuse_js_1 = require("./reuse.js");
7
+ const scheduler_js_1 = require("./scheduler.js");
8
+ const sources_js_1 = require("./sources.js");
9
+ const store_js_1 = require("./store.js");
10
+ /** The runtime gated flag for an `alwaysOn` config, typed as its `Gated` literal. */
11
+ const gatedFlag = (alwaysOn) => (alwaysOn !== true);
12
+ exports.gatedFlag = gatedFlag;
13
+ exports.Revision = effect_1.Brand.nominal();
14
+ exports.RevisionSchema = effect_1.Schema.Number.pipe(effect_1.Schema.brand('reform/Revision'));
15
+ exports.revisionZero = (0, exports.Revision)(0);
16
+ const bumpRevision = (r) => (0, exports.Revision)(r + 1);
17
+ exports.bumpRevision = bumpRevision;
18
+ /**
19
+ * Build the driver: store + subscription + request queue + run fiber, all owned
20
+ * by the ambient scope (callers run this under `Layer.scoped`). A change to an
21
+ * input re-runs the query latest-wins (a new run cancels the in-flight one; or,
22
+ * with `coalesce: 'trailing'`, lets it finish and runs one trailing refetch);
23
+ * while a re-run is in flight the last `Success`/`Error` is kept with
24
+ * `refetching: true`.
25
+ */
26
+ const makeQueryDriver = (options) => effect_1.Effect.gen(function* () {
27
+ const scheduler = yield* scheduler_js_1.resolveScheduler;
28
+ const sources = yield* (0, sources_js_1.wireSources)(options.inputs, options.invalidateBy);
29
+ const extraKey = options.extraKey;
30
+ const keyOf = (args) => extraKey === undefined ? sources.keyOf(args) : [...sources.keyOf(args), extraKey.read()];
31
+ const subscribeAll = (listener) => {
32
+ const offSources = sources.subscribe(listener);
33
+ const offExtra = extraKey?.subscribe(listener);
34
+ return () => {
35
+ offSources();
36
+ offExtra?.();
37
+ };
38
+ };
39
+ const disabledNow = (args = sources.snapshot()) => options.gated && options.disabled !== undefined ? options.disabled(args) : false;
40
+ const store = (0, store_js_1.makeStore)(disabledNow() ? asyncData_js_1.AsyncData.idle : asyncData_js_1.AsyncData.loading, scheduler);
41
+ // The last requested key, so a change that doesn't move it (or only churns
42
+ // an input `invalidateBy` ignores) doesn't re-fetch.
43
+ const lastKey = effect_1.MutableRef.make(undefined);
44
+ // The run-generation counter: bumped when a run is REQUESTED (enqueued), so
45
+ // `requested()` names the newest run that could possibly be in flight.
46
+ const generation = effect_1.MutableRef.make(0);
47
+ const nextGeneration = () => {
48
+ effect_1.MutableRef.update(generation, (n) => n + 1);
49
+ return effect_1.MutableRef.get(generation);
50
+ };
51
+ // Mark a re-fetch in flight without dropping the visible value (SWR).
52
+ const markRefetching = () => {
53
+ const prev = store.get();
54
+ if (prev._tag === 'Success')
55
+ store.set(asyncData_js_1.AsyncData.success(prev.value, true));
56
+ else if (prev._tag === 'Error')
57
+ store.set(asyncData_js_1.AsyncData.error(prev.error, true));
58
+ else
59
+ store.set(asyncData_js_1.AsyncData.loading);
60
+ };
61
+ // One run of the query, folded into the store. A failure becomes `Error`; an
62
+ // interrupt (latest-wins cancel) leaves the state untouched; a defect (a bug
63
+ // in the body) is logged and isolated — the driver keeps running. A `Success`
64
+ // additionally reports its generation through `onSettled`, after the write,
65
+ // so settle-driven consequences observe the converged value.
66
+ const runQuery = (request) => options.query(request.args).pipe(effect_1.Effect.tapDefect((defect) => effect_1.Effect.logError(`reform: ${options.label} '${options.name}' query defect`, defect)), effect_1.Effect.matchCause({
67
+ onSuccess: (value) => {
68
+ if (!disabledNow()) {
69
+ // `reuse`: share unchanged subtrees with the previous Success
70
+ // value, so a refetch that barely moved keeps identities stable.
71
+ const prev = store.get();
72
+ const shared = options.reuse === true && prev._tag === 'Success'
73
+ ? (0, reuse_js_1.reuse)(prev.value, value)
74
+ : value;
75
+ store.set(asyncData_js_1.AsyncData.success(shared, false));
76
+ options.onSettled?.(request.generation);
77
+ }
78
+ },
79
+ onFailure: (cause) => {
80
+ const failure = effect_1.Cause.failureOption(cause);
81
+ if (effect_1.Option.isSome(failure) && !disabledNow()) {
82
+ store.set(asyncData_js_1.AsyncData.error(failure.value, false));
83
+ }
84
+ },
85
+ }));
86
+ // The driver. `'switch'` (default): each new request cancels the in-flight
87
+ // run — the same latest-wins semantics a `latest` channel gives procedures.
88
+ // `'trailing'`: a strictly sequential consumer that, on wake, drains every
89
+ // request that piled up during the flight and runs the LATEST one —
90
+ // "exactly one trailing run after settle" by construction. The trailing
91
+ // run's generation is the max drained (the latest request's), so it
92
+ // vouches for every request it conflated.
93
+ const trailing = options.coalesce === 'trailing';
94
+ const requests = yield* effect_1.Queue.unbounded();
95
+ yield* effect_1.Effect.forkScoped(trailing
96
+ ? effect_1.Effect.forever(effect_1.Effect.gen(function* () {
97
+ const first = yield* effect_1.Queue.take(requests);
98
+ const queued = yield* effect_1.Queue.takeAll(requests);
99
+ const request = effect_1.Option.getOrElse(effect_1.Chunk.last(queued), () => first);
100
+ // A disable that landed while the request waited: skip the run.
101
+ if (!disabledNow())
102
+ yield* runQuery(request);
103
+ // The settle may have written a stale-key result with
104
+ // `refetching: false`; if a newer request is already waiting,
105
+ // restore the syncing flag before the next take — both writes
106
+ // coalesce into one scheduler flush for subscribers.
107
+ const pending = yield* effect_1.Queue.size(requests);
108
+ if (pending > 0 && !disabledNow())
109
+ yield* effect_1.Effect.sync(markRefetching);
110
+ }))
111
+ : effect_1.Stream.fromQueue(requests).pipe(effect_1.Stream.flatMap((request) => effect_1.Stream.fromEffect(runQuery(request)), { switch: true }), effect_1.Stream.runDrain));
112
+ const trigger = () => {
113
+ const args = sources.snapshot();
114
+ if (disabledNow(args)) {
115
+ // Switched off: show Idle and forget the key so re-enabling always re-runs.
116
+ effect_1.MutableRef.set(lastKey, undefined);
117
+ store.set(asyncData_js_1.AsyncData.idle);
118
+ return;
119
+ }
120
+ const key = keyOf(args);
121
+ const previous = effect_1.MutableRef.get(lastKey);
122
+ if (previous !== undefined && (0, sources_js_1.sameKey)(key, previous))
123
+ return;
124
+ effect_1.MutableRef.set(lastKey, key);
125
+ markRefetching();
126
+ effect_1.Queue.unsafeOffer(requests, { args, generation: nextGeneration() });
127
+ };
128
+ const unsubscribe = subscribeAll(trigger);
129
+ yield* effect_1.Effect.addFinalizer(() => effect_1.Effect.sync(unsubscribe));
130
+ // Kick off the first fetch unless the query starts disabled.
131
+ const initial = sources.snapshot();
132
+ if (!disabledNow(initial)) {
133
+ effect_1.MutableRef.set(lastKey, keyOf(initial));
134
+ effect_1.Queue.unsafeOffer(requests, { args: initial, generation: nextGeneration() });
135
+ }
136
+ return { store, requested: () => effect_1.MutableRef.get(generation) };
137
+ });
138
+ exports.makeQueryDriver = makeQueryDriver;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reuse = void 0;
4
+ const effect_1 = require("effect");
5
+ // Structural sharing for recomputed calc outputs: reconcile a fresh output
6
+ // against the previous one, substituting previous nodes wherever value equality
7
+ // holds, so identities only move where values moved. Downstream memo boundaries
8
+ // (React subtrees keyed on object identity) then skip everything that didn't
9
+ // change. Referentially transparent: the result is value-equal to `next` — only
10
+ // identities shift toward `previous` — and neither argument is mutated, so the
11
+ // pass composes with the framework's Equal-based invalidation untouched.
12
+ // Bounds the walk; calc outputs are shallow plain data by framework convention.
13
+ const maxDepth = 16;
14
+ const isPlainRecord = (v) => typeof v === 'object' &&
15
+ v !== null &&
16
+ (Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);
17
+ const go = (prev, next, depth) => {
18
+ if (Object.is(prev, next))
19
+ return prev;
20
+ // Data/Schema classes implement Equal+Hash: substitute wholesale on value
21
+ // equality. Plain objects/arrays fall through (their Equal is referential).
22
+ if (effect_1.Equal.equals(prev, next))
23
+ return prev;
24
+ if (depth <= 0)
25
+ return next;
26
+ if (Array.isArray(prev) && Array.isArray(next)) {
27
+ const out = next.map((item, i) => (i < prev.length ? go(prev[i], item, depth - 1) : item));
28
+ return prev.length === next.length && out.every((v, i) => Object.is(v, prev[i]))
29
+ ? prev
30
+ : out;
31
+ }
32
+ if (isPlainRecord(prev) && isPlainRecord(next)) {
33
+ const keys = Object.keys(next);
34
+ const out = {};
35
+ for (const key of keys)
36
+ out[key] = key in prev ? go(prev[key], next[key], depth - 1) : next[key];
37
+ const allPrev = keys.length === Object.keys(prev).length &&
38
+ keys.every((key) => key in prev && Object.is(out[key], prev[key]));
39
+ return allPrev ? prev : out;
40
+ }
41
+ // Data/Schema class instances that DIFFER still get walked: their Equal is
42
+ // fieldwise over own enumerable fields, so a reconstruction over reconciled
43
+ // fields (same prototype; the constructor is bypassed, but every leaf is a
44
+ // validated value out of `prev` or `next`) stays value-equal to `next` while
45
+ // unchanged children — e.g. one untouched element inside a class-typed
46
+ // container — keep their `prev` identity.
47
+ if (effect_1.Equal.isEqual(prev) &&
48
+ effect_1.Equal.isEqual(next) &&
49
+ Object.getPrototypeOf(prev) === Object.getPrototypeOf(next)) {
50
+ const prevFields = Object.fromEntries(Object.entries(prev));
51
+ const nextFields = Object.entries(next);
52
+ const out = {};
53
+ for (const [key, value] of nextFields)
54
+ out[key] = key in prevFields ? go(prevFields[key], value, depth - 1) : value;
55
+ const allPrev = nextFields.length === Object.keys(prevFields).length &&
56
+ nextFields.every(([key]) => key in prevFields && Object.is(out[key], prevFields[key]));
57
+ return allPrev ? prev : Object.assign(Object.create(Object.getPrototypeOf(next)), out);
58
+ }
59
+ // Class instances without Equal (Date, Map, …) are opaque leaves.
60
+ return next;
61
+ };
62
+ /**
63
+ * Substitute `previous` nodes into `next` wherever they are value-equal. The
64
+ * walker only ever returns `previous`, `next`, or a key/index-wise
65
+ * reconstruction of `next` whose every leaf came from one of them, so the
66
+ * result is value-equal to `next` and the type is preserved by construction —
67
+ * the single cast below is that argument, in the style of `wireSources` /
68
+ * `narrowStore`.
69
+ */
70
+ const reuse = (previous, next) => go(previous, next, maxDepth);
71
+ exports.reuse = reuse;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveScheduler = exports.notificationsLayer = exports.Notifications = exports.defaultScheduler = exports.makeScheduler = void 0;
4
+ const effect_1 = require("effect");
5
+ const makeScheduler = () => {
6
+ const pending = new Set();
7
+ const armed = effect_1.MutableRef.make(false);
8
+ const flush = () => {
9
+ effect_1.MutableRef.set(armed, false);
10
+ // Drain to a fixpoint within ONE microtask. A derived store's `onChange`
11
+ // re-schedules during the pass (it is itself a listener of its upstream), so
12
+ // resolving the whole dependency graph here — instead of re-arming a fresh
13
+ // microtask per layer — collapses a depth-N propagation into a single flush
14
+ // and wakes each leaf subscriber once at its final value. Converges because
15
+ // a derived store schedules only when its output actually moves (Equal).
16
+ while (pending.size > 0) {
17
+ const due = [...pending];
18
+ pending.clear();
19
+ for (const listener of due) {
20
+ // One listener's throw (a defect in a user calc body) must not abandon
21
+ // the rest of the flush; surface it to the host's global handler on a
22
+ // fresh microtask instead of unwinding this loop.
23
+ try {
24
+ listener();
25
+ }
26
+ catch (error) {
27
+ queueMicrotask(() => {
28
+ throw error;
29
+ });
30
+ }
31
+ }
32
+ }
33
+ };
34
+ const schedule = (listeners) => {
35
+ for (const listener of listeners)
36
+ pending.add(listener);
37
+ if (pending.size > 0 && !effect_1.MutableRef.get(armed)) {
38
+ effect_1.MutableRef.set(armed, true);
39
+ queueMicrotask(flush);
40
+ }
41
+ };
42
+ return { schedule };
43
+ };
44
+ exports.makeScheduler = makeScheduler;
45
+ /**
46
+ * The process-wide scheduler, used by any store created outside a runtime that
47
+ * provides its own `Notifications`. Fine for a single client app (microtask
48
+ * ordering is global anyway); a runtime that needs isolation — concurrent SSR,
49
+ * multiple mounted roots — provides `Notifications` upstream of its stores.
50
+ */
51
+ exports.defaultScheduler = (0, exports.makeScheduler)();
52
+ /**
53
+ * Optional per-runtime scheduler. When a runtime provides it upstream of its
54
+ * state/calc layers, those stores coalesce on it instead of the global default,
55
+ * isolating their notification timing from other runtimes in the same process.
56
+ */
57
+ class Notifications extends effect_1.Context.Tag('reform/Notifications')() {
58
+ }
59
+ exports.Notifications = Notifications;
60
+ /**
61
+ * A fresh per-runtime scheduler. Merged into `Engine`, so a runtime that wires
62
+ * its state/calc layers *downstream* of `Engine` gets isolated notification
63
+ * timing; the common sibling wiring falls back to `defaultScheduler` (still
64
+ * correct, just process-shared).
65
+ */
66
+ exports.notificationsLayer = effect_1.Layer.sync(Notifications, exports.makeScheduler);
67
+ /**
68
+ * Resolve the scheduler a store should use: the runtime's `Notifications` if one
69
+ * is in context, else the global default. Uses `serviceOption` so it imposes no
70
+ * hard requirement — a store layer wired as a sibling of the runtime (the common
71
+ * pattern) simply falls back to the default.
72
+ */
73
+ exports.resolveScheduler = effect_1.Effect.map(effect_1.Effect.serviceOption(Notifications), effect_1.Option.getOrElse(() => exports.defaultScheduler));
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CurrentSeedOverrides = void 0;
4
+ const effect_1 = require("effect");
5
+ /**
6
+ * The sanctioned tooling/test seam for seeding CLOSED scenes. A scene's layers
7
+ * are pre-composed (each state group seeds its own live store), so an outer
8
+ * layer cannot override an inner store — but a FiberRef set via
9
+ * `Layer.locally(ref, value)(layer)` IS visible inside the construction effects
10
+ * of nested layers. `State.live` consults this ref at store-construction time:
11
+ * if the state's member name is present, the (schema-validated) value replaces
12
+ * the authored seed; otherwise the authored seed wins.
13
+ *
14
+ * Empty by default — production wiring never touches it. The dev-tool inspector
15
+ * (`Scene.seedScene`) and proofs are the only intended writers. `globalValue`
16
+ * keeps a single ref instance even if the module is loaded twice (duplicated
17
+ * bundles, HMR), so the writer and the reader always agree.
18
+ */
19
+ exports.CurrentSeedOverrides = effect_1.GlobalValue.globalValue(Symbol.for('reform/CurrentSeedOverrides'), () => effect_1.FiberRef.unsafeMake({}));
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wireSources = exports.sameKey = void 0;
4
+ const effect_1 = require("effect");
5
+ /**
6
+ * The runtime key an input contributes to the inputs object. Prefer the
7
+ * explicit `manifest.name` — the string handed to `make` — over the structural
8
+ * `name`: a `Calc`/`AsyncCalc` *class* satisfies `Source` through its static
9
+ * `name`, and a user subclass declaration (`class Feed extends Calc.make(…)`)
10
+ * defines its OWN `name` from the class binding, shadowing the explicit one.
11
+ * That binding is what minifiers rename, so keying the snapshot by `input.name`
12
+ * made every subclassed calc input `undefined` in production builds (and would
13
+ * silently mis-key in dev whenever the binding differs from the `make` name —
14
+ * the type-level key, `SourceName<S>`, is always the `make` string). The
15
+ * manifest is inherited through the static prototype chain and never shadowed.
16
+ * `StateToken`s carry no manifest; their `name` is an instance field holding
17
+ * the explicit member key, so the fallback is always the declared string.
18
+ */
19
+ const inputKey = (input) => effect_1.Predicate.hasProperty(input, 'manifest') &&
20
+ effect_1.Predicate.isRecord(input.manifest) &&
21
+ effect_1.Predicate.isString(input.manifest.name)
22
+ ? input.manifest.name
23
+ : input.name;
24
+ /** Element-wise value equality of two invalidation keys. */
25
+ const sameKey = (a, b) => a.length === b.length && a.every((value, i) => effect_1.Equal.equals(value, b[i]));
26
+ exports.sameKey = sameKey;
27
+ /**
28
+ * Read each input's backing store and return the shared sensing surface. The
29
+ * caller owns lifecycle: register the `subscribe` result's unsubscribe as a
30
+ * scope finalizer (both live bodies run under `Layer.scoped`).
31
+ */
32
+ const wireSources = (inputs, invalidateBy) => effect_1.Effect.gen(function* () {
33
+ const sources = [];
34
+ for (const input of inputs)
35
+ sources.push(yield* input.store);
36
+ const snapshot = () => {
37
+ const args = {};
38
+ inputs.forEach((input, i) => {
39
+ // `noUncheckedIndexedAccess`: `sources` is built 1:1 with `inputs`, but
40
+ // guard the index so the read is honestly total.
41
+ const source = sources[i];
42
+ if (source !== undefined)
43
+ args[inputKey(input)] = source.getSnapshot();
44
+ });
45
+ return args;
46
+ };
47
+ const keyOf = (args) => invalidateBy ? invalidateBy(args) : Object.values(args);
48
+ const subscribe = (listener) => {
49
+ const unsubscribes = sources.map((source) => source.subscribe(listener));
50
+ return () => {
51
+ for (const unsubscribe of unsubscribes)
52
+ unsubscribe();
53
+ };
54
+ };
55
+ return { snapshot, keyOf, subscribe };
56
+ // Yielding each `input.store` (a `Context.Tag<Store<unknown>>`) widens the
57
+ // inferred requirement to `Store<unknown>`; restate the precise per-input
58
+ // `InputStores<Inputs>` union the signature promises. This is the single
59
+ // place that cast lives — `Calc.live`/`AsyncCalc.live` inherit it.
60
+ });
61
+ exports.wireSources = wireSources;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeDerivedStore = exports.makeStore = void 0;
4
+ const effect_1 = require("effect");
5
+ const inspect_js_1 = require("./inspect.js");
6
+ const scheduler_js_1 = require("./scheduler.js");
7
+ const makeStore = (initial, scheduler = scheduler_js_1.defaultScheduler) => {
8
+ // The store is a mutable reactive slot by design, so its value lives in a
9
+ // `MutableRef` we update in place (no reassigned binding).
10
+ const value = effect_1.MutableRef.make(initial);
11
+ const version = effect_1.MutableRef.make(0);
12
+ const listeners = new Set();
13
+ return {
14
+ get: () => effect_1.MutableRef.get(value),
15
+ getSnapshot: () => effect_1.MutableRef.get(value),
16
+ getVersion: () => effect_1.MutableRef.get(version),
17
+ set: (next) => {
18
+ if (effect_1.Equal.equals(effect_1.MutableRef.get(value), next))
19
+ return;
20
+ effect_1.MutableRef.set(value, next);
21
+ effect_1.MutableRef.update(version, (n) => n + 1);
22
+ scheduler.schedule(listeners);
23
+ },
24
+ subscribe: (listener) => {
25
+ listeners.add(listener);
26
+ return () => {
27
+ listeners.delete(listener);
28
+ };
29
+ },
30
+ ...(0, inspect_js_1.inspectable)(() => ({ _id: 'reform/Store', value: effect_1.MutableRef.get(value) })),
31
+ };
32
+ };
33
+ exports.makeStore = makeStore;
34
+ /**
35
+ * A read-only `Store` whose value is computed from upstream sources.
36
+ *
37
+ * Reads are pull-fresh: `get`/`getSnapshot` call `compute` directly, so a
38
+ * headless snapshot always reflects the current source state without waiting for
39
+ * a notification (`compute` must memoize so an unchanged read returns a stable
40
+ * reference — `useSyncExternalStore` requires it). *Notifications*, by contrast,
41
+ * run through the same coalescing scheduler as `makeStore`: an upstream change
42
+ * recomputes once and wakes subscribers only when the output actually moves (by
43
+ * `Equal.equals`), so every store kind shares one notification timing. `set` is
44
+ * a no-op — the only writer is the upstream subscription.
45
+ */
46
+ const makeDerivedStore = (compute, subscribeUpstream, scheduler = scheduler_js_1.defaultScheduler) => {
47
+ const listeners = new Set();
48
+ const version = effect_1.MutableRef.make(0);
49
+ // The last output we notified against — tracked only here, never touched by
50
+ // reads, so a `get` racing ahead of `onChange` can't suppress a notification.
51
+ const seen = effect_1.MutableRef.make({ value: compute() });
52
+ const onChange = () => {
53
+ const previous = effect_1.MutableRef.get(seen);
54
+ const next = compute();
55
+ effect_1.MutableRef.set(seen, { value: next });
56
+ if (previous === undefined || !effect_1.Equal.equals(previous.value, next)) {
57
+ effect_1.MutableRef.update(version, (n) => n + 1);
58
+ scheduler.schedule(listeners);
59
+ }
60
+ };
61
+ const unsubscribe = subscribeUpstream(onChange);
62
+ const store = {
63
+ get: compute,
64
+ getSnapshot: compute,
65
+ getVersion: () => effect_1.MutableRef.get(version),
66
+ set: () => { },
67
+ subscribe: (listener) => {
68
+ listeners.add(listener);
69
+ return () => {
70
+ listeners.delete(listener);
71
+ };
72
+ },
73
+ ...(0, inspect_js_1.inspectable)(() => ({ _id: 'reform/Store', value: compute() })),
74
+ };
75
+ return { store, unsubscribe };
76
+ };
77
+ exports.makeDerivedStore = makeDerivedStore;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readTracked = exports.CurrentTracker = void 0;
4
+ const effect_1 = require("effect");
5
+ /**
6
+ * The active render's dependency tracker (D5). Present only while a composition
7
+ * is rendering under `@reform/react`; absent in procedures and headless reads.
8
+ */
9
+ class CurrentTracker extends effect_1.Context.Tag('reform/Tracker')() {
10
+ }
11
+ exports.CurrentTracker = CurrentTracker;
12
+ /**
13
+ * Read a store's current snapshot and, if a tracker is active, record the store
14
+ * as a dependency of the current render. One read API, two behaviors by context
15
+ * (D5): inside a render it subscribes the slice; elsewhere it just snapshots.
16
+ */
17
+ const readTracked = (store) => effect_1.Effect.map(effect_1.Effect.serviceOption(CurrentTracker), (tracker) => {
18
+ if (effect_1.Option.isSome(tracker))
19
+ tracker.value.add(store);
20
+ return store.getSnapshot();
21
+ });
22
+ exports.readTracked = readTracked;
@@ -0,0 +1,4 @@
1
+ {
2
+ "type": "commonjs",
3
+ "sideEffects": []
4
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.live = exports.make = void 0;
4
+ const effect_1 = require("effect");
5
+ const channel_js_1 = require("../channel/channel.js");
6
+ const definition_js_1 = require("../definition/definition.js");
7
+ const bus_js_1 = require("../runtime/bus.js");
8
+ /**
9
+ * A cross-event-loop flow. The *definition* declares its trigger events and the
10
+ * channel it runs on; the *implementation* is the effectful body. Procedures no
11
+ * longer subscribe to the bus themselves — the single drain loop routes events
12
+ * to the channel, which runs the registered bodies under its policy.
13
+ */
14
+ const make = (name, config) => (0, definition_js_1.definitionClass)({
15
+ manifest: { kind: 'Procedure', name },
16
+ events: config.events,
17
+ channel: config.channel,
18
+ });
19
+ exports.make = make;
20
+ /**
21
+ * Register the flow body into its channel. The body may read state and dispatch
22
+ * events, but never writes state (the core invariant); its context requirements
23
+ * (RPC clients, etc.) are captured here so the channel fiber can run it with no
24
+ * outstanding R.
25
+ */
26
+ const live = (procedure, body) => {
27
+ const name = procedure.manifest.name;
28
+ const channelName = procedure.channel.manifest.name;
29
+ const handles = new Set(procedure.events.map((e) => e.tag));
30
+ // Scoped registration (see `Reducer.live`): an eager procedure's scope is the
31
+ // root runtime's, so behavior is unchanged; a lazy feature's procedure registers
32
+ // on mount and unregisters on unmount, so it stops running and is reclaimed.
33
+ return effect_1.Layer.scopedDiscard(effect_1.Effect.gen(function* () {
34
+ const procedures = yield* channel_js_1.Procedures;
35
+ if (procedures.entries.some((e) => e.name === name)) {
36
+ yield* effect_1.Effect.logWarning(`reform: duplicate procedure name '${name}' registered`);
37
+ }
38
+ // Snapshot the body's full context (Bus + RPC clients) so `run` is total.
39
+ const runtime = yield* effect_1.Effect.runtime();
40
+ const entry = {
41
+ name,
42
+ channelName,
43
+ // The channel only routes events whose tag is in `handles`, so at runtime
44
+ // `event` is always one of `Events`; `narrowHandled` restores the body's
45
+ // declared event union from the erased `Tagged` envelope.
46
+ handles,
47
+ run: (event) => effect_1.Effect.provide(effect_1.Effect.gen(() => body((0, bus_js_1.narrowHandled)(event))), runtime),
48
+ };
49
+ yield* effect_1.Effect.acquireRelease(effect_1.Effect.sync(() => procedures.register(entry)), () => effect_1.Effect.sync(() => procedures.unregister(entry)));
50
+ }));
51
+ };
52
+ exports.live = live;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.make = make;
4
+ exports.live = live;
5
+ const effect_1 = require("effect");
6
+ const definition_js_1 = require("../definition/definition.js");
7
+ const errors_js_1 = require("../internal/errors.js");
8
+ const stateFamily_js_1 = require("../state/stateFamily.js");
9
+ const bus_js_1 = require("../runtime/bus.js");
10
+ const loop_js_1 = require("../runtime/loop.js");
11
+ function make(name, config) {
12
+ return (0, definition_js_1.definitionClass)({
13
+ manifest: { kind: 'Reducer', name },
14
+ config,
15
+ handles: new Set(config.events.map((e) => e.tag)),
16
+ });
17
+ }
18
+ /** Reject an accidentally-async fold up front: a reducer must be a pure sync write. */
19
+ const sync = (value) => {
20
+ // Loose `!= null` so a reducer returning `undefined` doesn't crash the guard.
21
+ if (value != null && typeof value.then === 'function') {
22
+ throw new errors_js_1.AsyncReducer();
23
+ }
24
+ return value;
25
+ };
26
+ function live(reducer, fold) {
27
+ const { config, handles } = reducer;
28
+ const name = reducer.manifest.name;
29
+ // Scoped registration: register on build, unregister on scope close. For an eager
30
+ // app the scope is the root runtime's (closes only at dispose), so behavior is
31
+ // unchanged; for a lazy feature the scope is the feature's, so unmount removes the
32
+ // entry — it stops folding and is reclaimed instead of leaking onto a dead store.
33
+ return effect_1.Layer.scopedDiscard(effect_1.Effect.gen(function* () {
34
+ const reducers = yield* loop_js_1.Reducers;
35
+ if (reducers.entries.some((e) => e.name === name)) {
36
+ yield* effect_1.Effect.logWarning(`reform: duplicate reducer name '${name}' registered`);
37
+ }
38
+ const entry = yield* effect_1.Effect.gen(function* () {
39
+ if ('family' in config) {
40
+ const family = yield* config.family.store;
41
+ return {
42
+ name,
43
+ handles,
44
+ apply: (event) => {
45
+ // The loop only invokes `apply` for events in `handles`, so `event`
46
+ // is one of this reducer's declared events; `narrowHandled` restores
47
+ // that typed view from the erased `Tagged` envelope `keyOf` is fed.
48
+ const key = config.keyOf((0, bus_js_1.narrowHandled)(event));
49
+ const next = fold(family.at(key).get(), event);
50
+ // A fold may return the eviction sentinel to drop the key's store
51
+ // (bounding an otherwise-unbounded family) instead of a new value.
52
+ if (next === stateFamily_js_1.Tombstone)
53
+ family.forget(key);
54
+ else
55
+ family.at(key).set(sync(next));
56
+ },
57
+ };
58
+ }
59
+ const store = yield* config.states[0].store;
60
+ return { name, handles, apply: (event) => store.set(sync(fold(store.get(), event))) };
61
+ });
62
+ yield* effect_1.Effect.acquireRelease(effect_1.Effect.sync(() => reducers.register(entry)), () => effect_1.Effect.sync(() => reducers.unregister(entry)));
63
+ }));
64
+ }