@yaks/wake 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 +325 -2
- package/_dist/cloudflare.d.ts +68 -0
- package/_dist/cloudflare.d.ts.map +1 -0
- package/_dist/comp.d.ts +59 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/deno.d.ts +40 -0
- package/_dist/deno.d.ts.map +1 -0
- package/_dist/due.d.ts +96 -0
- package/_dist/due.d.ts.map +1 -0
- package/_dist/every.d.ts +65 -0
- package/_dist/every.d.ts.map +1 -0
- package/_dist/graph.d.ts +4 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/held.d.ts +9 -0
- package/_dist/held.d.ts.map +1 -0
- package/_dist/mod.d.ts +59 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/plugin.d.ts +20 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/run.d.ts +24 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/service.d.ts +9 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tick.d.ts +36 -0
- package/_dist/tick.d.ts.map +1 -0
- package/_dist/tools.d.ts +6 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/watch.d.ts +58 -0
- package/_dist/watch.d.ts.map +1 -0
- package/_dist/while.d.ts +43 -0
- package/_dist/while.d.ts.map +1 -0
- package/cloudflare.js +80 -0
- package/cloudflare.js.map +1 -0
- package/cloudflare.ts +113 -0
- package/comp.js +59 -0
- package/comp.js.map +1 -0
- package/comp.ts +139 -0
- package/deno.js +57 -0
- package/deno.js.map +1 -0
- package/deno.json +1 -0
- package/deno.ts +78 -0
- package/due.js +147 -0
- package/due.js.map +1 -0
- package/due.ts +180 -0
- package/every.js +203 -0
- package/every.js.map +1 -0
- package/every.ts +223 -0
- package/graph.js +10 -0
- package/graph.js.map +1 -0
- package/graph.ts +11 -0
- package/held.js +29 -0
- package/held.js.map +1 -0
- package/held.ts +42 -0
- package/mod.js +59 -0
- package/mod.js.map +1 -0
- package/mod.ts +60 -0
- package/package.json +49 -4
- package/plugin.js +87 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +94 -0
- package/run.js +124 -0
- package/run.js.map +1 -0
- package/run.ts +117 -0
- package/service.js +28 -0
- package/service.js.map +1 -0
- package/service.ts +42 -0
- package/tick.js +168 -0
- package/tick.js.map +1 -0
- package/tick.ts +181 -0
- package/tools.js +83 -0
- package/tools.js.map +1 -0
- package/tools.ts +76 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +270 -0
- package/vocab.ts +16 -0
- package/watch.js +175 -0
- package/watch.js.map +1 -0
- package/watch.ts +193 -0
- package/while.js +159 -0
- package/while.js.map +1 -0
- package/while.ts +176 -0
package/while.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// A wake that repeats only while something holds. `wake.while` is an ordered
|
|
2
|
+
// list of `{match, every}`: a query over the whole graph, and the cadence it
|
|
3
|
+
// asks for while that query finds anything. The first that finds anything
|
|
4
|
+
// sets the cadence; when none does, the wake's own `every` does, and a wake
|
|
5
|
+
// without one sleeps — `at` cleared, nothing owed, no timer anywhere.
|
|
6
|
+
//
|
|
7
|
+
// Two moments decide it, and each is a function over a graph:
|
|
8
|
+
//
|
|
9
|
+
// a firing `tick` asks `cadence` which one the next instant is counted
|
|
10
|
+
// in, so a world somebody is in keeps coming back, and one
|
|
11
|
+
// nobody is in fires its last and sleeps
|
|
12
|
+
// a write `rouse` arms a wake whose condition a write made hold, when
|
|
13
|
+
// that brings it sooner than the instant it holds — a player
|
|
14
|
+
// arriving is a write, so arriving is what wakes the world
|
|
15
|
+
//
|
|
16
|
+
// Neither reads a clock it was not given. The program holding the graph calls
|
|
17
|
+
// `rouse` once its writes commit, the way it calls `tick` when its own clock
|
|
18
|
+
// goes off.
|
|
19
|
+
import { after, over } from "@yaks/fp";
|
|
20
|
+
import { Stale } from "@yaks/graph/errors";
|
|
21
|
+
import { token } from "@yaks/graph/sha256";
|
|
22
|
+
import { and, limit, line, parse } from "@yaks/query";
|
|
23
|
+
import { WAKE } from "./comp.js";
|
|
24
|
+
import { wakeOf } from "./due.js";
|
|
25
|
+
import { recur } from "./every.js";
|
|
26
|
+
let iso = (t)=>new Date(t).toISOString();
|
|
27
|
+
// A condition asks only whether anything matches, so it is read one row deep.
|
|
28
|
+
let probe = (match)=>{
|
|
29
|
+
let q = parse(match);
|
|
30
|
+
return and(...q.clauses.filter((c)=>c.kind != 'limit'), limit(1));
|
|
31
|
+
};
|
|
32
|
+
// A scheduler pass asks each distinct condition once before it writes a wake.
|
|
33
|
+
// Failed reads stay uncached so each affected wake can report its own refusal.
|
|
34
|
+
export let matches = (graph, now)=>{
|
|
35
|
+
let found = new Map();
|
|
36
|
+
return async (match)=>{
|
|
37
|
+
let yes = found.get(match);
|
|
38
|
+
if (yes != null) return yes;
|
|
39
|
+
yes = (await graph.read(probe(match), {
|
|
40
|
+
now
|
|
41
|
+
})).length > 0;
|
|
42
|
+
found.set(match, yes);
|
|
43
|
+
return yes;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The cadence a wake's conditions ask for at `now`: the `every` of the first
|
|
48
|
+
* `while` entry whose query finds anything, or `null` when none does.
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { cadence } from '@yaks/wake'
|
|
52
|
+
*
|
|
53
|
+
* // await cadence(graph, { while: [{ match: '.player', every: '5m' }] }, now)
|
|
54
|
+
* ```
|
|
55
|
+
*/ export let cadence = async (graph, wake, now, holds = matches(graph, now))=>{
|
|
56
|
+
for (let { match, every } of wake.while ?? []){
|
|
57
|
+
if (await holds(match)) return every;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Arm each wake whose conditions now hold at the first instant its cadence
|
|
63
|
+
* owes, when that is sooner than the instant it holds: a sleeping wake is
|
|
64
|
+
* woken, and one on a slow cadence is brought forward when a faster
|
|
65
|
+
* condition starts to hold. A wake whose `at` moved while this ran keeps the
|
|
66
|
+
* other writer's instant.
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { rouse } from '@yaks/wake'
|
|
70
|
+
*
|
|
71
|
+
* // after a write commits: await rouse(graph, Date.now())
|
|
72
|
+
* ```
|
|
73
|
+
*/ export let rouse = async (graph, now = Date.now(), catalog)=>{
|
|
74
|
+
let result = {
|
|
75
|
+
roused: [],
|
|
76
|
+
refused: []
|
|
77
|
+
};
|
|
78
|
+
let holds = matches(graph, now);
|
|
79
|
+
let ready = [];
|
|
80
|
+
for (let wake of catalog ?? await graph.read(line`.${WAKE}.while`, {
|
|
81
|
+
now
|
|
82
|
+
})){
|
|
83
|
+
let w = wakeOf(wake) ?? {};
|
|
84
|
+
try {
|
|
85
|
+
let every = await cadence(graph, w, now, holds);
|
|
86
|
+
let at = every == null ? null : recur(every, now, now);
|
|
87
|
+
if (at == null || w.at && Date.parse(w.at) <= at) continue;
|
|
88
|
+
ready.push({
|
|
89
|
+
wake,
|
|
90
|
+
at
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
ready.push({
|
|
94
|
+
wake,
|
|
95
|
+
error
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
for (let choice of ready){
|
|
100
|
+
let { wake } = choice;
|
|
101
|
+
if ('error' in choice) {
|
|
102
|
+
if (!(choice.error instanceof Stale)) {
|
|
103
|
+
result.refused.push({
|
|
104
|
+
wake,
|
|
105
|
+
error: choice.error
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
let w = wakeOf(wake) ?? {};
|
|
111
|
+
try {
|
|
112
|
+
let applied = await graph.apply([
|
|
113
|
+
{
|
|
114
|
+
entity: wake.entity,
|
|
115
|
+
[WAKE]: {
|
|
116
|
+
at: iso(choice.at)
|
|
117
|
+
},
|
|
118
|
+
$was: {
|
|
119
|
+
[WAKE]: {
|
|
120
|
+
at: token(w.at)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
], {
|
|
125
|
+
now: iso(now)
|
|
126
|
+
});
|
|
127
|
+
result.roused.push(...applied.filter((b)=>b.entity.eid == wake.entity.eid));
|
|
128
|
+
} catch (error) {
|
|
129
|
+
// Another writer moved `at` first; its instant stands.
|
|
130
|
+
if (!(error instanceof Stale)) result.refused.push({
|
|
131
|
+
wake,
|
|
132
|
+
error
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
// One `while` entry, refused unless it is a query this graph can answer and a
|
|
139
|
+
// cadence `every` can read.
|
|
140
|
+
let entry = (tx, e, i)=>{
|
|
141
|
+
let { match, every } = e ?? {};
|
|
142
|
+
if (typeof match != 'string' || typeof every != 'string') {
|
|
143
|
+
throw new TypeError(`wake.while[${i}] is {match, every}: a query and a cadence`);
|
|
144
|
+
}
|
|
145
|
+
if (recur(every, 0, 0) == null) {
|
|
146
|
+
throw new TypeError(`wake.while[${i}].every: no cadence in "${every}"`);
|
|
147
|
+
}
|
|
148
|
+
return tx.read(probe(match));
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The `precondition` hook: each `while` a write gives is read once, so a
|
|
152
|
+
* condition that does not parse, or names a word this graph does not speak,
|
|
153
|
+
* is refused with the reader's own reason when it is written, not at every
|
|
154
|
+
* firing after.
|
|
155
|
+
*/ export let conditions = (bundles, tx)=>after(over(bundles.flatMap((b)=>(wakeOf(b)?.while ?? []).map((e, i)=>[
|
|
156
|
+
e,
|
|
157
|
+
i
|
|
158
|
+
])), ([e, i])=>entry(tx, e, i)), ()=>bundles);
|
|
159
|
+
//# sourceMappingURL=while.js.map
|
package/while.js.map
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MAAM;YAAE,CAAC,KAAK,EAAE;cAAE,IAAI,MAAM,EAAE,EAAE;YAAE;UAAE;QACtC;OAAE,EAAE;QAAE,KAAK,IAAI;MAAK;MACpB,OAAO,MAAM,CAAC,IAAI,IACb,QAAQ,MAAM,CAAC,CAAC,IAAM,EAAE,MAAM,CAAC,GAAG,IAAI,KAAK,MAAM,CAAC,GAAG;IAE5D,EAAE,OAAO,OAAO;MACd,uDAAuD;MACvD,IAAI,CAAC,CAAC,iBAAiB,KAAK,GAAG,OAAO,OAAO,CAAC,IAAI,CAAC;QAAE;QAAM;MAAM;IACnE;EACF;EACA,OAAO;AACT,EAAC;AAED,8EAA8E;AAC9E,4BAA4B;AAC5B,IAAI,QAAQ,CAAC,IAAY,GAAY;EACnC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,GAAI,KAAK,CAAC;EAC9B,IAAI,OAAO,SAAS,YAAY,OAAO,SAAS,UAAU;IACxD,MAAM,IAAI,UACR,CAAC,WAAW,EAAE,EAAE,0CAA0C,CAAC;EAE/D;EACA,IAAI,MAAM,OAAO,GAAG,MAAM,MAAM;IAC9B,MAAM,IAAI,UAAU,CAAC,WAAW,EAAE,EAAE,wBAAwB,EAAE,MAAM,CAAC,CAAC;EACxE;EACA,OAAO,GAAG,IAAI,CAAC,MAAM;AACvB;AAEA;;;;;CAKC,GACD,OAAO,IAAI,aAAmB,CAAC,SAAS,KACtC,MACE,KACE,QAAQ,OAAO,CAAC,CAAC,IACf,CAAC,OAAO,IAAI,SAAS,EAAE,EAAE,GAAG,CAAC,CAAC,GAAG,IAAM;QAAC;QAAG;OAAE,IAE/C,CAAC,CAAC,GAAG,EAAE,GAAK,MAAM,IAAI,GAAG,KAE3B,IAAM,SACP"}
|
package/while.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// A wake that repeats only while something holds. `wake.while` is an ordered
|
|
2
|
+
// list of `{match, every}`: a query over the whole graph, and the cadence it
|
|
3
|
+
// asks for while that query finds anything. The first that finds anything
|
|
4
|
+
// sets the cadence; when none does, the wake's own `every` does, and a wake
|
|
5
|
+
// without one sleeps — `at` cleared, nothing owed, no timer anywhere.
|
|
6
|
+
//
|
|
7
|
+
// Two moments decide it, and each is a function over a graph:
|
|
8
|
+
//
|
|
9
|
+
// a firing `tick` asks `cadence` which one the next instant is counted
|
|
10
|
+
// in, so a world somebody is in keeps coming back, and one
|
|
11
|
+
// nobody is in fires its last and sleeps
|
|
12
|
+
// a write `rouse` arms a wake whose condition a write made hold, when
|
|
13
|
+
// that brings it sooner than the instant it holds — a player
|
|
14
|
+
// arriving is a write, so arriving is what wakes the world
|
|
15
|
+
//
|
|
16
|
+
// Neither reads a clock it was not given. The program holding the graph calls
|
|
17
|
+
// `rouse` once its writes commit, the way it calls `tick` when its own clock
|
|
18
|
+
// goes off.
|
|
19
|
+
|
|
20
|
+
import { after, over } from '@yaks/fp'
|
|
21
|
+
import type { Bundle, Hook, ReadTx } from 'jsr:@yaks/graph@0.2.36'
|
|
22
|
+
import { Stale } from '@yaks/graph/errors'
|
|
23
|
+
import { token } from '@yaks/graph/sha256'
|
|
24
|
+
import { and, limit, line, parse, type Query } from '@yaks/query'
|
|
25
|
+
import { WAKE, type Wake } from './comp.js'
|
|
26
|
+
import { wakeOf } from './due.js'
|
|
27
|
+
import { recur } from './every.js'
|
|
28
|
+
import type { Driver } from './tick.ts'
|
|
29
|
+
|
|
30
|
+
let iso = (t: number): string => new Date(t).toISOString()
|
|
31
|
+
type Holds = (match: string) => Promise<boolean>
|
|
32
|
+
|
|
33
|
+
// A condition asks only whether anything matches, so it is read one row deep.
|
|
34
|
+
let probe = (match: string): Query => {
|
|
35
|
+
let q = parse(match)
|
|
36
|
+
return and(...q.clauses.filter((c) => c.kind != 'limit'), limit(1))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// A scheduler pass asks each distinct condition once before it writes a wake.
|
|
40
|
+
// Failed reads stay uncached so each affected wake can report its own refusal.
|
|
41
|
+
export let matches = (graph: Pick<Driver, 'read'>, now: number): Holds => {
|
|
42
|
+
let found = new Map<string, boolean>()
|
|
43
|
+
return async (match: string): Promise<boolean> => {
|
|
44
|
+
let yes = found.get(match)
|
|
45
|
+
if (yes != null) return yes
|
|
46
|
+
yes = (await graph.read(probe(match), { now })).length > 0
|
|
47
|
+
found.set(match, yes)
|
|
48
|
+
return yes
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The cadence a wake's conditions ask for at `now`: the `every` of the first
|
|
54
|
+
* `while` entry whose query finds anything, or `null` when none does.
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { cadence } from '@yaks/wake'
|
|
58
|
+
*
|
|
59
|
+
* // await cadence(graph, { while: [{ match: '.player', every: '5m' }] }, now)
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export let cadence = async (
|
|
63
|
+
graph: Pick<Driver, 'read'>,
|
|
64
|
+
wake: Wake,
|
|
65
|
+
now: number,
|
|
66
|
+
holds: Holds = matches(graph, now),
|
|
67
|
+
): Promise<string | null> => {
|
|
68
|
+
for (let { match, every } of wake.while ?? []) {
|
|
69
|
+
if (await holds(match)) return every
|
|
70
|
+
}
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What a pass of {@link rouse} did: the wakes it armed, and the ones it
|
|
75
|
+
* could not, each with why. */
|
|
76
|
+
export type Roused = {
|
|
77
|
+
/** the applied wake bundles */
|
|
78
|
+
roused: Bundle[]
|
|
79
|
+
/** the wakes whose conditions could not be read or whose write was refused */
|
|
80
|
+
refused: { wake: Bundle; error: unknown }[]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Arm each wake whose conditions now hold at the first instant its cadence
|
|
85
|
+
* owes, when that is sooner than the instant it holds: a sleeping wake is
|
|
86
|
+
* woken, and one on a slow cadence is brought forward when a faster
|
|
87
|
+
* condition starts to hold. A wake whose `at` moved while this ran keeps the
|
|
88
|
+
* other writer's instant.
|
|
89
|
+
*
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { rouse } from '@yaks/wake'
|
|
92
|
+
*
|
|
93
|
+
* // after a write commits: await rouse(graph, Date.now())
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export let rouse = async (
|
|
97
|
+
graph: Driver,
|
|
98
|
+
now: number = Date.now(),
|
|
99
|
+
catalog?: Bundle[],
|
|
100
|
+
): Promise<Roused> => {
|
|
101
|
+
let result: Roused = { roused: [], refused: [] }
|
|
102
|
+
let holds = matches(graph, now)
|
|
103
|
+
let ready: (
|
|
104
|
+
| { wake: Bundle; at: number }
|
|
105
|
+
| { wake: Bundle; error: unknown }
|
|
106
|
+
)[] = []
|
|
107
|
+
for (
|
|
108
|
+
let wake of catalog ?? await graph.read(line`.${WAKE}.while`, { now })
|
|
109
|
+
) {
|
|
110
|
+
let w = wakeOf(wake) ?? {}
|
|
111
|
+
try {
|
|
112
|
+
let every = await cadence(graph, w, now, holds)
|
|
113
|
+
let at = every == null ? null : recur(every, now, now)
|
|
114
|
+
if (at == null || (w.at && Date.parse(w.at) <= at)) continue
|
|
115
|
+
ready.push({ wake, at })
|
|
116
|
+
} catch (error) {
|
|
117
|
+
ready.push({ wake, error })
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (let choice of ready) {
|
|
121
|
+
let { wake } = choice
|
|
122
|
+
if ('error' in choice) {
|
|
123
|
+
if (!(choice.error instanceof Stale)) {
|
|
124
|
+
result.refused.push({ wake, error: choice.error })
|
|
125
|
+
}
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
let w = wakeOf(wake) ?? {}
|
|
129
|
+
try {
|
|
130
|
+
let applied = await graph.apply([{
|
|
131
|
+
entity: wake.entity,
|
|
132
|
+
[WAKE]: { at: iso(choice.at) },
|
|
133
|
+
$was: { [WAKE]: { at: token(w.at) } },
|
|
134
|
+
}], { now: iso(now) })
|
|
135
|
+
result.roused.push(
|
|
136
|
+
...applied.filter((b) => b.entity.eid == wake.entity.eid),
|
|
137
|
+
)
|
|
138
|
+
} catch (error) {
|
|
139
|
+
// Another writer moved `at` first; its instant stands.
|
|
140
|
+
if (!(error instanceof Stale)) result.refused.push({ wake, error })
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return result
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// One `while` entry, refused unless it is a query this graph can answer and a
|
|
147
|
+
// cadence `every` can read.
|
|
148
|
+
let entry = (tx: ReadTx, e: unknown, i: number): unknown => {
|
|
149
|
+
let { match, every } = (e ?? {}) as { match?: unknown; every?: unknown }
|
|
150
|
+
if (typeof match != 'string' || typeof every != 'string') {
|
|
151
|
+
throw new TypeError(
|
|
152
|
+
`wake.while[${i}] is {match, every}: a query and a cadence`,
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
if (recur(every, 0, 0) == null) {
|
|
156
|
+
throw new TypeError(`wake.while[${i}].every: no cadence in "${every}"`)
|
|
157
|
+
}
|
|
158
|
+
return tx.read(probe(match))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The `precondition` hook: each `while` a write gives is read once, so a
|
|
163
|
+
* condition that does not parse, or names a word this graph does not speak,
|
|
164
|
+
* is refused with the reader's own reason when it is written, not at every
|
|
165
|
+
* firing after.
|
|
166
|
+
*/
|
|
167
|
+
export let conditions: Hook = (bundles, tx) =>
|
|
168
|
+
after(
|
|
169
|
+
over(
|
|
170
|
+
bundles.flatMap((b) =>
|
|
171
|
+
(wakeOf(b)?.while ?? []).map((e, i) => [e, i] as const)
|
|
172
|
+
),
|
|
173
|
+
([e, i]) => entry(tx, e, i),
|
|
174
|
+
),
|
|
175
|
+
() => bundles,
|
|
176
|
+
)
|