@yaks/sync 0.0.0-stage → 0.2.36

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 (75) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +729 -2
  3. package/_dist/coverage.d.ts +7 -0
  4. package/_dist/coverage.d.ts.map +1 -0
  5. package/_dist/inbound.d.ts +43 -0
  6. package/_dist/inbound.d.ts.map +1 -0
  7. package/_dist/mark.d.ts +29 -0
  8. package/_dist/mark.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +78 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/outbound.d.ts +55 -0
  12. package/_dist/outbound.d.ts.map +1 -0
  13. package/_dist/outbox.d.ts +48 -0
  14. package/_dist/outbox.d.ts.map +1 -0
  15. package/_dist/pace.d.ts +9 -0
  16. package/_dist/pace.d.ts.map +1 -0
  17. package/_dist/port.d.ts +31 -0
  18. package/_dist/port.d.ts.map +1 -0
  19. package/_dist/revision.d.ts +60 -0
  20. package/_dist/revision.d.ts.map +1 -0
  21. package/_dist/saying.d.ts +12 -0
  22. package/_dist/saying.d.ts.map +1 -0
  23. package/_dist/socket.d.ts +151 -0
  24. package/_dist/socket.d.ts.map +1 -0
  25. package/_dist/sync.d.ts +97 -0
  26. package/_dist/sync.d.ts.map +1 -0
  27. package/_dist/tier.d.ts +62 -0
  28. package/_dist/tier.d.ts.map +1 -0
  29. package/clock.js +43 -0
  30. package/clock.js.map +1 -0
  31. package/clock.ts +56 -0
  32. package/coverage.js +13 -0
  33. package/coverage.js.map +1 -0
  34. package/coverage.ts +26 -0
  35. package/deno.json +1 -0
  36. package/inbound.js +217 -0
  37. package/inbound.js.map +1 -0
  38. package/inbound.ts +251 -0
  39. package/mark.js +87 -0
  40. package/mark.js.map +1 -0
  41. package/mark.ts +108 -0
  42. package/mod.js +78 -0
  43. package/mod.js.map +1 -0
  44. package/mod.ts +153 -0
  45. package/outbound.js +170 -0
  46. package/outbound.js.map +1 -0
  47. package/outbound.ts +221 -0
  48. package/outbox.js +281 -0
  49. package/outbox.js.map +1 -0
  50. package/outbox.ts +343 -0
  51. package/pace.js +96 -0
  52. package/pace.js.map +1 -0
  53. package/pace.ts +91 -0
  54. package/package.json +32 -4
  55. package/port.js +128 -0
  56. package/port.js.map +1 -0
  57. package/port.ts +176 -0
  58. package/rebase.js +93 -0
  59. package/rebase.js.map +1 -0
  60. package/rebase.ts +77 -0
  61. package/revision.js +104 -0
  62. package/revision.js.map +1 -0
  63. package/revision.ts +146 -0
  64. package/saying.js +62 -0
  65. package/saying.js.map +1 -0
  66. package/saying.ts +69 -0
  67. package/socket.js +694 -0
  68. package/socket.js.map +1 -0
  69. package/socket.ts +927 -0
  70. package/sync.js +542 -0
  71. package/sync.js.map +1 -0
  72. package/sync.ts +705 -0
  73. package/tier.js +176 -0
  74. package/tier.js.map +1 -0
  75. package/tier.ts +211 -0
package/tier.js ADDED
@@ -0,0 +1,176 @@
1
+ // What one committed list of bundles sends to the server. A client graph holds
2
+ // three kinds of state at once — what the server owns, what this browser owns,
3
+ // and what only this render owns — and being able to write all three in one
4
+ // `apply()` is the whole point of running the graph in a page. Each component
5
+ // declares which kind it is, using the two core vocabulary keywords (@yaks/vocab
6
+ // lifetime.ts):
7
+ //
8
+ // sync: "server" (the default) the server owns it and fans it out
9
+ // sync: "peers" the server relays it to subscribers without owning it
10
+ // sync: "none" it stays on this node
11
+ //
12
+ // durable: "forever" (the default) storage — the server's, or the vault
13
+ // durable: "connection" memory, for as long as this connection lives
14
+ // durable: "5s" the same, plus a timer
15
+ //
16
+ // A component that declares neither is sent to the server and kept forever,
17
+ // because the common case is data the server owns.
18
+ //
19
+ // What is sent outward is narrower than what was written locally: only
20
+ // components that leave this node, only the properties a client is allowed to
21
+ // write (a stamped property is the server's to write), and only the bundles the
22
+ // caller actually passed in — the entities a cascade deleted and the
23
+ // provenance the stamping phase wrote are the local graph's own conclusions,
24
+ // and the server will reach the same ones from the same patch.
25
+ import { comps, dead } from "@yaks/graph/bundle";
26
+ import { durableOf, saveOf, syncOf } from "@yaks/vocab";
27
+ import { asked, before, ruled } from "./mark.js";
28
+ export { durableOf, syncOf };
29
+ /** Whether a component's writes leave this node at all — a `sync: none`
30
+ * component is never sent anywhere. */ export let outbound = (vocab, comp)=>syncOf(vocab, comp) != 'none';
31
+ /** Whether the server keeps a component, so a row it answers is the whole
32
+ * truth about its saved value. Peer components with a save query also ride
33
+ * stored snapshots; their live values still arrive through the relay. */ export let stored = (vocab, comp)=>syncOf(vocab, comp) == 'server' || saveOf(vocab, comp) != null;
34
+ /**
35
+ * Where a component's state has to be kept on this node, for the components no
36
+ * server will ever send back: the vault when it is durable forever (it
37
+ * survives a reload), process memory otherwise (it goes with the tab, or with
38
+ * its timer). A component that is sent to the server is neither, because the
39
+ * server is what sends it back.
40
+ */ export let local = (vocab, comp)=>outbound(vocab, comp) ? null : durableOf(vocab, comp) == 'forever' ? 'vault' : 'memory';
41
+ // The properties of one patch a client is allowed to write. A stamped property
42
+ // is the server's to write (it writes its own `created`), and a computed one
43
+ // has no value to send — `writable` is exactly those two excluded.
44
+ let writable = (vocab, name, patch)=>{
45
+ let allowed = new Set(vocab.comp(name)?.writable ?? []);
46
+ return Object.fromEntries(Object.entries(patch).filter(([c])=>allowed.has(c)));
47
+ };
48
+ // A component that holds nothing but the stamps this node's graph wrote: the
49
+ // server writes its own, so there is nothing in it for a server to keep.
50
+ let stampsOnly = (vocab, name, held)=>!!held && Object.keys(held).length > 0 && !Object.keys(writable(vocab, name, held)).length;
51
+ /**
52
+ * Whether a row is this node's own: it holds a component that stays here, and
53
+ * nothing a server keeps besides the stamps this graph wrote on it. No server
54
+ * ever answers for such a row, so a watch that waits for the server's answer
55
+ * would never show it.
56
+ *
57
+ * ```ts
58
+ * import { equal } from '@yaks/testing'
59
+ * import { loadVocab } from '@yaks/vocab'
60
+ * import { stays } from '@yaks/sync'
61
+ * let open = { type: 'boolean' }
62
+ * let v = loadVocab({ $defs: {
63
+ * doc: { component: true, properties: { title: { type: 'string' } } },
64
+ * Panel: { component: true, sync: 'none', properties: { open } },
65
+ * } })
66
+ * equal(stays(v, { entity: { eid: 'm' }, Panel: { open: true } }), true)
67
+ * equal(stays(v, { entity: { eid: 'm' }, Panel: {}, doc: {} }), false)
68
+ * ```
69
+ */ export let stays = (vocab, b)=>{
70
+ let held = comps(b);
71
+ return held.some(([name])=>!outbound(vocab, name)) && held.every(([name, c])=>!outbound(vocab, name) || stampsOnly(vocab, name, c));
72
+ };
73
+ // One committed list of bundles, reduced to the components with one given
74
+ // `sync` value: the bundles the caller passed in, carrying the properties a
75
+ // client is allowed to write. A bundle with nothing left in it is dropped.
76
+ let leaving = (bundles, vocab, sync, marks)=>bundles.flatMap((b)=>{
77
+ if (!asked(b)) return [];
78
+ let out = {
79
+ entity: {
80
+ eid: b.entity.eid
81
+ }
82
+ };
83
+ if (marks && b.$was) out.$was = b.$was;
84
+ if (marks && dead(b)) out.$delete = true;
85
+ for (let [name, patch] of comps(b)){
86
+ if (syncOf(vocab, name) != sync) continue;
87
+ if (patch == null) {
88
+ out[name] = null; // dropping a component needs no properties
89
+ continue;
90
+ }
91
+ let keep = writable(vocab, name, patch);
92
+ // A patch of nothing but stamped properties has nothing to send.
93
+ if (Object.keys(patch).length && !Object.keys(keep).length) continue;
94
+ out[name] = keep;
95
+ }
96
+ return comps(out).length || out.$delete ? [
97
+ out
98
+ ] : [];
99
+ });
100
+ /**
101
+ * One committed list of bundles, reduced to what the server should be told —
102
+ * the `sync: server` components, the ones it owns and stores. A write that is
103
+ * entirely local returns an empty list and nothing is posted at all.
104
+ *
105
+ * The `$was` precondition is sent with it, so the check the local graph just
106
+ * made is made again against the server's copy; the identity is sent as the
107
+ * `eid` alone, because `num` belongs to whoever is storing it.
108
+ */ export let outward = (bundles, vocab)=>leaving(bundles, vocab, 'server', true);
109
+ /**
110
+ * One committed list of bundles, reduced to what the peers should be told —
111
+ * the `sync: peers` components, which the server relays and saves only when
112
+ * they declare a save query.
113
+ *
114
+ * These are sent over the WebSocket, not to `POST /apply`. Their lifetime is
115
+ * that socket's: `durable: connection` means the server clears them when the
116
+ * connection closes, so the connection the value arrived on has to be the one
117
+ * holding it. A second reason is traffic — a caret or a cursor moves faster
118
+ * than a POST should. The server admits saved values as the socket's vouched
119
+ * writer; the page sends no extra request or provenance claim.
120
+ */ export let relayed = (bundles, vocab)=>leaving(bundles, vocab, 'peers', false);
121
+ // The patches that put these components of one entity back as `was` held
122
+ // them: a component it did not hold is dropped, one the write dropped comes
123
+ // back whole, and every property a patch named returns to what it was (or is
124
+ // cleared).
125
+ let undo = (eid, patches, was)=>{
126
+ let out = {
127
+ entity: {
128
+ eid
129
+ }
130
+ };
131
+ for (let [name, patch] of patches){
132
+ let held = was?.[name];
133
+ if (!held) out[name] = null;
134
+ else if (patch == null) out[name] = held;
135
+ else {
136
+ out[name] = Object.fromEntries(Object.keys(patch).map((c)=>[
137
+ c,
138
+ held[c] ?? null
139
+ ]));
140
+ }
141
+ }
142
+ return comps(out).length ? [
143
+ out
144
+ ] : [];
145
+ };
146
+ /**
147
+ * The inverse of one committed list of bundles, as far as the server's
148
+ * refusal reaches: what to patch back when it refuses. What the caller sent is
149
+ * restored from the copy {@link before} took of it; its local and relayed
150
+ * values were not in the refused request, so they stay as they are. What the
151
+ * write's rules added is restored whole, since the write they followed from
152
+ * never happened.
153
+ *
154
+ * A DELETE gets no inverse here: a write that deletes is never applied
155
+ * optimistically — it waits for the server (sync.ts), so there is never a
156
+ * tombstone to lift.
157
+ */ export let inverse = (bundles, vocab)=>bundles.flatMap((b)=>{
158
+ let was = before(b);
159
+ let wrote = asked(b) ? outward([
160
+ b
161
+ ], vocab)[0] : b;
162
+ if (was === undefined || !wrote || dead(b)) return [];
163
+ return undo(b.entity.eid, comps(wrote), was);
164
+ });
165
+ /**
166
+ * What the write's rules guessed about the components the server keeps, put
167
+ * back: the server ran the same rules over the whole graph, and its answer
168
+ * replaces the guess, so the guess is undone just before the answer lands. A
169
+ * rule's `sync: none` components are the page's own, and stay.
170
+ */ export let guessed = (bundles, vocab)=>bundles.flatMap((b)=>{
171
+ let was = before(b);
172
+ if (!ruled(b) || was === undefined) return [];
173
+ let kept = comps(b).filter(([name])=>stored(vocab, name));
174
+ return undo(b.entity.eid, kept, was);
175
+ });
176
+ //# sourceMappingURL=tier.js.map
package/tier.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tier.js","sources":["./tier.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,+EAA+E;AAC/E,4EAA4E;AAC5E,8EAA8E;AAC9E,iFAAiF;AACjF,gBAAgB;AAChB,EAAE;AACF,qEAAqE;AACrE,0EAA0E;AAC1E,0CAA0C;AAC1C,EAAE;AACF,8EAA8E;AAC9E,wEAAwE;AACxE,kDAAkD;AAClD,EAAE;AACF,4EAA4E;AAC5E,mDAAmD;AACnD,EAAE;AACF,uEAAuE;AACvE,8EAA8E;AAC9E,gFAAgF;AAChF,qEAAqE;AACrE,6EAA6E;AAC7E,+DAA+D;AAG/D,SAAS,KAAK,EAAE,IAAI,kCAAuC;AAC3D,SAAS,SAAS,EAAE,MAAM,EAAa,MAAM,2BAA4C;AACzF,SAAS,KAAK,EAAE,MAAM,EAAE,KAAK,oBAAmB;AAEhD,SAAS,SAAS,EAAa,MAAM,GAAE;AAEvC;qCACqC,GACrC,OAAO,IAAI,WAAW,CAAC,OAAc,OACnC,OAAO,OAAO,SAAS,OAAM;AAE/B;;uEAEuE,GACvE,OAAO,IAAI,SAAS,CAAC,OAAc,OACjC,OAAO,OAAO,SAAS,YAAY,OAAO,OAAO,SAAS,KAAI;AAEhE;;;;;;CAMC,GACD,OAAO,IAAI,QAAQ,CAAC,OAAc,OAChC,SAAS,OAAO,QACZ,OACA,UAAU,OAAO,SAAS,YAC1B,UACA,SAAQ;AAEd,+EAA+E;AAC/E,6EAA6E;AAC7E,mEAAmE;AACnE,IAAI,WAAW,CAAC,OAAc,MAAc;EAC1C,IAA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QAAQ;EAC1C,GAAE;AAEJ;;;;;CAKC,GACD,OAAO,IAAI,UAAU,CAAC,SAAmB,QACvC,QAAQ,OAAO,CAAC,CAAC;IACf,IAAI,MAAM,OAAO;IACjB,IAAI,CAAC,MAAM,MAAM,QAAQ,WAAW,OAAO,EAAE;IAC7C,IAAI,OAAO,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,GAAK,OAAO,OAAO;IACrD,OAAO,KAAK,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM;EAClC,GAAE"}
package/tier.ts ADDED
@@ -0,0 +1,211 @@
1
+ // What one committed list of bundles sends to the server. A client graph holds
2
+ // three kinds of state at once — what the server owns, what this browser owns,
3
+ // and what only this render owns — and being able to write all three in one
4
+ // `apply()` is the whole point of running the graph in a page. Each component
5
+ // declares which kind it is, using the two core vocabulary keywords (@yaks/vocab
6
+ // lifetime.ts):
7
+ //
8
+ // sync: "server" (the default) the server owns it and fans it out
9
+ // sync: "peers" the server relays it to subscribers without owning it
10
+ // sync: "none" it stays on this node
11
+ //
12
+ // durable: "forever" (the default) storage — the server's, or the vault
13
+ // durable: "connection" memory, for as long as this connection lives
14
+ // durable: "5s" the same, plus a timer
15
+ //
16
+ // A component that declares neither is sent to the server and kept forever,
17
+ // because the common case is data the server owns.
18
+ //
19
+ // What is sent outward is narrower than what was written locally: only
20
+ // components that leave this node, only the properties a client is allowed to
21
+ // write (a stamped property is the server's to write), and only the bundles the
22
+ // caller actually passed in — the entities a cascade deleted and the
23
+ // provenance the stamping phase wrote are the local graph's own conclusions,
24
+ // and the server will reach the same ones from the same patch.
25
+
26
+ import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
27
+ import { comps, dead } from '@yaks/graph/bundle'
28
+ import { durableOf, saveOf, type Sync, syncOf, type Vocab } from '@yaks/vocab'
29
+ import { asked, before, ruled } from './mark.js'
30
+
31
+ export { durableOf, type Sync, syncOf }
32
+
33
+ /** Whether a component's writes leave this node at all — a `sync: none`
34
+ * component is never sent anywhere. */
35
+ export let outbound = (vocab: Vocab, comp: string): boolean =>
36
+ syncOf(vocab, comp) != 'none'
37
+
38
+ /** Whether the server keeps a component, so a row it answers is the whole
39
+ * truth about its saved value. Peer components with a save query also ride
40
+ * stored snapshots; their live values still arrive through the relay. */
41
+ export let stored = (vocab: Vocab, comp: string): boolean =>
42
+ syncOf(vocab, comp) == 'server' || saveOf(vocab, comp) != null
43
+
44
+ /**
45
+ * Where a component's state has to be kept on this node, for the components no
46
+ * server will ever send back: the vault when it is durable forever (it
47
+ * survives a reload), process memory otherwise (it goes with the tab, or with
48
+ * its timer). A component that is sent to the server is neither, because the
49
+ * server is what sends it back.
50
+ */
51
+ export let local = (vocab: Vocab, comp: string): 'vault' | 'memory' | null =>
52
+ outbound(vocab, comp)
53
+ ? null
54
+ : durableOf(vocab, comp) == 'forever'
55
+ ? 'vault'
56
+ : 'memory'
57
+
58
+ // The properties of one patch a client is allowed to write. A stamped property
59
+ // is the server's to write (it writes its own `created`), and a computed one
60
+ // has no value to send — `writable` is exactly those two excluded.
61
+ let writable = (vocab: Vocab, name: string, patch: Comp): Comp => {
62
+ let allowed = new Set(vocab.comp(name)?.writable ?? [])
63
+ return Object.fromEntries(
64
+ Object.entries(patch).filter(([c]) => allowed.has(c)),
65
+ )
66
+ }
67
+
68
+ // A component that holds nothing but the stamps this node's graph wrote: the
69
+ // server writes its own, so there is nothing in it for a server to keep.
70
+ let stampsOnly = (vocab: Vocab, name: string, held: Comp | null) =>
71
+ !!held && Object.keys(held).length > 0 &&
72
+ !Object.keys(writable(vocab, name, held)).length
73
+
74
+ /**
75
+ * Whether a row is this node's own: it holds a component that stays here, and
76
+ * nothing a server keeps besides the stamps this graph wrote on it. No server
77
+ * ever answers for such a row, so a watch that waits for the server's answer
78
+ * would never show it.
79
+ *
80
+ * ```ts
81
+ * import { equal } from '@yaks/testing'
82
+ * import { loadVocab } from '@yaks/vocab'
83
+ * import { stays } from '@yaks/sync'
84
+ * let open = { type: 'boolean' }
85
+ * let v = loadVocab({ $defs: {
86
+ * doc: { component: true, properties: { title: { type: 'string' } } },
87
+ * Panel: { component: true, sync: 'none', properties: { open } },
88
+ * } })
89
+ * equal(stays(v, { entity: { eid: 'm' }, Panel: { open: true } }), true)
90
+ * equal(stays(v, { entity: { eid: 'm' }, Panel: {}, doc: {} }), false)
91
+ * ```
92
+ */
93
+ export let stays = (vocab: Vocab, b: Bundle): boolean => {
94
+ let held = comps(b)
95
+ return held.some(([name]) => !outbound(vocab, name)) &&
96
+ held.every(([name, c]) =>
97
+ !outbound(vocab, name) || stampsOnly(vocab, name, c)
98
+ )
99
+ }
100
+
101
+ // One committed list of bundles, reduced to the components with one given
102
+ // `sync` value: the bundles the caller passed in, carrying the properties a
103
+ // client is allowed to write. A bundle with nothing left in it is dropped.
104
+ let leaving = (
105
+ bundles: Bundle[],
106
+ vocab: Vocab,
107
+ sync: Sync,
108
+ marks: boolean,
109
+ ): Bundle[] =>
110
+ bundles.flatMap((b) => {
111
+ if (!asked(b)) return []
112
+ let out: Bundle = { entity: { eid: b.entity.eid } }
113
+ if (marks && b.$was) out.$was = b.$was
114
+ if (marks && dead(b)) out.$delete = true
115
+ for (let [name, patch] of comps(b)) {
116
+ if (syncOf(vocab, name) != sync) continue
117
+ if (patch == null) {
118
+ out[name] = null // dropping a component needs no properties
119
+ continue
120
+ }
121
+ let keep = writable(vocab, name, patch)
122
+ // A patch of nothing but stamped properties has nothing to send.
123
+ if (Object.keys(patch).length && !Object.keys(keep).length) continue
124
+ out[name] = keep
125
+ }
126
+ return comps(out).length || out.$delete ? [out] : []
127
+ })
128
+
129
+ /**
130
+ * One committed list of bundles, reduced to what the server should be told —
131
+ * the `sync: server` components, the ones it owns and stores. A write that is
132
+ * entirely local returns an empty list and nothing is posted at all.
133
+ *
134
+ * The `$was` precondition is sent with it, so the check the local graph just
135
+ * made is made again against the server's copy; the identity is sent as the
136
+ * `eid` alone, because `num` belongs to whoever is storing it.
137
+ */
138
+ export let outward = (bundles: Bundle[], vocab: Vocab): Bundle[] =>
139
+ leaving(bundles, vocab, 'server', true)
140
+
141
+ /**
142
+ * One committed list of bundles, reduced to what the peers should be told —
143
+ * the `sync: peers` components, which the server relays and saves only when
144
+ * they declare a save query.
145
+ *
146
+ * These are sent over the WebSocket, not to `POST /apply`. Their lifetime is
147
+ * that socket's: `durable: connection` means the server clears them when the
148
+ * connection closes, so the connection the value arrived on has to be the one
149
+ * holding it. A second reason is traffic — a caret or a cursor moves faster
150
+ * than a POST should. The server admits saved values as the socket's vouched
151
+ * writer; the page sends no extra request or provenance claim.
152
+ */
153
+ export let relayed = (bundles: Bundle[], vocab: Vocab): Bundle[] =>
154
+ leaving(bundles, vocab, 'peers', false)
155
+
156
+ // The patches that put these components of one entity back as `was` held
157
+ // them: a component it did not hold is dropped, one the write dropped comes
158
+ // back whole, and every property a patch named returns to what it was (or is
159
+ // cleared).
160
+ let undo = (
161
+ eid: string,
162
+ patches: [string, Comp | null][],
163
+ was: Bundle | null,
164
+ ): Bundle[] => {
165
+ let out: Bundle = { entity: { eid } }
166
+ for (let [name, patch] of patches) {
167
+ let held = was?.[name] as Comp | undefined
168
+ if (!held) out[name] = null
169
+ else if (patch == null) out[name] = held
170
+ else {
171
+ out[name] = Object.fromEntries(
172
+ Object.keys(patch).map((c) => [c, held[c] ?? null]),
173
+ )
174
+ }
175
+ }
176
+ return comps(out).length ? [out] : []
177
+ }
178
+
179
+ /**
180
+ * The inverse of one committed list of bundles, as far as the server's
181
+ * refusal reaches: what to patch back when it refuses. What the caller sent is
182
+ * restored from the copy {@link before} took of it; its local and relayed
183
+ * values were not in the refused request, so they stay as they are. What the
184
+ * write's rules added is restored whole, since the write they followed from
185
+ * never happened.
186
+ *
187
+ * A DELETE gets no inverse here: a write that deletes is never applied
188
+ * optimistically — it waits for the server (sync.ts), so there is never a
189
+ * tombstone to lift.
190
+ */
191
+ export let inverse = (bundles: Bundle[], vocab: Vocab): Bundle[] =>
192
+ bundles.flatMap((b) => {
193
+ let was = before(b)
194
+ let wrote = asked(b) ? outward([b], vocab)[0] : b
195
+ if (was === undefined || !wrote || dead(b)) return []
196
+ return undo(b.entity.eid, comps(wrote), was)
197
+ })
198
+
199
+ /**
200
+ * What the write's rules guessed about the components the server keeps, put
201
+ * back: the server ran the same rules over the whole graph, and its answer
202
+ * replaces the guess, so the guess is undone just before the answer lands. A
203
+ * rule's `sync: none` components are the page's own, and stay.
204
+ */
205
+ export let guessed = (bundles: Bundle[], vocab: Vocab): Bundle[] =>
206
+ bundles.flatMap((b) => {
207
+ let was = before(b)
208
+ if (!ruled(b) || was === undefined) return []
209
+ let kept = comps(b).filter(([name]) => stored(vocab, name))
210
+ return undo(b.entity.eid, kept, was)
211
+ })