@yaks/effects 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 +979 -2
- package/_dist/docs.d.ts +10 -0
- package/_dist/docs.d.ts.map +1 -0
- package/_dist/enqueue.d.ts +12 -0
- package/_dist/enqueue.d.ts.map +1 -0
- package/_dist/expire.d.ts +31 -0
- package/_dist/expire.d.ts.map +1 -0
- package/_dist/invocation.d.ts +36 -0
- package/_dist/invocation.d.ts.map +1 -0
- package/_dist/lease.d.ts +98 -0
- package/_dist/lease.d.ts.map +1 -0
- package/_dist/mod.d.ts +89 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/outage.d.ts +48 -0
- package/_dist/outage.d.ts.map +1 -0
- package/_dist/pool.d.ts +148 -0
- package/_dist/pool.d.ts.map +1 -0
- package/_dist/provisional.d.ts +8 -0
- package/_dist/provisional.d.ts.map +1 -0
- package/_dist/ready.d.ts +3 -0
- package/_dist/ready.d.ts.map +1 -0
- package/_dist/registration.d.ts +17 -0
- package/_dist/registration.d.ts.map +1 -0
- package/_dist/registry.d.ts +153 -0
- package/_dist/registry.d.ts.map +1 -0
- package/_dist/tools.d.ts +21 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/trace.d.ts +63 -0
- package/_dist/trace.d.ts.map +1 -0
- package/_dist/views.d.ts +15 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +11 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/worker.d.ts +19 -0
- package/_dist/worker.d.ts.map +1 -0
- package/_dist/write.d.ts +25 -0
- package/_dist/write.d.ts.map +1 -0
- package/cleanup.js +75 -0
- package/cleanup.js.map +1 -0
- package/cleanup.ts +83 -0
- package/deno.json +1 -0
- package/docs.js +13 -0
- package/docs.js.map +1 -0
- package/docs.ts +13 -0
- package/enqueue.js +71 -0
- package/enqueue.js.map +1 -0
- package/enqueue.ts +86 -0
- package/expire.js +225 -0
- package/expire.js.map +1 -0
- package/expire.ts +244 -0
- package/invocation.js +245 -0
- package/invocation.js.map +1 -0
- package/invocation.ts +250 -0
- package/lease.js +343 -0
- package/lease.js.map +1 -0
- package/lease.ts +444 -0
- package/mod.js +89 -0
- package/mod.js.map +1 -0
- package/mod.ts +92 -0
- package/outage.js +77 -0
- package/outage.js.map +1 -0
- package/outage.ts +84 -0
- package/package.json +52 -4
- package/page.js +315 -0
- package/page.js.map +1 -0
- package/page.ts +524 -0
- package/pool.js +1474 -0
- package/pool.js.map +1 -0
- package/pool.ts +1635 -0
- package/provisional.js +20 -0
- package/provisional.js.map +1 -0
- package/provisional.ts +26 -0
- package/ready.js +48 -0
- package/ready.js.map +1 -0
- package/ready.ts +93 -0
- package/registration.js +21 -0
- package/registration.js.map +1 -0
- package/registration.ts +43 -0
- package/registry.js +872 -0
- package/registry.js.map +1 -0
- package/registry.ts +1196 -0
- package/tools.js +182 -0
- package/tools.js.map +1 -0
- package/tools.ts +229 -0
- package/trace.js +167 -0
- package/trace.js.map +1 -0
- package/trace.ts +217 -0
- package/views.js +18 -0
- package/views.js.map +1 -0
- package/views.ts +18 -0
- package/vocab.js +18 -0
- package/vocab.js.map +1 -0
- package/vocab.json +364 -0
- package/vocab.ts +20 -0
- package/worker.js +114 -0
- package/worker.js.map +1 -0
- package/worker.ts +146 -0
- package/write.js +38 -0
- package/write.js.map +1 -0
- package/write.ts +60 -0
package/worker.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// What a worker says about itself. A process working the pool (./pool.ts)
|
|
2
|
+
// writes a `worker` component on its own process entity every {@link SAY}
|
|
3
|
+
// while it works:
|
|
4
|
+
//
|
|
5
|
+
// worker{at, running, runs, ran, errors, history, cpu, lag}
|
|
6
|
+
//
|
|
7
|
+
// so anyone reading the graph sees how many workers are up, what each is
|
|
8
|
+
// running, how fast runs end and how busy each process is, without asking the
|
|
9
|
+
// process. A row whose `at` stops moving is a worker that stopped saying: it
|
|
10
|
+
// ended, or its thread is stuck. One leaving the pool takes its row with it,
|
|
11
|
+
// and one killed before it could is cleared by the next worker to start.
|
|
12
|
+
//
|
|
13
|
+
// Saying is a commit every reader of the graph's feed handles, so a worker
|
|
14
|
+
// says itself no more often than its presence lease is renewed (./lease.ts
|
|
15
|
+
// `HOLD`): `history` keeps each second, so the runs a second still read by
|
|
16
|
+
// the second, a few seconds late.
|
|
17
|
+
//
|
|
18
|
+
// How busy is said in two ways that hold however many runs a worker takes at
|
|
19
|
+
// once: `cpu`, the share of a core its process used, and `lag`, how late its
|
|
20
|
+
// thread ran a timer, which climbs as its event loop fills.
|
|
21
|
+
//
|
|
22
|
+
// The counting and the reading are pure; the one timer is `lagging`'s.
|
|
23
|
+
/** The component a worker says itself in, by the name ./vocab.json declares
|
|
24
|
+
* it. */ export let WORKER = 'worker';
|
|
25
|
+
/** How often a worker says itself (ms): as often as it renews its presence
|
|
26
|
+
* lease. */ export let SAY = 15_000;
|
|
27
|
+
/** How many seconds of `history` a worker keeps. */ export let SPAN = 60;
|
|
28
|
+
/** How many of its runs a worker lists, oldest first. */ export let SHOWN = 20;
|
|
29
|
+
/**
|
|
30
|
+
* Runs counted by the second they ended in, kept for `span` seconds: `count`
|
|
31
|
+
* one at a moment (ms), and `last` reads the `span` whole seconds before a
|
|
32
|
+
* moment, oldest first.
|
|
33
|
+
*/ /// let s = seconds(3)
|
|
34
|
+
/// s.count(1_000); s.count(1_500); s.count(2_100)
|
|
35
|
+
/// s.last(3_000) -> [0, 2, 1]
|
|
36
|
+
/// s.last(5_000) -> [1, 0, 0]
|
|
37
|
+
/// s.last(9_000) -> [0, 0, 0]
|
|
38
|
+
export let seconds = (span = SPAN)=>{
|
|
39
|
+
// One slot more than the span: the second still going.
|
|
40
|
+
let size = span + 1;
|
|
41
|
+
let counts = Array(size).fill(0);
|
|
42
|
+
let top = -Infinity;
|
|
43
|
+
let slot = (s)=>(s % size + size) % size;
|
|
44
|
+
// Up to second `s`, emptying the seconds passed over.
|
|
45
|
+
let roll = (s)=>{
|
|
46
|
+
if (s <= top) return;
|
|
47
|
+
if (s - top >= size) counts.fill(0);
|
|
48
|
+
else for(let i = top + 1; i <= s; i++)counts[slot(i)] = 0;
|
|
49
|
+
top = s;
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
count: (ms)=>{
|
|
53
|
+
let s = Math.floor(ms / 1000);
|
|
54
|
+
roll(s);
|
|
55
|
+
if (s > top - size) counts[slot(s)]++;
|
|
56
|
+
},
|
|
57
|
+
last: (ms)=>{
|
|
58
|
+
let s = Math.floor(ms / 1000);
|
|
59
|
+
roll(s);
|
|
60
|
+
return Array.from({
|
|
61
|
+
length: span
|
|
62
|
+
}, (_, i)=>counts[slot(s - span + i)]);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/** Runs ended a second, over the last `n` seconds of a history. */ /// rate([9, 0, 0, 4, 6], 2) -> 5
|
|
67
|
+
/// rate([1, 2]) -> 0.3
|
|
68
|
+
/// rate([]) -> 0
|
|
69
|
+
export let rate = (history, n = 10)=>history.slice(-n).reduce((a, b)=>a + b, 0) / n;
|
|
70
|
+
/**
|
|
71
|
+
* How late this thread runs a timer: one is set every `every` ms, and `read`
|
|
72
|
+
* answers the most any ran past its time since the last read, in whole ms.
|
|
73
|
+
* `stop` clears it.
|
|
74
|
+
*/ export let lagging = (every = 100)=>{
|
|
75
|
+
let worst = 0;
|
|
76
|
+
let due = performance.now() + every;
|
|
77
|
+
let look = ()=>{
|
|
78
|
+
let now = performance.now();
|
|
79
|
+
worst = Math.max(worst, now - due);
|
|
80
|
+
due = now + every;
|
|
81
|
+
timer = setTimeout(look, every);
|
|
82
|
+
};
|
|
83
|
+
let timer = setTimeout(look, every);
|
|
84
|
+
return {
|
|
85
|
+
read: ()=>{
|
|
86
|
+
let w = Math.max(0, Math.round(worst));
|
|
87
|
+
worst = 0;
|
|
88
|
+
return w;
|
|
89
|
+
},
|
|
90
|
+
stop: ()=>clearTimeout(timer)
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
/** A worker's row read back, or nothing where there is none. */ /// said({ worker: { at: 'x', running: 2 } })?.running -> 2
|
|
94
|
+
/// said({ worker: { at: 'x' } })?.runs -> []
|
|
95
|
+
/// said({}) -> undefined
|
|
96
|
+
export let said = (b)=>{
|
|
97
|
+
let w = b[WORKER];
|
|
98
|
+
if (!w?.at) return undefined;
|
|
99
|
+
return {
|
|
100
|
+
at: w.at,
|
|
101
|
+
running: w.running ?? 0,
|
|
102
|
+
runs: w.runs ?? [],
|
|
103
|
+
ran: w.ran ?? 0,
|
|
104
|
+
errors: w.errors ?? 0,
|
|
105
|
+
history: w.history ?? [],
|
|
106
|
+
...w.cpu != null ? {
|
|
107
|
+
cpu: w.cpu
|
|
108
|
+
} : {},
|
|
109
|
+
...w.lag != null ? {
|
|
110
|
+
lag: w.lag
|
|
111
|
+
} : {}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
//# sourceMappingURL=worker.js.map
|
package/worker.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sources":["./worker.ts"],"names":[],"mappings":"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"}
|
package/worker.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// What a worker says about itself. A process working the pool (./pool.ts)
|
|
2
|
+
// writes a `worker` component on its own process entity every {@link SAY}
|
|
3
|
+
// while it works:
|
|
4
|
+
//
|
|
5
|
+
// worker{at, running, runs, ran, errors, history, cpu, lag}
|
|
6
|
+
//
|
|
7
|
+
// so anyone reading the graph sees how many workers are up, what each is
|
|
8
|
+
// running, how fast runs end and how busy each process is, without asking the
|
|
9
|
+
// process. A row whose `at` stops moving is a worker that stopped saying: it
|
|
10
|
+
// ended, or its thread is stuck. One leaving the pool takes its row with it,
|
|
11
|
+
// and one killed before it could is cleared by the next worker to start.
|
|
12
|
+
//
|
|
13
|
+
// Saying is a commit every reader of the graph's feed handles, so a worker
|
|
14
|
+
// says itself no more often than its presence lease is renewed (./lease.ts
|
|
15
|
+
// `HOLD`): `history` keeps each second, so the runs a second still read by
|
|
16
|
+
// the second, a few seconds late.
|
|
17
|
+
//
|
|
18
|
+
// How busy is said in two ways that hold however many runs a worker takes at
|
|
19
|
+
// once: `cpu`, the share of a core its process used, and `lag`, how late its
|
|
20
|
+
// thread ran a timer, which climbs as its event loop fills.
|
|
21
|
+
//
|
|
22
|
+
// The counting and the reading are pure; the one timer is `lagging`'s.
|
|
23
|
+
|
|
24
|
+
/** The component a worker says itself in, by the name ./vocab.json declares
|
|
25
|
+
* it. */
|
|
26
|
+
export let WORKER = 'worker'
|
|
27
|
+
|
|
28
|
+
/** How often a worker says itself (ms): as often as it renews its presence
|
|
29
|
+
* lease. */
|
|
30
|
+
export let SAY = 15_000
|
|
31
|
+
|
|
32
|
+
/** How many seconds of `history` a worker keeps. */
|
|
33
|
+
export let SPAN = 60
|
|
34
|
+
|
|
35
|
+
/** How many of its runs a worker lists, oldest first. */
|
|
36
|
+
export let SHOWN = 20
|
|
37
|
+
|
|
38
|
+
/** A run going in a worker, as it lists it. */
|
|
39
|
+
export type Listed = {
|
|
40
|
+
/** the run's `effect` row */
|
|
41
|
+
run: string
|
|
42
|
+
handler: string
|
|
43
|
+
target: string
|
|
44
|
+
/** when it started, ISO-8601 */
|
|
45
|
+
since: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What a worker says: the `worker` component. */
|
|
49
|
+
export type Worker = {
|
|
50
|
+
at: string
|
|
51
|
+
running: number
|
|
52
|
+
runs: Listed[]
|
|
53
|
+
ran: number
|
|
54
|
+
errors: number
|
|
55
|
+
history: number[]
|
|
56
|
+
cpu?: number
|
|
57
|
+
lag?: number
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Runs counted by the second they ended in, kept for `span` seconds: `count`
|
|
62
|
+
* one at a moment (ms), and `last` reads the `span` whole seconds before a
|
|
63
|
+
* moment, oldest first.
|
|
64
|
+
*/
|
|
65
|
+
/// let s = seconds(3)
|
|
66
|
+
/// s.count(1_000); s.count(1_500); s.count(2_100)
|
|
67
|
+
/// s.last(3_000) -> [0, 2, 1]
|
|
68
|
+
/// s.last(5_000) -> [1, 0, 0]
|
|
69
|
+
/// s.last(9_000) -> [0, 0, 0]
|
|
70
|
+
export let seconds = (span = SPAN) => {
|
|
71
|
+
// One slot more than the span: the second still going.
|
|
72
|
+
let size = span + 1
|
|
73
|
+
let counts: number[] = Array(size).fill(0)
|
|
74
|
+
let top = -Infinity
|
|
75
|
+
let slot = (s: number) => ((s % size) + size) % size
|
|
76
|
+
// Up to second `s`, emptying the seconds passed over.
|
|
77
|
+
let roll = (s: number) => {
|
|
78
|
+
if (s <= top) return
|
|
79
|
+
if (s - top >= size) counts.fill(0)
|
|
80
|
+
else for (let i = top + 1; i <= s; i++) counts[slot(i)] = 0
|
|
81
|
+
top = s
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
count: (ms: number) => {
|
|
85
|
+
let s = Math.floor(ms / 1000)
|
|
86
|
+
roll(s)
|
|
87
|
+
if (s > top - size) counts[slot(s)]++
|
|
88
|
+
},
|
|
89
|
+
last: (ms: number): number[] => {
|
|
90
|
+
let s = Math.floor(ms / 1000)
|
|
91
|
+
roll(s)
|
|
92
|
+
return Array.from({ length: span }, (_, i) => counts[slot(s - span + i)])
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Runs ended a second, over the last `n` seconds of a history. */
|
|
98
|
+
/// rate([9, 0, 0, 4, 6], 2) -> 5
|
|
99
|
+
/// rate([1, 2]) -> 0.3
|
|
100
|
+
/// rate([]) -> 0
|
|
101
|
+
export let rate = (history: number[], n = 10): number =>
|
|
102
|
+
history.slice(-n).reduce((a, b) => a + b, 0) / n
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* How late this thread runs a timer: one is set every `every` ms, and `read`
|
|
106
|
+
* answers the most any ran past its time since the last read, in whole ms.
|
|
107
|
+
* `stop` clears it.
|
|
108
|
+
*/
|
|
109
|
+
export let lagging = (every = 100) => {
|
|
110
|
+
let worst = 0
|
|
111
|
+
let due = performance.now() + every
|
|
112
|
+
let look = () => {
|
|
113
|
+
let now = performance.now()
|
|
114
|
+
worst = Math.max(worst, now - due)
|
|
115
|
+
due = now + every
|
|
116
|
+
timer = setTimeout(look, every)
|
|
117
|
+
}
|
|
118
|
+
let timer = setTimeout(look, every)
|
|
119
|
+
return {
|
|
120
|
+
read: (): number => {
|
|
121
|
+
let w = Math.max(0, Math.round(worst))
|
|
122
|
+
worst = 0
|
|
123
|
+
return w
|
|
124
|
+
},
|
|
125
|
+
stop: () => clearTimeout(timer),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** A worker's row read back, or nothing where there is none. */
|
|
130
|
+
/// said({ worker: { at: 'x', running: 2 } })?.running -> 2
|
|
131
|
+
/// said({ worker: { at: 'x' } })?.runs -> []
|
|
132
|
+
/// said({}) -> undefined
|
|
133
|
+
export let said = (b: Record<string, unknown>): Worker | undefined => {
|
|
134
|
+
let w = b[WORKER] as Partial<Worker> | undefined
|
|
135
|
+
if (!w?.at) return undefined
|
|
136
|
+
return {
|
|
137
|
+
at: w.at,
|
|
138
|
+
running: w.running ?? 0,
|
|
139
|
+
runs: w.runs ?? [],
|
|
140
|
+
ran: w.ran ?? 0,
|
|
141
|
+
errors: w.errors ?? 0,
|
|
142
|
+
history: w.history ?? [],
|
|
143
|
+
...w.cpu != null ? { cpu: w.cpu } : {},
|
|
144
|
+
...w.lag != null ? { lag: w.lag } : {},
|
|
145
|
+
}
|
|
146
|
+
}
|
package/write.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// How an effect writes back, and what stops it looping.
|
|
2
|
+
//
|
|
3
|
+
// A handler that only reads is served by the detached transaction it already
|
|
4
|
+
// holds. A handler that writes is a different matter: `tx.patch` puts rows
|
|
5
|
+
// straight into storage, underneath the whole pipeline — no admission, no
|
|
6
|
+
// stamped properties, no journal row, no subscriber notified, and no other
|
|
7
|
+
// effect ever hearing about it. That is how the outcome of a letter became a
|
|
8
|
+
// row a page only found on its next query, instead of an update it was pushed
|
|
9
|
+
// (T-34044). So an effect's write is a new batch — bundles applied in
|
|
10
|
+
// one transaction — through the graph's own `apply()`, after the commit that
|
|
11
|
+
// triggered the handler.
|
|
12
|
+
//
|
|
13
|
+
// That invites a loop, and the loop is stopped here rather than by a rule in
|
|
14
|
+
// every handler. Each batch records how many effect-written generations deep it
|
|
15
|
+
// is, under `$effect`: a `$`-prefixed key, so it belongs to `apply()`'s
|
|
16
|
+
// pipeline and is never a property — the same mechanism `$before` uses to carry
|
|
17
|
+
// a reading from one phase to a later one. The registry triggers nothing for a
|
|
18
|
+
// batch past the depth it allows, so an effect that writes the component it
|
|
19
|
+
// watches runs a bounded number of times and stops, whatever it writes and
|
|
20
|
+
// however it is registered.
|
|
21
|
+
/** The key a batch's generation rides under. Not a component: `$`-prefixed
|
|
22
|
+
* keys are `apply()`'s pipeline, never properties, so no storage adapter sees
|
|
23
|
+
* it. */ export let ORIGIN = '$effect';
|
|
24
|
+
/** How deep in effect-written batches this one is: `0` for a batch that came
|
|
25
|
+
* from a client, `1` for an effect's own write, one more for each write an
|
|
26
|
+
* effect makes about that one. */ export let generation = (bundles)=>Number(bundles.find((b)=>b[ORIGIN] != null)?.[ORIGIN] ?? 0);
|
|
27
|
+
/** The same batch, marked as an effect's own at generation `gen`. Copies, so
|
|
28
|
+
* the handler's own bundles are never edited under it. */ export let marked = (bundles, gen)=>bundles.map((b)=>({
|
|
29
|
+
...b,
|
|
30
|
+
[ORIGIN]: String(gen)
|
|
31
|
+
}));
|
|
32
|
+
/** Take the mark back off, so what a caller gets back is the batch as applied
|
|
33
|
+
* and nothing else. In place, like {@link strip} — the bundles it edits are
|
|
34
|
+
* the copies this package made. */ export let unmark = (bundles)=>{
|
|
35
|
+
for (let b of bundles)if (ORIGIN in b) delete b[ORIGIN];
|
|
36
|
+
return bundles;
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=write.js.map
|
package/write.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write.js","sources":["./write.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,0EAA0E;AAC1E,2EAA2E;AAC3E,6EAA6E;AAC7E,8EAA8E;AAC9E,sEAAsE;AACtE,6EAA6E;AAC7E,yBAAyB;AACzB,EAAE;AACF,6EAA6E;AAC7E,gFAAgF;AAChF,wEAAwE;AACxE,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,2EAA2E;AAC3E,4BAA4B;AAkB5B;;OAEO,GACP,OAAO,IAAI,SAAS,UAAS;AAE7B;;gCAEgC,GAChC,OAAO,IAAI,aAAa,CAAC,UACvB,OAAO,QAAQ,IAAI,CAAC,CAAC,IAAM,CAAC,CAAC,OAAO,IAAI,OAAO,CAAC,OAAO,IAAI,GAAE;AAE/D;wDACwD,GACxD,OAAO,IAAI,SAAS,CAAC,SAAmB,MACtC,QAAQ,GAAG,CAAC,CAAC,IAAM,CAAC;MAAE,GAAG,CAAC;MAAE,CAAC,OAAO,EAAE,OAAO;IAAK,CAAC,GAAE;AAEvD;;iCAEiC,GACjC,OAAO,IAAI,SAAS,CAAC;EACnB,KAAK,IAAI,KAAK,QAAS,IAAI,UAAU,GAAG,OAAO,CAAC,CAAC,OAAO;EACxD,OAAO;AACT,EAAC"}
|
package/write.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// How an effect writes back, and what stops it looping.
|
|
2
|
+
//
|
|
3
|
+
// A handler that only reads is served by the detached transaction it already
|
|
4
|
+
// holds. A handler that writes is a different matter: `tx.patch` puts rows
|
|
5
|
+
// straight into storage, underneath the whole pipeline — no admission, no
|
|
6
|
+
// stamped properties, no journal row, no subscriber notified, and no other
|
|
7
|
+
// effect ever hearing about it. That is how the outcome of a letter became a
|
|
8
|
+
// row a page only found on its next query, instead of an update it was pushed
|
|
9
|
+
// (T-34044). So an effect's write is a new batch — bundles applied in
|
|
10
|
+
// one transaction — through the graph's own `apply()`, after the commit that
|
|
11
|
+
// triggered the handler.
|
|
12
|
+
//
|
|
13
|
+
// That invites a loop, and the loop is stopped here rather than by a rule in
|
|
14
|
+
// every handler. Each batch records how many effect-written generations deep it
|
|
15
|
+
// is, under `$effect`: a `$`-prefixed key, so it belongs to `apply()`'s
|
|
16
|
+
// pipeline and is never a property — the same mechanism `$before` uses to carry
|
|
17
|
+
// a reading from one phase to a later one. The registry triggers nothing for a
|
|
18
|
+
// batch past the depth it allows, so an effect that writes the component it
|
|
19
|
+
// watches runs a bounded number of times and stops, whatever it writes and
|
|
20
|
+
// however it is registered.
|
|
21
|
+
|
|
22
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A write from inside an effect: one new batch through the graph's own
|
|
26
|
+
* `apply()`, post-commit, returning the batch as applied.
|
|
27
|
+
*
|
|
28
|
+
* The application supplies it ({@link Opts.write}), because only it knows which
|
|
29
|
+
* graph and in whose name. It writes as the kernel — the server itself, not the
|
|
30
|
+
* client whose batch triggered the effect — so it must be applied `trusted`: an
|
|
31
|
+
* outcome an effect records (`delivered`, `bounced`) is a server-owned
|
|
32
|
+
* property, and an untrusted apply would refuse the properties the effect
|
|
33
|
+
* exists to write, as in
|
|
34
|
+
* `effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })`.
|
|
35
|
+
*/
|
|
36
|
+
export type Write = (bundles: Bundle[]) => Bundle[] | Promise<Bundle[]>
|
|
37
|
+
|
|
38
|
+
/** The key a batch's generation rides under. Not a component: `$`-prefixed
|
|
39
|
+
* keys are `apply()`'s pipeline, never properties, so no storage adapter sees
|
|
40
|
+
* it. */
|
|
41
|
+
export let ORIGIN = '$effect'
|
|
42
|
+
|
|
43
|
+
/** How deep in effect-written batches this one is: `0` for a batch that came
|
|
44
|
+
* from a client, `1` for an effect's own write, one more for each write an
|
|
45
|
+
* effect makes about that one. */
|
|
46
|
+
export let generation = (bundles: Bundle[]): number =>
|
|
47
|
+
Number(bundles.find((b) => b[ORIGIN] != null)?.[ORIGIN] ?? 0)
|
|
48
|
+
|
|
49
|
+
/** The same batch, marked as an effect's own at generation `gen`. Copies, so
|
|
50
|
+
* the handler's own bundles are never edited under it. */
|
|
51
|
+
export let marked = (bundles: Bundle[], gen: number): Bundle[] =>
|
|
52
|
+
bundles.map((b) => ({ ...b, [ORIGIN]: String(gen) }))
|
|
53
|
+
|
|
54
|
+
/** Take the mark back off, so what a caller gets back is the batch as applied
|
|
55
|
+
* and nothing else. In place, like {@link strip} — the bundles it edits are
|
|
56
|
+
* the copies this package made. */
|
|
57
|
+
export let unmark = (bundles: Bundle[]): Bundle[] => {
|
|
58
|
+
for (let b of bundles) if (ORIGIN in b) delete b[ORIGIN]
|
|
59
|
+
return bundles
|
|
60
|
+
}
|