@yaks/member 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.
- package/LICENSE +202 -0
- package/README.md +471 -2
- package/_dist/asking.d.ts +4 -0
- package/_dist/asking.d.ts.map +1 -0
- package/_dist/comp.d.ts +14 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/deny.d.ts +19 -0
- package/_dist/deny.d.ts.map +1 -0
- package/_dist/graph.d.ts +4 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/guard.d.ts +45 -0
- package/_dist/guard.d.ts.map +1 -0
- package/_dist/keywords.d.ts +80 -0
- package/_dist/keywords.d.ts.map +1 -0
- package/_dist/mod.d.ts +112 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/owns.d.ts +18 -0
- package/_dist/owns.d.ts.map +1 -0
- package/_dist/pace.d.ts +48 -0
- package/_dist/pace.d.ts.map +1 -0
- package/_dist/personal.d.ts +46 -0
- package/_dist/personal.d.ts.map +1 -0
- package/_dist/plugin.d.ts +32 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/policy.d.ts +35 -0
- package/_dist/policy.d.ts.map +1 -0
- package/_dist/privacy.d.ts +55 -0
- package/_dist/privacy.d.ts.map +1 -0
- package/_dist/roles.d.ts +20 -0
- package/_dist/roles.d.ts.map +1 -0
- package/_dist/vocab.d.ts +10 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/words.d.ts +53 -0
- package/_dist/words.d.ts.map +1 -0
- package/asking.js +65 -0
- package/asking.js.map +1 -0
- package/asking.ts +77 -0
- package/comp.js +43 -0
- package/comp.js.map +1 -0
- package/comp.ts +52 -0
- package/deno.json +1 -0
- package/deny.js +31 -0
- package/deny.js.map +1 -0
- package/deny.ts +45 -0
- package/graph.js +6 -0
- package/graph.js.map +1 -0
- package/graph.ts +16 -0
- package/guard.js +265 -0
- package/guard.js.map +1 -0
- package/guard.ts +327 -0
- package/keywords.js +170 -0
- package/keywords.js.map +1 -0
- package/keywords.ts +190 -0
- package/meta/member.vocab.json +70 -0
- package/mod.js +112 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/owns.js +12 -0
- package/owns.js.map +1 -0
- package/owns.ts +23 -0
- package/pace.js +189 -0
- package/pace.js.map +1 -0
- package/pace.ts +195 -0
- package/package.json +33 -4
- package/personal.js +448 -0
- package/personal.js.map +1 -0
- package/personal.ts +550 -0
- package/plugin.js +63 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +62 -0
- package/policy.js +115 -0
- package/policy.js.map +1 -0
- package/policy.ts +176 -0
- package/privacy.js +293 -0
- package/privacy.js.map +1 -0
- package/privacy.ts +379 -0
- package/roles.js +164 -0
- package/roles.js.map +1 -0
- package/roles.ts +245 -0
- package/vocab.js +19 -0
- package/vocab.js.map +1 -0
- package/vocab.json +272 -0
- package/vocab.ts +20 -0
- package/words.js +75 -0
- package/words.js.map +1 -0
- package/words.ts +123 -0
package/pace.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// A stored component's pace is held per instrument, entity and component.
|
|
2
|
+
// The graph's entity stamps cannot keep these clocks: another instrument or
|
|
3
|
+
// an unrelated component can overwrite updated without changing this clock.
|
|
4
|
+
// `_pace{writes}` keeps the last accepted write of each key on its entity,
|
|
5
|
+
// inside the same transaction as the values it admits. Expired clocks are
|
|
6
|
+
// discarded on that entity's next paced write.
|
|
7
|
+
//
|
|
8
|
+
// A writer is the bundle's vouched via, whether signed in or out. All writers
|
|
9
|
+
// with no via share a clock on that entity's component. Authentication belongs
|
|
10
|
+
// to the receiving door, which replaces whatever actor the client claimed.
|
|
11
|
+
//
|
|
12
|
+
// Saying the same values again is no write and starts no clock. Clearing a
|
|
13
|
+
// component goes at once and leaves its clock intact; deleting the entity
|
|
14
|
+
// removes the clocks with it, through the graph's ordinary lifecycle.
|
|
15
|
+
import { after } from "@yaks/fp";
|
|
16
|
+
import { composed, dead, raced } from "@yaks/graph/bundle";
|
|
17
|
+
import { token } from "@yaks/graph/sha256";
|
|
18
|
+
import { writers } from "@yaks/graph/provenance";
|
|
19
|
+
import { paceOf, syncOf } from "@yaks/vocab";
|
|
20
|
+
/** A write refused for coming sooner than its component's pace. */ export class Paced extends Error {
|
|
21
|
+
actor;
|
|
22
|
+
comp;
|
|
23
|
+
pace;
|
|
24
|
+
wait;
|
|
25
|
+
entity;
|
|
26
|
+
/**
|
|
27
|
+
* @param actor the writing instrument, or `null` when no via was supplied
|
|
28
|
+
* @param comp the component they wrote
|
|
29
|
+
* @param pace how often one writer may write it, in ms
|
|
30
|
+
* @param wait how long until they may again, in ms
|
|
31
|
+
*/ constructor(actor, comp, pace, wait, entity){
|
|
32
|
+
super(`${comp} is written at most once every ${span(pace)} by each writer — ` + `${actor ?? 'a writer with no via'} may again in ${span(wait)}`), this.actor = actor, this.comp = comp, this.pace = pace, this.wait = wait, this.entity = entity;
|
|
33
|
+
this.name = 'Paced';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// A span as a person reads it: `1s`, `250ms`, `1.5s`.
|
|
37
|
+
let span = (ms)=>ms >= 1000 ? `${+(ms / 1000).toFixed(1)}s` : `${ms}ms`;
|
|
38
|
+
/**
|
|
39
|
+
* The paces a loaded vocabulary declares on stored components. A relayed one
|
|
40
|
+
* is @yaks/sync's to keep: its value never reaches a store.
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { assertEquals } from '@std/assert'
|
|
44
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
45
|
+
* import { pacesIn } from '@yaks/member'
|
|
46
|
+
* let v = loadVocab({
|
|
47
|
+
* $defs: {
|
|
48
|
+
* line: { component: true, type: 'object', pace: '1s' },
|
|
49
|
+
* spot: {
|
|
50
|
+
* component: true,
|
|
51
|
+
* type: 'object',
|
|
52
|
+
* sync: 'peers',
|
|
53
|
+
* durable: 'connection',
|
|
54
|
+
* pace: '100ms',
|
|
55
|
+
* },
|
|
56
|
+
* },
|
|
57
|
+
* })
|
|
58
|
+
* assertEquals(pacesIn(v), { line: 1000 })
|
|
59
|
+
* ```
|
|
60
|
+
*/ export let pacesIn = (v)=>Object.fromEntries(v.all.flatMap((c)=>{
|
|
61
|
+
let pace = paceOf(v, c);
|
|
62
|
+
return pace && syncOf(v, c) == 'server' ? [
|
|
63
|
+
[
|
|
64
|
+
c,
|
|
65
|
+
pace
|
|
66
|
+
]
|
|
67
|
+
] : [];
|
|
68
|
+
}));
|
|
69
|
+
let clocks = (row, paces, now, instrument)=>{
|
|
70
|
+
let ledger = row?._pace;
|
|
71
|
+
if (ledger && !instrument) return ledger.writes;
|
|
72
|
+
let recent = new Map();
|
|
73
|
+
let remember = (write)=>{
|
|
74
|
+
let via = write.via == null ? null : instrument?.(write.via) ?? write.via;
|
|
75
|
+
let key = JSON.stringify([
|
|
76
|
+
write.comp,
|
|
77
|
+
via
|
|
78
|
+
]);
|
|
79
|
+
if (write.at > (recent.get(key)?.at ?? -Infinity)) {
|
|
80
|
+
recent.set(key, {
|
|
81
|
+
...write,
|
|
82
|
+
via
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
if (ledger) {
|
|
87
|
+
for (let write of ledger.writes)remember(write);
|
|
88
|
+
return [
|
|
89
|
+
...recent.values()
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
// TODO: Remove this transition reader once pre-ledger rows have migrated.
|
|
93
|
+
// Their stamps lack component history, so conservatively initialize every
|
|
94
|
+
// worn paced component from both recent instruments. The first accepted
|
|
95
|
+
// write persists these clocks; all later checks read only the ledger.
|
|
96
|
+
for (let stamp of [
|
|
97
|
+
'created',
|
|
98
|
+
'updated'
|
|
99
|
+
]){
|
|
100
|
+
let s = row?.[stamp];
|
|
101
|
+
if (!s) continue;
|
|
102
|
+
let at = Date.parse(String(s.at));
|
|
103
|
+
if (!Number.isFinite(at)) continue;
|
|
104
|
+
let via = s.via ?? null;
|
|
105
|
+
for (let [comp, pace] of Object.entries(paces)){
|
|
106
|
+
if (!row?.[comp] || at + pace <= now) continue;
|
|
107
|
+
remember({
|
|
108
|
+
comp,
|
|
109
|
+
via,
|
|
110
|
+
at
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return [
|
|
115
|
+
...recent.values()
|
|
116
|
+
];
|
|
117
|
+
};
|
|
118
|
+
// A JSON-valued property read from storage is another object, but the same
|
|
119
|
+
// value is still a retry. Use the graph's own value tokens for that case.
|
|
120
|
+
let same = (a, b)=>a == b || typeof a == 'object' && typeof b == 'object' && token(a) == token(b);
|
|
121
|
+
// Whether this patch gives or changes a value; clearing goes at once.
|
|
122
|
+
let writes = (patch, held)=>!!patch && (!held || Object.entries(patch).some(([p, v])=>!same(v, held[p])));
|
|
123
|
+
/**
|
|
124
|
+
* Refuse writes sooner than the component's pace permits their via on that
|
|
125
|
+
* entity. Append the accepted clocks as server-owned patches for the graph to
|
|
126
|
+
* persist with the batch. Distinct entities, components and vias hold
|
|
127
|
+
* independent clocks; all writers with no via share one on each component.
|
|
128
|
+
* `now` is the moment the batch is taken, in ms.
|
|
129
|
+
*/ export let pacing = (paces, tx, bundles, now = Date.now(), instrument)=>{
|
|
130
|
+
if (!bundles.some((b)=>Object.keys(paces).some((c)=>b[c]))) {
|
|
131
|
+
return bundles;
|
|
132
|
+
}
|
|
133
|
+
let eids = [
|
|
134
|
+
...new Set(bundles.map((b)=>b.entity.eid))
|
|
135
|
+
];
|
|
136
|
+
let writer = writers(bundles);
|
|
137
|
+
return after(tx.get(eids), (rows)=>{
|
|
138
|
+
let held = new Map(rows.map((r)=>[
|
|
139
|
+
r.entity.eid,
|
|
140
|
+
r
|
|
141
|
+
]));
|
|
142
|
+
// A transaction writes one resulting value per component. Deletion wins,
|
|
143
|
+
// and the graph swallows a patch that raced a tombstone; neither may gain
|
|
144
|
+
// a clock patch that would bring a deleted entity back.
|
|
145
|
+
let patches = composed(bundles.filter((b)=>{
|
|
146
|
+
let row = held.get(b.entity.eid);
|
|
147
|
+
return !row || !dead(row) || !raced(b);
|
|
148
|
+
}));
|
|
149
|
+
let added = [];
|
|
150
|
+
for (let b of patches){
|
|
151
|
+
if (dead(b)) continue;
|
|
152
|
+
let eid = b.entity.eid;
|
|
153
|
+
let row = held.get(eid);
|
|
154
|
+
let wrote = writer(eid).via;
|
|
155
|
+
let via = wrote == null ? null : instrument?.(wrote) ?? wrote;
|
|
156
|
+
let recent = clocks(row, paces, now, instrument);
|
|
157
|
+
let next;
|
|
158
|
+
for (let [comp, pace] of Object.entries(paces)){
|
|
159
|
+
if (!writes(b[comp], row?.[comp])) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
let last = recent.find((w)=>w.comp == comp && w.via == via);
|
|
163
|
+
let wait = last ? last.at + pace - now : 0;
|
|
164
|
+
if (wait > 0) throw new Paced(via, comp, pace, wait, eid);
|
|
165
|
+
next ??= recent.filter((w)=>w.at + (paces[w.comp] ?? 0) > now);
|
|
166
|
+
next.push({
|
|
167
|
+
comp,
|
|
168
|
+
via,
|
|
169
|
+
at: now
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (next) {
|
|
173
|
+
added.push({
|
|
174
|
+
entity: b.entity,
|
|
175
|
+
_pace: {
|
|
176
|
+
writes: next
|
|
177
|
+
},
|
|
178
|
+
$actor: writer(eid),
|
|
179
|
+
$quiet: true
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return added.length ? [
|
|
184
|
+
...bundles,
|
|
185
|
+
...added
|
|
186
|
+
] : bundles;
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
//# sourceMappingURL=pace.js.map
|
package/pace.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pace.js","sources":["./pace.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,4EAA4E;AAC5E,2EAA2E;AAC3E,0EAA0E;AAC1E,+CAA+C;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AI,CAAC,CAAC,IAAM,EAAE,IAAI,IAAI,QAAQ,EAAE,GAAG,IAAI;QACzD,IAAI,OAAO,OAAO,KAAK,EAAE,GAAG,OAAO,MAAM;QACzC,IAAI,OAAO,GAAG,MAAM,IAAI,MAAM,KAAK,MAAM,MAAM,MAAM;QACrD,SAAS,OAAO,MAAM,CAAC,CAAC,IAAM,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI;QAC5D,KAAK,IAAI,CAAC;UAAE;UAAM;UAAK,IAAI;QAAI;MACjC;MACA,IAAI,MAAM;QACR,MAAM,IAAI,CAAC;UACT,QAAQ,EAAE,MAAM;UAChB,OAAO;YAAE,QAAQ;UAAK;UACtB,QAAQ,OAAO;UACf,QAAQ;QACV;MACF;IACF;IACA,OAAO,MAAM,MAAM,GAAG;SAAI;SAAY;KAAM,GAAG;EACjD;AACF,EAAC"}
|
package/pace.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// A stored component's pace is held per instrument, entity and component.
|
|
2
|
+
// The graph's entity stamps cannot keep these clocks: another instrument or
|
|
3
|
+
// an unrelated component can overwrite updated without changing this clock.
|
|
4
|
+
// `_pace{writes}` keeps the last accepted write of each key on its entity,
|
|
5
|
+
// inside the same transaction as the values it admits. Expired clocks are
|
|
6
|
+
// discarded on that entity's next paced write.
|
|
7
|
+
//
|
|
8
|
+
// A writer is the bundle's vouched via, whether signed in or out. All writers
|
|
9
|
+
// with no via share a clock on that entity's component. Authentication belongs
|
|
10
|
+
// to the receiving door, which replaces whatever actor the client claimed.
|
|
11
|
+
//
|
|
12
|
+
// Saying the same values again is no write and starts no clock. Clearing a
|
|
13
|
+
// component goes at once and leaves its clock intact; deleting the entity
|
|
14
|
+
// removes the clocks with it, through the graph's ordinary lifecycle.
|
|
15
|
+
|
|
16
|
+
import type { Bundle, Comp, Eid, ReadTx } from 'jsr:@yaks/graph@0.2.36'
|
|
17
|
+
import { after } from '@yaks/fp'
|
|
18
|
+
import { composed, dead, raced } from '@yaks/graph/bundle'
|
|
19
|
+
import { token } from '@yaks/graph/sha256'
|
|
20
|
+
import { writers } from '@yaks/graph/provenance'
|
|
21
|
+
import { paceOf, syncOf, type Vocab } from '@yaks/vocab'
|
|
22
|
+
|
|
23
|
+
/** How often one via writes one entity’s component, in ms, by name. */
|
|
24
|
+
export type Paces = Record<string, number>
|
|
25
|
+
export type Instrument = (via: Eid) => Eid
|
|
26
|
+
|
|
27
|
+
/** A write refused for coming sooner than its component's pace. */
|
|
28
|
+
export class Paced extends Error {
|
|
29
|
+
/**
|
|
30
|
+
* @param actor the writing instrument, or `null` when no via was supplied
|
|
31
|
+
* @param comp the component they wrote
|
|
32
|
+
* @param pace how often one writer may write it, in ms
|
|
33
|
+
* @param wait how long until they may again, in ms
|
|
34
|
+
*/
|
|
35
|
+
constructor(
|
|
36
|
+
public actor: Eid | null,
|
|
37
|
+
public comp: string,
|
|
38
|
+
public pace: number,
|
|
39
|
+
public wait: number,
|
|
40
|
+
public entity?: Eid,
|
|
41
|
+
) {
|
|
42
|
+
super(
|
|
43
|
+
`${comp} is written at most once every ${span(pace)} by each writer — ` +
|
|
44
|
+
`${actor ?? 'a writer with no via'} may again in ${span(wait)}`,
|
|
45
|
+
)
|
|
46
|
+
this.name = 'Paced'
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A span as a person reads it: `1s`, `250ms`, `1.5s`.
|
|
51
|
+
let span = (ms: number) =>
|
|
52
|
+
ms >= 1000 ? `${+(ms / 1000).toFixed(1)}s` : `${ms}ms`
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The paces a loaded vocabulary declares on stored components. A relayed one
|
|
56
|
+
* is @yaks/sync's to keep: its value never reaches a store.
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { assertEquals } from '@std/assert'
|
|
60
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
61
|
+
* import { pacesIn } from '@yaks/member'
|
|
62
|
+
* let v = loadVocab({
|
|
63
|
+
* $defs: {
|
|
64
|
+
* line: { component: true, type: 'object', pace: '1s' },
|
|
65
|
+
* spot: {
|
|
66
|
+
* component: true,
|
|
67
|
+
* type: 'object',
|
|
68
|
+
* sync: 'peers',
|
|
69
|
+
* durable: 'connection',
|
|
70
|
+
* pace: '100ms',
|
|
71
|
+
* },
|
|
72
|
+
* },
|
|
73
|
+
* })
|
|
74
|
+
* assertEquals(pacesIn(v), { line: 1000 })
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export let pacesIn = (v: Vocab): Paces =>
|
|
78
|
+
Object.fromEntries(
|
|
79
|
+
v.all.flatMap((c) => {
|
|
80
|
+
let pace = paceOf(v, c)
|
|
81
|
+
return pace && syncOf(v, c) == 'server' ? [[c, pace]] : []
|
|
82
|
+
}),
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
// One independent clock. The ledger is read whole, never queried inside.
|
|
86
|
+
type Write = { comp: string; via: Eid | null; at: number }
|
|
87
|
+
|
|
88
|
+
let clocks = (
|
|
89
|
+
row: Bundle | undefined,
|
|
90
|
+
paces: Paces,
|
|
91
|
+
now: number,
|
|
92
|
+
instrument?: Instrument,
|
|
93
|
+
): Write[] => {
|
|
94
|
+
let ledger = row?._pace as Comp | undefined
|
|
95
|
+
if (ledger && !instrument) return ledger.writes as Write[]
|
|
96
|
+
let recent = new Map<string, Write>()
|
|
97
|
+
let remember = (write: Write) => {
|
|
98
|
+
let via = write.via == null ? null : instrument?.(write.via) ?? write.via
|
|
99
|
+
let key = JSON.stringify([write.comp, via])
|
|
100
|
+
if (write.at > (recent.get(key)?.at ?? -Infinity)) {
|
|
101
|
+
recent.set(key, { ...write, via })
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (ledger) {
|
|
105
|
+
for (let write of ledger.writes as Write[]) remember(write)
|
|
106
|
+
return [...recent.values()]
|
|
107
|
+
}
|
|
108
|
+
// TODO: Remove this transition reader once pre-ledger rows have migrated.
|
|
109
|
+
// Their stamps lack component history, so conservatively initialize every
|
|
110
|
+
// worn paced component from both recent instruments. The first accepted
|
|
111
|
+
// write persists these clocks; all later checks read only the ledger.
|
|
112
|
+
for (let stamp of ['created', 'updated']) {
|
|
113
|
+
let s = row?.[stamp] as Comp | undefined
|
|
114
|
+
if (!s) continue
|
|
115
|
+
let at = Date.parse(String(s.at))
|
|
116
|
+
if (!Number.isFinite(at)) continue
|
|
117
|
+
let via = s.via as Eid | undefined ?? null
|
|
118
|
+
for (let [comp, pace] of Object.entries(paces)) {
|
|
119
|
+
if (!row?.[comp] || at + pace <= now) continue
|
|
120
|
+
remember({ comp, via, at })
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return [...recent.values()]
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// A JSON-valued property read from storage is another object, but the same
|
|
127
|
+
// value is still a retry. Use the graph's own value tokens for that case.
|
|
128
|
+
let same = (a: unknown, b: unknown) =>
|
|
129
|
+
a == b ||
|
|
130
|
+
typeof a == 'object' && typeof b == 'object' && token(a) == token(b)
|
|
131
|
+
|
|
132
|
+
// Whether this patch gives or changes a value; clearing goes at once.
|
|
133
|
+
let writes = (patch: Comp | null, held: Comp | undefined) =>
|
|
134
|
+
!!patch &&
|
|
135
|
+
(!held || Object.entries(patch).some(([p, v]) => !same(v, held[p])))
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Refuse writes sooner than the component's pace permits their via on that
|
|
139
|
+
* entity. Append the accepted clocks as server-owned patches for the graph to
|
|
140
|
+
* persist with the batch. Distinct entities, components and vias hold
|
|
141
|
+
* independent clocks; all writers with no via share one on each component.
|
|
142
|
+
* `now` is the moment the batch is taken, in ms.
|
|
143
|
+
*/
|
|
144
|
+
export let pacing = (
|
|
145
|
+
paces: Paces,
|
|
146
|
+
tx: ReadTx,
|
|
147
|
+
bundles: Bundle[],
|
|
148
|
+
now: number = Date.now(),
|
|
149
|
+
instrument?: Instrument,
|
|
150
|
+
): Bundle[] | Promise<Bundle[]> => {
|
|
151
|
+
if (!bundles.some((b) => Object.keys(paces).some((c) => b[c]))) {
|
|
152
|
+
return bundles
|
|
153
|
+
}
|
|
154
|
+
let eids = [...new Set(bundles.map((b) => b.entity.eid))]
|
|
155
|
+
let writer = writers(bundles)
|
|
156
|
+
return after(tx.get(eids), (rows) => {
|
|
157
|
+
let held = new Map(rows.map((r) => [r.entity.eid, r]))
|
|
158
|
+
// A transaction writes one resulting value per component. Deletion wins,
|
|
159
|
+
// and the graph swallows a patch that raced a tombstone; neither may gain
|
|
160
|
+
// a clock patch that would bring a deleted entity back.
|
|
161
|
+
let patches = composed(bundles.filter((b) => {
|
|
162
|
+
let row = held.get(b.entity.eid)
|
|
163
|
+
return !row || !dead(row) || !raced(b)
|
|
164
|
+
}))
|
|
165
|
+
let added: Bundle[] = []
|
|
166
|
+
for (let b of patches) {
|
|
167
|
+
if (dead(b)) continue
|
|
168
|
+
let eid = b.entity.eid
|
|
169
|
+
let row = held.get(eid)
|
|
170
|
+
let wrote = writer(eid).via
|
|
171
|
+
let via = wrote == null ? null : instrument?.(wrote) ?? wrote
|
|
172
|
+
let recent = clocks(row, paces, now, instrument)
|
|
173
|
+
let next: Write[] | undefined
|
|
174
|
+
for (let [comp, pace] of Object.entries(paces)) {
|
|
175
|
+
if (!writes(b[comp] as Comp | null, row?.[comp] as Comp | undefined)) {
|
|
176
|
+
continue
|
|
177
|
+
}
|
|
178
|
+
let last = recent.find((w) => w.comp == comp && w.via == via)
|
|
179
|
+
let wait = last ? last.at + pace - now : 0
|
|
180
|
+
if (wait > 0) throw new Paced(via, comp, pace, wait, eid)
|
|
181
|
+
next ??= recent.filter((w) => w.at + (paces[w.comp] ?? 0) > now)
|
|
182
|
+
next.push({ comp, via, at: now })
|
|
183
|
+
}
|
|
184
|
+
if (next) {
|
|
185
|
+
added.push({
|
|
186
|
+
entity: b.entity,
|
|
187
|
+
_pace: { writes: next },
|
|
188
|
+
$actor: writer(eid),
|
|
189
|
+
$quiet: true,
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return added.length ? [...bundles, ...added] : bundles
|
|
194
|
+
})
|
|
195
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/member",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/member",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./vocab": {
|
|
12
|
+
"types": "./_dist/vocab.d.ts",
|
|
13
|
+
"default": "./vocab.js"
|
|
14
|
+
},
|
|
15
|
+
"./graph": {
|
|
16
|
+
"types": "./_dist/graph.d.ts",
|
|
17
|
+
"default": "./graph.js"
|
|
18
|
+
},
|
|
19
|
+
"./words": {
|
|
20
|
+
"types": "./_dist/words.d.ts",
|
|
21
|
+
"default": "./words.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@yaks/fp": "0.2.36",
|
|
27
|
+
"@yaks/graph": "0.2.36",
|
|
28
|
+
"@yaks/host": "0.2.36",
|
|
29
|
+
"@yaks/match": "0.2.36",
|
|
30
|
+
"@yaks/query": "0.2.36",
|
|
31
|
+
"@yaks/vocab": "0.2.36"
|
|
32
|
+
},
|
|
33
|
+
"description": "Access control for a graph: space membership, per-app permission grants, and access modes, with the checks that enforce them on reads and writes.",
|
|
34
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
35
|
+
}
|