@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/rebase.ts ADDED
@@ -0,0 +1,77 @@
1
+ // Reconcile an older receipt without replacing newer optimistic transactions.
2
+ import { after, same } from '@yaks/fp'
3
+ import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
4
+ import { comps, dead } from '@yaks/graph/bundle'
5
+ import { asked, asking, before, replicate, ruling } from './mark.js'
6
+
7
+ let patched = (row: Bundle, patch: Bundle): Bundle => {
8
+ let entity = { ...row.entity, ...patch.entity }
9
+ if (dead(patch)) return { entity, tombstone: {} }
10
+ let next: Bundle = { ...row, entity }
11
+ for (let [name, values] of comps(patch)) {
12
+ if (values == null) {
13
+ delete next[name]
14
+ continue
15
+ }
16
+ let comp = { ...(next[name] as Comp | undefined), ...values }
17
+ for (let prop of Object.keys(values)) {
18
+ if (values[prop] == null) delete comp[prop]
19
+ }
20
+ next[name] = comp
21
+ }
22
+ return next
23
+ }
24
+
25
+ let difference = (held: Bundle | undefined, next: Bundle): Bundle => {
26
+ if (!held || dead(next)) return next
27
+ let patch: Bundle = { entity: next.entity }
28
+ for (let [name] of comps(held)) if (!(name in next)) patch[name] = null
29
+ for (let [name, values] of comps(next)) {
30
+ let was = held[name] as Comp | undefined
31
+ if (same(was, values)) continue
32
+ patch[name] = {
33
+ ...Object.fromEntries(
34
+ Object.keys(was ?? {}).filter((prop) => !(prop in (values ?? {})))
35
+ .map((prop) => [prop, null]),
36
+ ),
37
+ ...values,
38
+ }
39
+ }
40
+ return patch
41
+ }
42
+
43
+ export let rebase = (
44
+ graph: Graph,
45
+ landing: Bundle[],
46
+ later: { bundles: Bundle[]; held: boolean }[],
47
+ ): Bundle[] | Promise<Bundle[]> => {
48
+ let affected = new Set(landing.map((b) => b.entity.eid))
49
+ let rows = new Map<Eid, Bundle>()
50
+ for (let write of later) {
51
+ if (write.held) continue
52
+ let snapshots = new Map<Eid, Bundle>()
53
+ write.bundles = write.bundles.map((b) => {
54
+ let eid = b.entity.eid
55
+ if (!affected.has(eid) || before(b) === undefined) return b
56
+ let row = rows.get(eid)
57
+ if (!row) {
58
+ row = before(b) ?? { entity: b.entity }
59
+ for (let patch of landing) {
60
+ if (patch.entity.eid === eid) row = patched(row, patch)
61
+ }
62
+ }
63
+ if (!snapshots.has(eid)) snapshots.set(eid, row)
64
+ rows.set(eid, dead(row) ? row : patched(row, b))
65
+ let base = snapshots.get(eid)!
66
+ return asked(b) ? asking(b, base) : ruling(b, base)
67
+ })
68
+ }
69
+ if (!rows.size) return replicate(graph, landing)
70
+ return after(graph.get([...rows.keys()]), (held) => {
71
+ let prior = new Map(held.map((b) => [b.entity.eid, b]))
72
+ return replicate(graph, [
73
+ ...landing.filter((b) => !rows.has(b.entity.eid)),
74
+ ...[...rows].map(([eid, row]) => difference(prior.get(eid), row)),
75
+ ])
76
+ })
77
+ }
package/revision.js ADDED
@@ -0,0 +1,104 @@
1
+ // Which revision of its store each held row came from, and which each
2
+ // subscription's last frame said: how a copy tells it is about to be handed
3
+ // an older row than it holds, or that a frame went missing.
4
+ //
5
+ // A store's frames each say the revision their rows were read at, and the one
6
+ // their subscription's frame before said (@yaks/api `Frame`). One socket's
7
+ // frames land in order, but a door over several stores (@yaks/reach) hears
8
+ // each store on its own socket, so a frame about a row can arrive after a
9
+ // newer one about it from another subscription. Such a frame, or one whose
10
+ // subscription said a revision this copy never heard, does not land: that
11
+ // subscription is asked again, whole, and the frames still on their way from
12
+ // before the asking are let go until its first answer arrives.
13
+ //
14
+ // A revision is one number, or, from a door, a number per store; two are
15
+ // compared on the stores both name, since a revision orders one store's
16
+ // frames and no other's.
17
+ /** How many rows' revisions are kept, the least recently landed let go
18
+ * first: more than a client keeps rows (@yaks/client `RETENTION_ROWS`), so a
19
+ * row's revision outlives the row in memory. */ export const REVISIONS = 50_000;
20
+ let keyed = (r)=>typeof r == 'number' ? {
21
+ '': r
22
+ } : r;
23
+ /** Whether `a` is older than `b` on a store both name.
24
+ *
25
+ * ```ts
26
+ * import { older } from '@yaks/sync'
27
+ * import { equal } from '@yaks/testing'
28
+ *
29
+ * equal(older(4, 5), true)
30
+ * equal(older({ books: 9, loans: 1 }, { loans: 2 }), true)
31
+ * equal(older({ books: 1 }, { loans: 2 }), false)
32
+ * ```
33
+ */ export let older = (a, b)=>{
34
+ let x = keyed(a), y = keyed(b);
35
+ return Object.entries(x).some(([at, n])=>at in y && n < y[at]);
36
+ };
37
+ let same = (a, b)=>{
38
+ let x = keyed(a), y = keyed(b);
39
+ let names = Object.keys(x);
40
+ return names.length == Object.keys(y).length && names.every((at)=>x[at] === y[at]);
41
+ };
42
+ let newest = (a, b)=>{
43
+ let out = {
44
+ ...a
45
+ };
46
+ for (let [at, n] of Object.entries(b))out[at] = Math.max(out[at] ?? n, n);
47
+ return out;
48
+ };
49
+ let touched = (f)=>[
50
+ ...(f.bundles ?? []).map((b)=>b.entity.eid),
51
+ ...(f.peers ?? []).map((b)=>b.entity.eid),
52
+ ...f.gone ?? [],
53
+ ...f.peerGone ?? []
54
+ ];
55
+ /**
56
+ * A copy's revisions. A frame without a revision, from a store that says
57
+ * none, is never held back.
58
+ *
59
+ * ```ts
60
+ * import { revisions } from '@yaks/sync'
61
+ * import { equal } from '@yaks/testing'
62
+ *
63
+ * let held = revisions()
64
+ * let row = { entity: { eid: 'dal' } }
65
+ * held.asked('a')
66
+ * held.landed({ id: 'a', revision: 5, bundles: [row] })
67
+ * equal(held.check({ id: 'b', revision: 4, bundles: [row] }), 'stale')
68
+ * equal(held.check({ id: 'a', revision: 7, since: 6 }), 'skipped')
69
+ * equal(held.check({ id: 'a', revision: 6, since: 5 }), undefined)
70
+ * ```
71
+ */ export let revisions = (limit = REVISIONS)=>{
72
+ let rows = new Map();
73
+ // A subscription's last revision, or `asked` until its first answer.
74
+ let chains = new Map();
75
+ return {
76
+ check: (f)=>{
77
+ if (f.revision === undefined) return;
78
+ let chain = chains.get(f.id);
79
+ if (f.since !== undefined) {
80
+ if (chain == 'asked') return 'asked';
81
+ if (!chain || !same(f.since, chain)) return 'skipped';
82
+ }
83
+ let rev = f.revision;
84
+ return touched(f).some((eid)=>{
85
+ let held = rows.get(eid);
86
+ return held && older(rev, held);
87
+ }) ? 'stale' : undefined;
88
+ },
89
+ landed: (f)=>{
90
+ if (f.revision === undefined) return;
91
+ let rev = keyed(f.revision);
92
+ chains.set(f.id, rev);
93
+ for (let eid of touched(f)){
94
+ let was = rows.get(eid);
95
+ rows.delete(eid);
96
+ rows.set(eid, newest(was, rev));
97
+ }
98
+ while(rows.size > limit)rows.delete(rows.keys().next().value);
99
+ },
100
+ asked: (id)=>void chains.set(id, 'asked'),
101
+ closed: (id)=>void chains.delete(id)
102
+ };
103
+ };
104
+ //# sourceMappingURL=revision.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"revision.js","sources":["./revision.ts"],"names":[],"mappings":"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"}
package/revision.ts ADDED
@@ -0,0 +1,146 @@
1
+ // Which revision of its store each held row came from, and which each
2
+ // subscription's last frame said: how a copy tells it is about to be handed
3
+ // an older row than it holds, or that a frame went missing.
4
+ //
5
+ // A store's frames each say the revision their rows were read at, and the one
6
+ // their subscription's frame before said (@yaks/api `Frame`). One socket's
7
+ // frames land in order, but a door over several stores (@yaks/reach) hears
8
+ // each store on its own socket, so a frame about a row can arrive after a
9
+ // newer one about it from another subscription. Such a frame, or one whose
10
+ // subscription said a revision this copy never heard, does not land: that
11
+ // subscription is asked again, whole, and the frames still on their way from
12
+ // before the asking are let go until its first answer arrives.
13
+ //
14
+ // A revision is one number, or, from a door, a number per store; two are
15
+ // compared on the stores both name, since a revision orders one store's
16
+ // frames and no other's.
17
+ import type { Eid } from 'jsr:@yaks/graph@0.2.36'
18
+
19
+ /** A store's revision: one number, or each store's under its name. */
20
+ export type Revision = number | Record<string, number>
21
+
22
+ /** What a frame says of its revision, as {@link revisions} reads it. */
23
+ export type Stamped = {
24
+ id: string
25
+ revision?: Revision
26
+ since?: Revision
27
+ bundles?: { entity: { eid: Eid } }[]
28
+ peers?: { entity: { eid: Eid } }[]
29
+ gone?: Eid[]
30
+ peerGone?: Eid[]
31
+ }
32
+
33
+ /** Why a frame does not land: `skipped`, its subscription's frame before it
34
+ * never arrived; `stale`, a row in it is older than the one held; `asked`,
35
+ * it was on its way before its subscription was asked again. */
36
+ export type Verdict = 'skipped' | 'stale' | 'asked'
37
+
38
+ /** How many rows' revisions are kept, the least recently landed let go
39
+ * first: more than a client keeps rows (@yaks/client `RETENTION_ROWS`), so a
40
+ * row's revision outlives the row in memory. */
41
+ export const REVISIONS = 50_000
42
+
43
+ type Keyed = Record<string, number>
44
+
45
+ let keyed = (r: Revision): Keyed => typeof r == 'number' ? { '': r } : r
46
+
47
+ /** Whether `a` is older than `b` on a store both name.
48
+ *
49
+ * ```ts
50
+ * import { older } from '@yaks/sync'
51
+ * import { equal } from '@yaks/testing'
52
+ *
53
+ * equal(older(4, 5), true)
54
+ * equal(older({ books: 9, loans: 1 }, { loans: 2 }), true)
55
+ * equal(older({ books: 1 }, { loans: 2 }), false)
56
+ * ```
57
+ */
58
+ export let older = (a: Revision, b: Revision): boolean => {
59
+ let x = keyed(a), y = keyed(b)
60
+ return Object.entries(x).some(([at, n]) => at in y && n < y[at])
61
+ }
62
+
63
+ let same = (a: Revision, b: Revision): boolean => {
64
+ let x = keyed(a), y = keyed(b)
65
+ let names = Object.keys(x)
66
+ return names.length == Object.keys(y).length &&
67
+ names.every((at) => x[at] === y[at])
68
+ }
69
+
70
+ let newest = (a: Keyed | undefined, b: Keyed): Keyed => {
71
+ let out = { ...a }
72
+ for (let [at, n] of Object.entries(b)) out[at] = Math.max(out[at] ?? n, n)
73
+ return out
74
+ }
75
+
76
+ let touched = (f: Stamped): Eid[] => [
77
+ ...(f.bundles ?? []).map((b) => b.entity.eid),
78
+ ...(f.peers ?? []).map((b) => b.entity.eid),
79
+ ...f.gone ?? [],
80
+ ...f.peerGone ?? [],
81
+ ]
82
+
83
+ /** The revisions a copy holds, of rows and of subscriptions. */
84
+ export type Revisions = {
85
+ /** why this frame must not land, or nothing when it may */
86
+ check: (frame: Stamped) => Verdict | undefined
87
+ /** this frame landed: its rows and its subscription are at its revision */
88
+ landed: (frame: Stamped) => void
89
+ /** this subscription was asked, again or for the first time */
90
+ asked: (id: string) => void
91
+ /** this subscription is closed */
92
+ closed: (id: string) => void
93
+ }
94
+
95
+ /**
96
+ * A copy's revisions. A frame without a revision, from a store that says
97
+ * none, is never held back.
98
+ *
99
+ * ```ts
100
+ * import { revisions } from '@yaks/sync'
101
+ * import { equal } from '@yaks/testing'
102
+ *
103
+ * let held = revisions()
104
+ * let row = { entity: { eid: 'dal' } }
105
+ * held.asked('a')
106
+ * held.landed({ id: 'a', revision: 5, bundles: [row] })
107
+ * equal(held.check({ id: 'b', revision: 4, bundles: [row] }), 'stale')
108
+ * equal(held.check({ id: 'a', revision: 7, since: 6 }), 'skipped')
109
+ * equal(held.check({ id: 'a', revision: 6, since: 5 }), undefined)
110
+ * ```
111
+ */
112
+ export let revisions = (limit = REVISIONS): Revisions => {
113
+ let rows = new Map<Eid, Keyed>()
114
+ // A subscription's last revision, or `asked` until its first answer.
115
+ let chains = new Map<string, Keyed | 'asked'>()
116
+ return {
117
+ check: (f) => {
118
+ if (f.revision === undefined) return
119
+ let chain = chains.get(f.id)
120
+ if (f.since !== undefined) {
121
+ if (chain == 'asked') return 'asked'
122
+ if (!chain || !same(f.since, chain)) return 'skipped'
123
+ }
124
+ let rev = f.revision
125
+ return touched(f).some((eid) => {
126
+ let held = rows.get(eid)
127
+ return held && older(rev, held)
128
+ })
129
+ ? 'stale'
130
+ : undefined
131
+ },
132
+ landed: (f) => {
133
+ if (f.revision === undefined) return
134
+ let rev = keyed(f.revision)
135
+ chains.set(f.id, rev)
136
+ for (let eid of touched(f)) {
137
+ let was = rows.get(eid)
138
+ rows.delete(eid)
139
+ rows.set(eid, newest(was, rev))
140
+ }
141
+ while (rows.size > limit) rows.delete(rows.keys().next().value!)
142
+ },
143
+ asked: (id) => void chains.set(id, 'asked'),
144
+ closed: (id) => void chains.delete(id),
145
+ }
146
+ }
package/saying.js ADDED
@@ -0,0 +1,62 @@
1
+ // What this node is saying: each `sync: peers` value it relayed and has not
2
+ // cleared, whole, as the server should be holding it.
3
+ //
4
+ // The server holds a relayed value under the connection that last said it
5
+ // (@yaks/api relay.ts), so a new connection holds nothing, and the one before
6
+ // it may still be open there, holding what this node said on it. So on every
7
+ // connection this node says again what it is saying, before it subscribes to
8
+ // anything: the new connection takes each value over, the old one's close
9
+ // clears none of them, and no answer hands this node back an older copy of its
10
+ // own. A clear that no connection carried is said again too, once.
11
+ //
12
+ // A connection is never told what it is saying itself, so an answer leaves
13
+ // these values out, and their absence from a reset frame says nothing
14
+ // (inbound.ts `hear`). A value heard from another connection takes that one
15
+ // over: the last writer holds it, so it is no longer this node's to say.
16
+ import { comps } from "@yaks/graph/bundle";
17
+ import { bundled, key } from "./pace.js";
18
+ export let saying = ()=>{
19
+ let said = new Map();
20
+ let held = ()=>bundled([
21
+ ...said.values()
22
+ ]);
23
+ return {
24
+ wrote: (bundles, carried)=>{
25
+ for (let b of bundles){
26
+ let eid = b.entity.eid;
27
+ for (let [comp, patch] of comps(b)){
28
+ let k = key(eid, comp);
29
+ if (patch != null) {
30
+ let was = said.get(k)?.[2];
31
+ said.set(k, [
32
+ eid,
33
+ comp,
34
+ {
35
+ ...was,
36
+ ...patch
37
+ }
38
+ ]);
39
+ } else if (carried) said.delete(k);
40
+ else said.set(k, [
41
+ eid,
42
+ comp,
43
+ null
44
+ ]);
45
+ }
46
+ }
47
+ },
48
+ heard: (bundles)=>{
49
+ for (let b of bundles){
50
+ for (let [comp] of comps(b))said.delete(key(b.entity.eid, comp));
51
+ }
52
+ },
53
+ mine: (eid, comp)=>said.get(key(eid, comp))?.[2] != null,
54
+ held,
55
+ again: ()=>{
56
+ let out = held();
57
+ for (let [k, [, , patch]] of said)if (patch == null) said.delete(k);
58
+ return out;
59
+ }
60
+ };
61
+ };
62
+ //# sourceMappingURL=saying.js.map
package/saying.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"saying.js","sources":["./saying.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,sDAAsD;AACtD,EAAE;AACF,0EAA0E;AAC1E,8EAA8E;AAC9E,6EAA6E;AAC7E,6EAA6E;AAC7E,0EAA0E;AAC1E,+EAA+E;AAC/E,mEAAmE;AACnE,EAAE;AACF,2EAA2E;AAC3E,sEAAsE;AACtE,4EAA4E;AAC5E,yEAAyE;AAGzE,SAAS,KAAK,kCAAuC;AACrD,SAAS,OAAO,EAAE,GAAG,oBAA8B;AAoBnD,OAAO,IAAI,SAAS;EAClB,IAAI,OAAO,IAAI;EACf,IAAI,OAAO,IAAM,QAAQ;SAAI,KAAK,MAAM;KAAG;EAC3C,OAAO;IACL,OAAO,CAAC,SAAS;MACf,KAAK,IAAI,KAAK,QAAS;QACrB,IAAI,MAAM,EAAE,MAAM,CAAC,GAAG;QACtB,KAAK,IAAI,CAAC,MAAM,MAAM,IAAI,MAAM,GAAI;UAClC,IAAI,IAAI,IAAI,KAAK;UACjB,IAAI,SAAS,MAAM;YACjB,IAAI,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,EAAE;YAC1B,KAAK,GAAG,CAAC,GAAG;cAAC;cAAK;cAAM;gBAAE,GAAG,GAAG;gBAAE,GAAG,KAAK;cAAC;aAAE;UAC/C,OAAO,IAAI,SAAS,KAAK,MAAM,CAAC;eAC3B,KAAK,GAAG,CAAC,GAAG;YAAC;YAAK;YAAM;WAAK;QACpC;MACF;IACF;IACA,OAAO,CAAC;MACN,KAAK,IAAI,KAAK,QAAS;QACrB,KAAK,IAAI,CAAC,KAAK,IAAI,MAAM,GAAI,KAAK,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,EAAE;MAC7D;IACF;IACA,MAAM,CAAC,KAAK,OAAS,KAAK,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC,EAAE,IAAI;IACtD;IACA,OAAO;MACL,IAAI,MAAM;MACV,KAAK,IAAI,CAAC,GAAG,KAAK,MAAM,CAAC,IAAI,KAAM,IAAI,SAAS,MAAM,KAAK,MAAM,CAAC;MAClE,OAAO;IACT;EACF;AACF,EAAC"}
package/saying.ts ADDED
@@ -0,0 +1,69 @@
1
+ // What this node is saying: each `sync: peers` value it relayed and has not
2
+ // cleared, whole, as the server should be holding it.
3
+ //
4
+ // The server holds a relayed value under the connection that last said it
5
+ // (@yaks/api relay.ts), so a new connection holds nothing, and the one before
6
+ // it may still be open there, holding what this node said on it. So on every
7
+ // connection this node says again what it is saying, before it subscribes to
8
+ // anything: the new connection takes each value over, the old one's close
9
+ // clears none of them, and no answer hands this node back an older copy of its
10
+ // own. A clear that no connection carried is said again too, once.
11
+ //
12
+ // A connection is never told what it is saying itself, so an answer leaves
13
+ // these values out, and their absence from a reset frame says nothing
14
+ // (inbound.ts `hear`). A value heard from another connection takes that one
15
+ // over: the last writer holds it, so it is no longer this node's to say.
16
+
17
+ import type { Bundle, Eid } from 'jsr:@yaks/graph@0.2.36'
18
+ import { comps } from '@yaks/graph/bundle'
19
+ import { bundled, key, type Said } from './pace.js'
20
+
21
+ /** Whether this node is saying one component of one entity. */
22
+ export type Mine = (eid: Eid, comp: string) => boolean
23
+
24
+ /** What this node is saying, kept across its connections. */
25
+ export type Saying = {
26
+ /** a committed write's relayed bundles; `carried` is whether a connection
27
+ * is open to take them now */
28
+ wrote: (bundles: Bundle[], carried: boolean) => void
29
+ /** relayed bundles heard from another connection */
30
+ heard: (bundles: Bundle[]) => void
31
+ /** whether this node is saying this component of this entity */
32
+ mine: Mine
33
+ /** the current patches, without consuming any waiting clears */
34
+ held: () => Bundle[]
35
+ /** everything to say again on a new connection */
36
+ again: () => Bundle[]
37
+ }
38
+
39
+ export let saying = (): Saying => {
40
+ let said = new Map<string, Said>()
41
+ let held = () => bundled([...said.values()])
42
+ return {
43
+ wrote: (bundles, carried) => {
44
+ for (let b of bundles) {
45
+ let eid = b.entity.eid
46
+ for (let [comp, patch] of comps(b)) {
47
+ let k = key(eid, comp)
48
+ if (patch != null) {
49
+ let was = said.get(k)?.[2]
50
+ said.set(k, [eid, comp, { ...was, ...patch }])
51
+ } else if (carried) said.delete(k)
52
+ else said.set(k, [eid, comp, null])
53
+ }
54
+ }
55
+ },
56
+ heard: (bundles) => {
57
+ for (let b of bundles) {
58
+ for (let [comp] of comps(b)) said.delete(key(b.entity.eid, comp))
59
+ }
60
+ },
61
+ mine: (eid, comp) => said.get(key(eid, comp))?.[2] != null,
62
+ held,
63
+ again: () => {
64
+ let out = held()
65
+ for (let [k, [, , patch]] of said) if (patch == null) said.delete(k)
66
+ return out
67
+ },
68
+ }
69
+ }