raf-effect-atom 0.0.0 → 0.2.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.
@@ -0,0 +1,56 @@
1
+ import * as Predicate from "effect/Predicate";
2
+ // src/internal/seeds.ts
3
+ const seedings = /* @__PURE__ */ new WeakMap();
4
+ const waiting = (registry) => {
5
+ const seeding = seedings.get(registry);
6
+ return seeding === void 0 || seeding.resolved ? void 0 : seeding;
7
+ };
8
+ /**
9
+ * Runs `seed` on `registry`. Until a seed's promise settles, the verbs wait
10
+ * for it.
11
+ */
12
+ const run = (registry, seed) => {
13
+ const result = seed(registry);
14
+ if (!Predicate.isPromiseLike(result)) return;
15
+ const seeded = Promise.resolve(result);
16
+ const seeding = {
17
+ deferred: [],
18
+ resolved: false,
19
+ seeded
20
+ };
21
+ seeded.then(() => {
22
+ seeding.resolved = true;
23
+ }, () => void 0);
24
+ seedings.set(registry, seeding);
25
+ };
26
+ /**
27
+ * The seed the verbs wait for: `undefined` once it has resolved, and the
28
+ * rejected promise once it has failed.
29
+ */
30
+ const pending = (registry) => waiting(registry)?.seeded;
31
+ /**
32
+ * Runs `start` once the registry's seed has resolved: at once, unless it's
33
+ * still running. A seed that fails never runs it.
34
+ */
35
+ const afterSeed = (registry, start) => {
36
+ const seeding = waiting(registry);
37
+ if (seeding === void 0) {
38
+ start();
39
+ return;
40
+ }
41
+ const started = seeding.seeded.then(start);
42
+ started.catch(() => void 0);
43
+ seeding.deferred.push(started);
44
+ };
45
+ /**
46
+ * Waits for the registry's seed and the starts that waited for it, so
47
+ * `Hydration.dehydrate` sees every atom the verbs started. Rejects with the
48
+ * seed's error, or with a start's defect.
49
+ */
50
+ const settled = async (registry) => {
51
+ const seeding = seedings.get(registry);
52
+ if (seeding === void 0) return;
53
+ await seeding.seeded;
54
+ await Promise.all(seeding.deferred);
55
+ };
56
+ export { afterSeed, pending, run, settled };
@@ -38,7 +38,7 @@ const nameOf = (atom) => {
38
38
  const key = keyOf(atom);
39
39
  return atom.label?.[0] ?? (Predicate.isString(key) ? key : "an atom");
40
40
  };
41
- const message = (read, started) => `raf-effect-atom: rendering ${read === started ? `read ${read}` : `read ${read}, which started ${started}`} before anything else had started it, so its request waits for this render: a request waterfall. Start it before render, in the route's loader or the server component (atoms.prefetch or atoms.ensure). If the state that picks it, such as a page, a filter or an open dialog, isn't in the URL, put it there so the loader can. As escape hatches for state that can't be in the URL: start it in the event handler that changes the state (Loader.make(registry).prefetch), or, if its input only exists in render, read it with useAtomResult(useWaterfall(atom)). ${docs}`;
41
+ const message = (read, started) => `raf-effect-atom: rendering ${read === started ? `read ${read}` : `read ${read}, which started ${started}`} before anything else had started it, so its request waits for this render: a request waterfall. Start it before render and pass its handle down, from the route's loader or the server component (atoms.prefetch(source, ...args) or atoms.ensure). If the state that picks it, such as a page, a filter or an open dialog, isn't in the URL, put it there so the loader can. As escape hatches for state that can't be in the URL: start it in the event handler that changes the state (Loader.make(registry).prefetch), or, if its input only exists in render, read it with useAtomResult(source, useWaterfall(source, ...args)). ${docs}`;
42
42
  const warned = /* @__PURE__ */ new WeakMap();
43
43
  /**
44
44
  * The nodes a read started in throw mode, with the error. React renders a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "raf-effect-atom",
3
- "version": "0.0.0",
3
+ "version": "0.2.0",
4
4
  "description": "Render-as-you-fetch for Effect Atom: start atoms from route loaders and stream them into the browser, typed failures intact",
5
5
  "keywords": [
6
6
  "effect",
@@ -58,7 +58,7 @@
58
58
  "@types/react-dom": "19.3.0",
59
59
  "@vitest/coverage-v8": "5.0.5",
60
60
  "effect": "4.0.6",
61
- "effect-atom-boundary": "0.0.0",
61
+ "effect-atom-boundary": "0.1.0",
62
62
  "happy-dom": "21.0.4",
63
63
  "react": "19.3.1",
64
64
  "react-dom": "19.3.1",