@orthograph/language-effect 0.0.0-stage → 0.1.0-next.2
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 +2 -2
- package/dist/device.d.ts +5 -0
- package/dist/device.js +6 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +25 -0
- package/dist/modules/access/internal/path.d.ts +3 -0
- package/dist/modules/access/internal/path.js +11 -0
- package/dist/modules/access/service.d.ts +5 -0
- package/dist/modules/access/service.js +111 -0
- package/dist/modules/access/types.d.ts +38 -0
- package/dist/modules/access/types.js +3 -0
- package/dist/modules/auth/service.d.ts +4 -0
- package/dist/modules/auth/service.js +19 -0
- package/dist/modules/auth/types.d.ts +38 -0
- package/dist/modules/auth/types.js +8 -0
- package/dist/modules/calls/internal/run.d.ts +2 -0
- package/dist/modules/calls/internal/run.js +28 -0
- package/dist/modules/calls/service.d.ts +2 -0
- package/dist/modules/calls/service.js +2 -0
- package/dist/modules/calls/types.d.ts +32 -0
- package/dist/modules/calls/types.js +1 -0
- package/dist/modules/handler/internal/attributes.d.ts +17 -0
- package/dist/modules/handler/internal/attributes.js +46 -0
- package/dist/modules/handler/internal/call-id.d.ts +2 -0
- package/dist/modules/handler/internal/call-id.js +5 -0
- package/dist/modules/handler/internal/call-metrics.d.ts +17 -0
- package/dist/modules/handler/internal/call-metrics.js +35 -0
- package/dist/modules/handler/internal/decided.d.ts +14 -0
- package/dist/modules/handler/internal/decided.js +106 -0
- package/dist/modules/handler/internal/defect.d.ts +4 -0
- package/dist/modules/handler/internal/defect.js +19 -0
- package/dist/modules/handler/internal/derived.d.ts +21 -0
- package/dist/modules/handler/internal/derived.js +96 -0
- package/dist/modules/handler/internal/device.d.ts +3 -0
- package/dist/modules/handler/internal/device.js +46 -0
- package/dist/modules/handler/internal/digest.d.ts +3 -0
- package/dist/modules/handler/internal/digest.js +52 -0
- package/dist/modules/handler/internal/environment.d.ts +3 -0
- package/dist/modules/handler/internal/environment.js +3 -0
- package/dist/modules/handler/internal/invariants.d.ts +12 -0
- package/dist/modules/handler/internal/invariants.js +28 -0
- package/dist/modules/handler/internal/move.d.ts +32 -0
- package/dist/modules/handler/internal/move.js +84 -0
- package/dist/modules/handler/internal/prehandle.d.ts +23 -0
- package/dist/modules/handler/internal/prehandle.js +34 -0
- package/dist/modules/handler/internal/read.d.ts +3 -0
- package/dist/modules/handler/internal/read.js +79 -0
- package/dist/modules/handler/internal/signal.d.ts +3 -0
- package/dist/modules/handler/internal/signal.js +10 -0
- package/dist/modules/handler/internal/span.d.ts +8 -0
- package/dist/modules/handler/internal/span.js +14 -0
- package/dist/modules/handler/service.d.ts +7 -0
- package/dist/modules/handler/service.js +163 -0
- package/dist/modules/handler/types.d.ts +217 -0
- package/dist/modules/handler/types.js +24 -0
- package/dist/modules/ids/service.d.ts +2 -0
- package/dist/modules/ids/service.js +11 -0
- package/dist/modules/ids/types.d.ts +8 -0
- package/dist/modules/ids/types.js +1 -0
- package/dist/modules/journal/service.d.ts +6 -0
- package/dist/modules/journal/service.js +4 -0
- package/dist/modules/journal/types.d.ts +22 -0
- package/dist/modules/journal/types.js +5 -0
- package/dist/modules/mcp/internal/input.d.ts +15 -0
- package/dist/modules/mcp/internal/input.js +35 -0
- package/dist/modules/mcp/internal/output.d.ts +16 -0
- package/dist/modules/mcp/internal/output.js +50 -0
- package/dist/modules/mcp/internal/tool.d.ts +4 -0
- package/dist/modules/mcp/internal/tool.js +56 -0
- package/dist/modules/mcp/service.d.ts +2 -0
- package/dist/modules/mcp/service.js +38 -0
- package/dist/modules/mcp/types.d.ts +101 -0
- package/dist/modules/mcp/types.js +1 -0
- package/dist/modules/mount/internal/none.d.ts +2 -0
- package/dist/modules/mount/internal/none.js +1 -0
- package/dist/modules/mount/service.d.ts +2 -0
- package/dist/modules/mount/service.js +12 -0
- package/dist/modules/mount/types.d.ts +11 -0
- package/dist/modules/mount/types.js +4 -0
- package/dist/modules/principals/service.d.ts +4 -0
- package/dist/modules/principals/service.js +4 -0
- package/dist/modules/principals/types.d.ts +15 -0
- package/dist/modules/principals/types.js +4 -0
- package/dist/modules/records/internal/holder.d.ts +16 -0
- package/dist/modules/records/internal/holder.js +55 -0
- package/dist/modules/records/internal/list.d.ts +12 -0
- package/dist/modules/records/internal/list.js +65 -0
- package/dist/modules/records/internal/order.d.ts +18 -0
- package/dist/modules/records/internal/order.js +41 -0
- package/dist/modules/records/internal/unique.d.ts +31 -0
- package/dist/modules/records/internal/unique.js +49 -0
- package/dist/modules/records/internal/unit.d.ts +3 -0
- package/dist/modules/records/internal/unit.js +135 -0
- package/dist/modules/records/service.d.ts +22 -0
- package/dist/modules/records/service.js +102 -0
- package/dist/modules/records/types.d.ts +170 -0
- package/dist/modules/records/types.js +9 -0
- package/dist/modules/timers/internal/world.d.ts +4 -0
- package/dist/modules/timers/internal/world.js +64 -0
- package/dist/modules/timers/service.d.ts +18 -0
- package/dist/modules/timers/service.js +125 -0
- package/dist/modules/timers/types.d.ts +102 -0
- package/dist/modules/timers/types.js +40 -0
- package/dist/modules/versions/internal/handlers.d.ts +6 -0
- package/dist/modules/versions/internal/handlers.js +36 -0
- package/dist/modules/versions/internal/keep.d.ts +21 -0
- package/dist/modules/versions/internal/keep.js +36 -0
- package/dist/modules/versions/service.d.ts +2 -0
- package/dist/modules/versions/service.js +14 -0
- package/dist/modules/versions/types.d.ts +27 -0
- package/dist/modules/versions/types.js +1 -0
- package/dist/modules/visibility/internal/rule.d.ts +7 -0
- package/dist/modules/visibility/internal/rule.js +23 -0
- package/dist/modules/visibility/internal/schema.d.ts +5 -0
- package/dist/modules/visibility/internal/schema.js +12 -0
- package/dist/modules/visibility/service.d.ts +15 -0
- package/dist/modules/visibility/service.js +27 -0
- package/dist/modules/visibility/types.d.ts +60 -0
- package/dist/modules/visibility/types.js +1 -0
- package/dist/modules/wire/internal/call-telemetry.d.ts +15 -0
- package/dist/modules/wire/internal/call-telemetry.js +31 -0
- package/dist/modules/wire/internal/refused-payload.d.ts +7 -0
- package/dist/modules/wire/internal/refused-payload.js +25 -0
- package/dist/modules/wire/internal/shown.d.ts +9 -0
- package/dist/modules/wire/internal/shown.js +32 -0
- package/dist/modules/wire/service.d.ts +3 -0
- package/dist/modules/wire/service.js +14 -0
- package/dist/modules/wire/types.d.ts +30 -0
- package/dist/modules/wire/types.js +1 -0
- package/dist/modules/work/internal/fake-world.d.ts +17 -0
- package/dist/modules/work/internal/fake-world.js +43 -0
- package/dist/modules/work/internal/fibers.d.ts +2 -0
- package/dist/modules/work/internal/fibers.js +5 -0
- package/dist/modules/work/service.d.ts +6 -0
- package/dist/modules/work/service.js +249 -0
- package/dist/modules/work/types.d.ts +103 -0
- package/dist/modules/work/types.js +19 -0
- package/dist/visibility.d.ts +1 -0
- package/dist/visibility.js +3 -0
- package/package.json +39 -3
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import type { Thing } from "orthograph";
|
|
3
|
+
import type { Entry } from "#modules/journal/types.js";
|
|
4
|
+
import { Journal } from "#modules/journal/types.js";
|
|
5
|
+
import { Records, Store, type Instance, type NamedRule, type UnitApi } from "./types.ts";
|
|
6
|
+
export declare const Unit: UnitApi;
|
|
7
|
+
/** A store and journal held in memory, for tests: the same versions and the same all-or-nothing commit as a host's. */
|
|
8
|
+
export declare const storeMemory: (initial?: ReadonlyArray<readonly [Thing, Instance]>, given?: ReadonlyArray<NamedRule>) => {
|
|
9
|
+
entries: Entry[];
|
|
10
|
+
rows: {
|
|
11
|
+
thing: Thing<import("orthograph").FieldsInput>;
|
|
12
|
+
instance: Instance;
|
|
13
|
+
version: number;
|
|
14
|
+
}[];
|
|
15
|
+
layer: Layer.Layer<Journal | Store, never, never>;
|
|
16
|
+
};
|
|
17
|
+
/** `Records` over an in-memory store, outside any call: for tests of roles and reads. */
|
|
18
|
+
export declare const recordsMemory: (initial: ReadonlyArray<readonly [Thing, Instance]>) => Layer.Layer<Records, never, never>;
|
|
19
|
+
/** The rules a `invariants.ts` module exports, each named `<thing>.<export>` (the thing's name, as the model has it): what a host's store and the unit enforce. */
|
|
20
|
+
export declare const invariantsIn: (thing: string, module: Readonly<Record<string, unknown>>) => ReadonlyArray<NamedRule>;
|
|
21
|
+
/** The one thing a `thing.ts` module exports, whatever its export is called. */
|
|
22
|
+
export declare const thingIn: (module: Readonly<Record<string, unknown>>) => Thing;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { Journal } from "#modules/journal/types.js";
|
|
3
|
+
import { journalOf } from "#modules/journal/service.js";
|
|
4
|
+
import { Records, Store } from "./types.js";
|
|
5
|
+
import { makeUnit } from "./internal/unit.js";
|
|
6
|
+
import { rulesOf, uniqueIndex } from "./internal/unique.js";
|
|
7
|
+
import { matches } from "./internal/list.js";
|
|
8
|
+
export const Unit = { make: makeUnit };
|
|
9
|
+
/** A store and journal held in memory, for tests: the same versions and the same all-or-nothing commit as a host's. */
|
|
10
|
+
export const storeMemory = (initial = [], given = []) => {
|
|
11
|
+
const rules = rulesOf(given);
|
|
12
|
+
const rows = initial.map(([thing, instance]) => ({ thing, instance, version: 0 }));
|
|
13
|
+
const entries = [];
|
|
14
|
+
const requests = new Map();
|
|
15
|
+
const at = (thing, id) => rows.findIndex((r) => r.thing === thing && r.instance["id"] === id);
|
|
16
|
+
const store = Store.of({
|
|
17
|
+
rules: given,
|
|
18
|
+
find: (thing, id) => Effect.sync(() => {
|
|
19
|
+
const r = rows[at(thing, id)];
|
|
20
|
+
return r && { instance: r.instance, version: r.version };
|
|
21
|
+
}),
|
|
22
|
+
list: ({ thing, where, state, order, limit, after }) => Effect.sync(() => {
|
|
23
|
+
// A row's place in `rows` is the order it was made in, and its cursor.
|
|
24
|
+
const ordered = rows.flatMap((r, i) => (r.thing === thing ? [{ r, i }] : []));
|
|
25
|
+
const matching = (order === "newest" ? ordered.reverse() : ordered).filter(({ r, i }) => matches(thing, where, state, r.instance) && (after === undefined || (order === "newest" ? i < Number(after) : i > Number(after))));
|
|
26
|
+
const page = matching.slice(0, limit);
|
|
27
|
+
return {
|
|
28
|
+
items: page.map(({ r }) => r.instance),
|
|
29
|
+
rows: page.map(({ r, i }) => ({ version: r.version, place: i })),
|
|
30
|
+
next: matching.length > limit ? String(page[page.length - 1].i) : null,
|
|
31
|
+
};
|
|
32
|
+
}),
|
|
33
|
+
place: (thing, id) => Effect.sync(() => (at(thing, id) === -1 ? undefined : at(thing, id))),
|
|
34
|
+
answered: (key) => Effect.sync(() => requests.get(key)),
|
|
35
|
+
commit: ({ writes, entries: events, request, lists = [], reads = [] }) => Effect.sync(() => {
|
|
36
|
+
if (request && requests.has(request.key))
|
|
37
|
+
return "conflict";
|
|
38
|
+
// A commit that changes nothing commits, guarding nothing, as D1's does.
|
|
39
|
+
if (writes.length === 0 && events.length === 0 && request === undefined)
|
|
40
|
+
return "committed";
|
|
41
|
+
// #764: a list's rows count as read. Before this commit's own writes, the rows matching each list must be the ones it read, at the same versions.
|
|
42
|
+
const moved = lists.some(({ thing, where, state, rows: read }) => {
|
|
43
|
+
const now = rows.filter((r) => r.thing === thing && matches(thing, where, state, r.instance));
|
|
44
|
+
const was = new Map(read.map((r) => [r.id, r.version]));
|
|
45
|
+
return now.length !== was.size || now.some((r) => was.get(String(r.instance["id"])) !== r.version);
|
|
46
|
+
});
|
|
47
|
+
if (moved)
|
|
48
|
+
return "conflict";
|
|
49
|
+
// #1045: so does each row it found, at the version it found it.
|
|
50
|
+
if (reads.some(({ thing, id, version }) => rows[at(thing, id)]?.version !== version))
|
|
51
|
+
return "conflict";
|
|
52
|
+
// One write at a time, checked after each, as D1 checks its index statement by statement: on a copy, so a refusal writes none of it.
|
|
53
|
+
const next = rows.map((r) => ({ ...r }));
|
|
54
|
+
const index = (thing, id) => next.findIndex((r) => r.thing === thing && r.instance["id"] === id);
|
|
55
|
+
const keys = new Map();
|
|
56
|
+
for (const w of writes) {
|
|
57
|
+
if (!keys.has(w.thing))
|
|
58
|
+
keys.set(w.thing, uniqueIndex(rules.of(w.thing), next.filter((r) => r.thing === w.thing).map((r) => r.instance)));
|
|
59
|
+
if (w.kind === "insert") {
|
|
60
|
+
const instance = { ...w.fields, ...(w.state ? { state: w.state } : {}) };
|
|
61
|
+
next.push({ thing: w.thing, instance, version: 0 });
|
|
62
|
+
if (keys.get(w.thing).move(undefined, instance))
|
|
63
|
+
return "conflict";
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const r = next[index(w.thing, w.id)];
|
|
67
|
+
if (r === undefined || r.version !== w.version)
|
|
68
|
+
return "conflict";
|
|
69
|
+
const from = r.instance;
|
|
70
|
+
r.instance = { ...from, ...w.changes, ...(w.state ? { state: w.state } : {}) };
|
|
71
|
+
r.version += 1;
|
|
72
|
+
if (keys.get(w.thing).move(from, r.instance))
|
|
73
|
+
return "conflict";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Nothing is spread into arguments (a big store would overflow the stack), and the answer is kept last: a commit that throws leaves nothing behind.
|
|
77
|
+
const journalled = Array.from(events);
|
|
78
|
+
rows.length = 0;
|
|
79
|
+
for (const r of next)
|
|
80
|
+
rows.push(r);
|
|
81
|
+
for (const e of journalled)
|
|
82
|
+
entries.push(e);
|
|
83
|
+
if (request)
|
|
84
|
+
requests.set(request.key, request.answer);
|
|
85
|
+
return "committed";
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
const journal = journalOf(entries);
|
|
89
|
+
return { entries, rows, layer: Layer.mergeAll(Layer.succeed(Store, store), Layer.succeed(Journal, journal)) };
|
|
90
|
+
};
|
|
91
|
+
/** `Records` over an in-memory store, outside any call: for tests of roles and reads. */
|
|
92
|
+
export const recordsMemory = (initial) => Layer.effect(Records, Effect.gen(function* () {
|
|
93
|
+
const store = yield* Store;
|
|
94
|
+
return (yield* makeUnit(store)).records;
|
|
95
|
+
})).pipe(Layer.provide(storeMemory(initial).layer));
|
|
96
|
+
/** The rules a `invariants.ts` module exports, each named `<thing>.<export>` (the thing's name, as the model has it): what a host's store and the unit enforce. */
|
|
97
|
+
export const invariantsIn = (thing, module) => Object.entries(module).flatMap(([name, rule]) => typeof rule === "object" && rule !== null && rule.kind === "invariant" ? [{ name: `${thing}.${name}`, rule: rule }] : []);
|
|
98
|
+
/** The one thing a `thing.ts` module exports, whatever its export is called. */
|
|
99
|
+
export const thingIn = (module) => {
|
|
100
|
+
const found = Object.values(module).find((x) => typeof x === "object" && x !== null && x.kind === "thing");
|
|
101
|
+
return found;
|
|
102
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { Context, type Effect } from "effect";
|
|
2
|
+
import type { AnyErrorClass, InstanceOf, Thing, Unique } from "orthograph";
|
|
3
|
+
import type { Entry } from "#modules/journal/types.js";
|
|
4
|
+
/** A thing's `Blueprint.optional` fields: those whose schema's key is optional. */
|
|
5
|
+
type OptionalKeys<T extends Thing> = {
|
|
6
|
+
[K in keyof T["fields"]]: T["fields"][K]["value"]["schema"]["~type.optionality"] extends "optional" ? K : never;
|
|
7
|
+
}[keyof T["fields"]];
|
|
8
|
+
/**
|
|
9
|
+
* A thing's fields, decoded: what a handler stores. A `Blueprint.optional` field may be left out, never written as `undefined`
|
|
10
|
+
* (exact optional properties); every other field is required (snag #1044).
|
|
11
|
+
*/
|
|
12
|
+
export type FieldsOf<T extends Thing> = {
|
|
13
|
+
readonly [K in keyof InstanceOf<T> as K extends "state" | OptionalKeys<T> ? never : K]: InstanceOf<T>[K];
|
|
14
|
+
} & {
|
|
15
|
+
readonly [K in keyof InstanceOf<T> as K extends Exclude<OptionalKeys<T>, "state"> ? K : never]?: InstanceOf<T>[K];
|
|
16
|
+
};
|
|
17
|
+
/** One stored instance: its fields, decoded, and its machine state when it has one. */
|
|
18
|
+
export type Instance = {
|
|
19
|
+
readonly [field: string]: unknown;
|
|
20
|
+
readonly state?: string;
|
|
21
|
+
};
|
|
22
|
+
/** An instance as the store holds it: with the version every update is guarded by. */
|
|
23
|
+
export interface Stored {
|
|
24
|
+
readonly instance: Instance;
|
|
25
|
+
readonly version: number;
|
|
26
|
+
}
|
|
27
|
+
/** One write in a commit. Fields are decoded; the store encodes them with the thing's own schema. */
|
|
28
|
+
export type Write = {
|
|
29
|
+
readonly kind: "insert";
|
|
30
|
+
readonly thing: Thing;
|
|
31
|
+
readonly fields: Instance;
|
|
32
|
+
readonly state: string | undefined;
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: "update";
|
|
35
|
+
readonly thing: Thing;
|
|
36
|
+
readonly id: string;
|
|
37
|
+
/** The version this call read. The update lands only if the stored version is still this one. */
|
|
38
|
+
readonly version: number;
|
|
39
|
+
readonly changes: Instance;
|
|
40
|
+
readonly state: string | undefined;
|
|
41
|
+
};
|
|
42
|
+
/** Everything one call changes: its writes and the events its move emitted, committed together or not at all. */
|
|
43
|
+
export interface Commit {
|
|
44
|
+
readonly writes: ReadonlyArray<Write>;
|
|
45
|
+
readonly entries: ReadonlyArray<Entry>;
|
|
46
|
+
/** For an operation declared `idempotent: "request"`: its answer, kept under its request key in the same commit. */
|
|
47
|
+
readonly request?: {
|
|
48
|
+
readonly key: string;
|
|
49
|
+
readonly answer: string;
|
|
50
|
+
};
|
|
51
|
+
/** #764: each list the call read. The store answers `conflict` when the rows matching a list's `thing`, `where` and `state` are not these any more. */
|
|
52
|
+
readonly lists?: ReadonlyArray<ListRead>;
|
|
53
|
+
/** #1045: each row the call found, at the version it found it. The store answers `conflict` when one of them moved on. */
|
|
54
|
+
readonly reads?: ReadonlyArray<RowRead>;
|
|
55
|
+
}
|
|
56
|
+
/** A row one call found (#1045): its thing, its id and the version it was read at. */
|
|
57
|
+
export interface RowRead {
|
|
58
|
+
readonly thing: Thing;
|
|
59
|
+
readonly id: string;
|
|
60
|
+
readonly version: number;
|
|
61
|
+
}
|
|
62
|
+
/** A list one call read (#764): every row then matching its `thing`, `where` and `state`, whatever page the call asked for, at the version the call read it. */
|
|
63
|
+
export interface ListRead {
|
|
64
|
+
readonly thing: Thing;
|
|
65
|
+
readonly where: Instance;
|
|
66
|
+
readonly state: string | undefined;
|
|
67
|
+
readonly rows: ReadonlyArray<{
|
|
68
|
+
readonly id: string;
|
|
69
|
+
readonly version: number;
|
|
70
|
+
}>;
|
|
71
|
+
}
|
|
72
|
+
/** `conflict`: an update's version had moved on, so nothing was written and the call runs again. */
|
|
73
|
+
export type Committed = "committed" | "conflict";
|
|
74
|
+
/** A page of a thing's instances: those matching every filter, in the order they were made, `limit` at most. */
|
|
75
|
+
export interface ListQuery {
|
|
76
|
+
readonly thing: Thing;
|
|
77
|
+
/** Fields that must equal these values (decoded). A filter on `state` is `state`, not here. */
|
|
78
|
+
readonly where: Instance;
|
|
79
|
+
readonly state: string | undefined;
|
|
80
|
+
readonly order: "newest" | "oldest";
|
|
81
|
+
readonly limit: number;
|
|
82
|
+
/** The `next` of the page before: continue after the instance it names. */
|
|
83
|
+
readonly after: string | undefined;
|
|
84
|
+
}
|
|
85
|
+
/** `next` names the last instance of this page when more follow it, and is null otherwise. */
|
|
86
|
+
export interface Listed {
|
|
87
|
+
readonly items: ReadonlyArray<Instance>;
|
|
88
|
+
readonly next: string | null;
|
|
89
|
+
}
|
|
90
|
+
/** A store's page: `Listed`, with each item's version and its place, the order rows were made in (`rows[i]` is `items[i]`'s). */
|
|
91
|
+
export interface Page extends Listed {
|
|
92
|
+
readonly rows: ReadonlyArray<{
|
|
93
|
+
readonly version: number;
|
|
94
|
+
readonly place: number;
|
|
95
|
+
}>;
|
|
96
|
+
}
|
|
97
|
+
/** A rule a store enforces and a unit looks through: the invariant as declared, and its full name (`handle.oneHolderPerName`), which a defect and a span quote. */
|
|
98
|
+
export interface NamedRule {
|
|
99
|
+
readonly name: string;
|
|
100
|
+
readonly rule: Unique;
|
|
101
|
+
}
|
|
102
|
+
declare const Store_base: Context.ServiceClass<Store, "@orthograph/language-effect/Store", {
|
|
103
|
+
/** The rules this store enforces at commit (G5), as the model carries them; a unit made over it looks through the same ones. Absent when the project declares none. */
|
|
104
|
+
readonly rules?: ReadonlyArray<NamedRule>;
|
|
105
|
+
readonly find: (thing: Thing, id: string) => Effect.Effect<Stored | undefined>;
|
|
106
|
+
/** One page of a thing's instances, in the order they were made (never a clock). A thing that lives as actors has none to list. */
|
|
107
|
+
readonly list: (query: ListQuery) => Effect.Effect<Page>;
|
|
108
|
+
/** Where a row sits in the order rows were made, as `list` gives it; undefined when there is none. */
|
|
109
|
+
readonly place: (thing: Thing, id: string) => Effect.Effect<number | undefined>;
|
|
110
|
+
/** `conflict` too when another call committed the same request key first: the retry then finds its answer. */
|
|
111
|
+
readonly commit: (commit: Commit) => Effect.Effect<Committed>;
|
|
112
|
+
/** The answer already committed under a request key, as wire JSON, if there is one. */
|
|
113
|
+
readonly answered: (key: string) => Effect.Effect<string | undefined>;
|
|
114
|
+
}>;
|
|
115
|
+
/** The host's side. A failing store dies: it is never the caller's refusal. */
|
|
116
|
+
export declare class Store extends Store_base {
|
|
117
|
+
}
|
|
118
|
+
declare const Records_base: Context.ServiceClass<Records, "@orthograph/language-effect/Records", {
|
|
119
|
+
/** The instance of `thing` with this id, or undefined: what this call's own writes left it as, else the store's. */
|
|
120
|
+
readonly find: (thing: Thing, id: string) => Effect.Effect<Instance | undefined>;
|
|
121
|
+
/** The one instance holding `value` under a unique rule, within its scope: what this call's own writes left it as, else the store's. The store enforces the same rule at commit. */
|
|
122
|
+
readonly findBy: {
|
|
123
|
+
/** G5: the one instance holding `value` under the unique `rule`, within the rule's scope. The value is the key's own type, or an object of the fields for a composite key. */
|
|
124
|
+
<R extends Unique<any, any>>(rule: R, value: NonNullable<R["~key"]>): Effect.Effect<Instance | undefined>;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* #764: a page of `thing`'s instances matching `where` and `state`, as this call's own writes leave them: a row it moved out drops, a stored row it moved in
|
|
128
|
+
* keeps its place, and the rows it made come after every stored row, in the order made. Every matching row counts as read: the commit is a `conflict` when
|
|
129
|
+
* another call changed which rows match, or one of them. A thing that lives as actors has none to list.
|
|
130
|
+
*/
|
|
131
|
+
readonly list: (query: ListQuery) => Effect.Effect<Listed>;
|
|
132
|
+
/** A new instance, in `state` when the thing has a machine (a move sets it otherwise). */
|
|
133
|
+
readonly insert: <T extends Thing>(thing: T, fields: FieldsOf<T>, state?: string) => Effect.Effect<void>;
|
|
134
|
+
/** Change some fields of an instance. Guarded by the version this call read it at. */
|
|
135
|
+
/** `state` moves the instance with no event of its own: only for work a sweep does on instances other than the one its move is about (it journals nothing). */
|
|
136
|
+
readonly update: <T extends Thing>(thing: T, id: string, changes: Partial<FieldsOf<T>>, state?: string) => Effect.Effect<void>;
|
|
137
|
+
}>;
|
|
138
|
+
/** The handler's side, during one attempt at a call. */
|
|
139
|
+
export declare class Records extends Records_base {
|
|
140
|
+
}
|
|
141
|
+
export interface UnitApi {
|
|
142
|
+
/** A fresh attempt's `Records` over `store`, and what it holds back. `rules` default to the store's own. */
|
|
143
|
+
readonly make: (store: Store["Service"], rules?: ReadonlyArray<NamedRule>) => Effect.Effect<Unit>;
|
|
144
|
+
}
|
|
145
|
+
/** A rule this call's rows would break, found before the commit: the rule's name, its refusal, and the row as the call leaves it (the refusal is filled from it, never from the holder's). */
|
|
146
|
+
export interface Violation {
|
|
147
|
+
readonly rule: string;
|
|
148
|
+
/** The thing's own error the runtime refuses with; absent when no caller can break the rule. */
|
|
149
|
+
readonly refuse: AnyErrorClass | undefined;
|
|
150
|
+
readonly row: Instance;
|
|
151
|
+
/** The other holder is a row this call wrote too: the handler's own bug, never a refusal or a race. */
|
|
152
|
+
readonly own: boolean;
|
|
153
|
+
/** The rule says breaking it is a lost race: the call runs again and meets what the winner made. */
|
|
154
|
+
readonly retry?: boolean;
|
|
155
|
+
}
|
|
156
|
+
export interface Unit {
|
|
157
|
+
readonly records: Records["Service"];
|
|
158
|
+
/** Read an instance and remember its version, so the move's update is guarded by it. */
|
|
159
|
+
readonly read: (thing: Thing, id: string) => Effect.Effect<Instance | undefined>;
|
|
160
|
+
/** Set the state of `id`: on its held-back insert, or as an update guarded by the version read. */
|
|
161
|
+
readonly setState: (thing: Thing, id: string, state: string) => Effect.Effect<void>;
|
|
162
|
+
readonly writes: Effect.Effect<ReadonlyArray<Write>>;
|
|
163
|
+
/** #764: each list this call read, for the commit to guard. */
|
|
164
|
+
readonly lists: Effect.Effect<ReadonlyArray<ListRead>>;
|
|
165
|
+
/** #1045: each row this call found by its id, for the commit to guard. A row only a list found is guarded by that list. */
|
|
166
|
+
readonly reads: Effect.Effect<ReadonlyArray<RowRead>>;
|
|
167
|
+
/** G5: the first rule one of this call's rows breaks by taking a key another instance holds, or undefined. Run after the move, before `writes`. */
|
|
168
|
+
readonly check: Effect.Effect<Violation | undefined>;
|
|
169
|
+
}
|
|
170
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Records: a thing's stored instances. Two sides: the host's `Store`, which reads and commits, and the handler's
|
|
2
|
+
// `Records`, which reads through the store and holds its writes back until the call commits them with its move.
|
|
3
|
+
import { Context } from "effect";
|
|
4
|
+
/** The host's side. A failing store dies: it is never the caller's refusal. */
|
|
5
|
+
export class Store extends Context.Service()("@orthograph/language-effect/Store") {
|
|
6
|
+
}
|
|
7
|
+
/** The handler's side, during one attempt at a call. */
|
|
8
|
+
export class Records extends Context.Service()("@orthograph/language-effect/Records") {
|
|
9
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// `Timers` and `DurableTimers` on the world's table (M8.07), for a process that is not the world's Durable Object. A wait is a row in
|
|
2
|
+
// the world's table, not a fiber the world can see: the world fires it when its clock reaches the instant, holding a token of its own
|
|
3
|
+
// for the sleeper until the sleeper has taken its token back (`reclaim`) and acknowledged (`cancel`), so the world is never quiet
|
|
4
|
+
// between the firing and the wake.
|
|
5
|
+
import { Clock, Context, Duration, Effect, Layer, Option } from "effect";
|
|
6
|
+
import { Busy, BusyBoxRef } from "#modules/work/types.js";
|
|
7
|
+
import { DurableTimers, TimerWakes, Timers, WorldTable } from "../types.js";
|
|
8
|
+
/** The furthest instant a `Date` holds either side of the epoch: the world's clock cannot hold more, so nothing past it is set. */
|
|
9
|
+
const MAX_INSTANT = 8.64e15;
|
|
10
|
+
/** An instant the world can hold, or a defect naming what asked for it (`Duration.infinity`, `NaN`, a span past the range). */
|
|
11
|
+
const instantOf = (at, what) => Number.isFinite(at) && Math.abs(at) <= MAX_INSTANT ? Effect.succeed(at) : Effect.die(new Error(`${what} needs a finite instant within a Date's range, not ${at}`));
|
|
12
|
+
export const TimersWorld = Layer.effectContext(Effect.gen(function* () {
|
|
13
|
+
const busy = yield* Busy;
|
|
14
|
+
const table = yield* WorldTable;
|
|
15
|
+
const wakes = yield* Effect.serviceOption(TimerWakes);
|
|
16
|
+
/**
|
|
17
|
+
* Back to work: the sleeper takes its own token again first, and only then does the world let go of the one it held for it. The
|
|
18
|
+
* cancel runs even when the reclaim is refused (the world is closed while a reset drains): a row left behind would be fired later,
|
|
19
|
+
* and the token the world holds for it would never be acknowledged.
|
|
20
|
+
*/
|
|
21
|
+
const resume = (lease, id) => Effect.ensuring(lease.reclaim, table.cancel(id));
|
|
22
|
+
const timers = Timers.of({
|
|
23
|
+
// The row and the fiber that owns it are one uninterruptible step: an interrupt while the world is being told waits for it,
|
|
24
|
+
// so a row the world holds always has a waiter to take the token the world holds for it. The waiter gives the row up itself
|
|
25
|
+
// if it is interrupted while it waits.
|
|
26
|
+
at: (at, owner, name, work) => Effect.uninterruptibleMask((restore) => Effect.flatMap(Effect.andThen(instantOf(at, `a timer for ${owner} ${name}`), table.add(owner, name, at, owner)), (id) => {
|
|
27
|
+
const run = restore(table.wait(id)).pipe(
|
|
28
|
+
// The work gets a token of its own (taken before `spawn` returns) before the world lets go of the one it held for the row.
|
|
29
|
+
Effect.andThen(busy.as(owner).spawn(Effect.andThen(table.cancel(id), Effect.ignore(work)))),
|
|
30
|
+
// Whatever ends the wait or the spawn (an interrupt, a refused token while a reset drains), the row is given up: idempotent.
|
|
31
|
+
Effect.onError(() => table.cancel(id)));
|
|
32
|
+
return Effect.as(Effect.provideService(Effect.forkDetach(run), BusyBoxRef, undefined), id);
|
|
33
|
+
})),
|
|
34
|
+
sleep: (owner, span) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
35
|
+
const now = yield* Clock.currentTimeMillis;
|
|
36
|
+
const at = yield* instantOf(now + Duration.toMillis(span), `a sleep for ${owner}`);
|
|
37
|
+
// The row is published before the token is lent out, so the world never sees the process idle with no row.
|
|
38
|
+
const id = yield* table.add(owner, "sleep", at, owner);
|
|
39
|
+
const lease = yield* busy.lend;
|
|
40
|
+
// However the wait ends, the token is taken back and the row given up, once.
|
|
41
|
+
yield* restore(table.wait(id)).pipe(Effect.ensuring(resume(lease, id)));
|
|
42
|
+
})),
|
|
43
|
+
within: (owner, span, wait) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
44
|
+
const now = yield* Clock.currentTimeMillis;
|
|
45
|
+
const at = yield* instantOf(now + Duration.toMillis(span), `a deadline for ${owner}`);
|
|
46
|
+
const id = yield* table.add(owner, "deadline", at, owner);
|
|
47
|
+
const lease = yield* busy.lend;
|
|
48
|
+
// The token is lent out while the race runs, and its branches are not the request's work: the port call is what is awaited.
|
|
49
|
+
return yield* restore(Effect.provideService(Effect.raceFirst(Effect.map(wait, Option.some), table.wait(id).pipe(Effect.as(Option.none()))), BusyBoxRef, undefined)).pipe(Effect.ensuring(resume(lease, id)));
|
|
50
|
+
})),
|
|
51
|
+
due: () => Effect.succeed(undefined),
|
|
52
|
+
fire: () => Effect.succeed([]),
|
|
53
|
+
pending: table.sleeping,
|
|
54
|
+
});
|
|
55
|
+
// Rule 6 of M8-timers-shape: nothing could fire a durable timer where no `TimerWakes` is provided, so setting one is a defect.
|
|
56
|
+
const durable = DurableTimers.of({
|
|
57
|
+
set: (at, owner, name, wake) => Option.isNone(wakes)
|
|
58
|
+
? Effect.die(new Error(`no TimerWakes to fire ${owner} ${name}: provide the host's`))
|
|
59
|
+
: Effect.andThen(instantOf(at, `a durable timer for ${owner} ${name}`), table.set(at, owner, name, wake)),
|
|
60
|
+
clear: table.clear,
|
|
61
|
+
pending: table.durable,
|
|
62
|
+
});
|
|
63
|
+
return Context.make(Timers, timers).pipe(Context.add(DurableTimers, durable));
|
|
64
|
+
}));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { Busy } from "#modules/work/types.js";
|
|
3
|
+
import { DurableTimers, TimerWakes, Timers, type DurableTimer } from "./types.ts";
|
|
4
|
+
export { TimersWorld } from "./internal/world.ts";
|
|
5
|
+
/**
|
|
6
|
+
* Timers for code that runs on a device or in a plain process, with no world to drive its clock: `sleep` waits on the
|
|
7
|
+
* real clock. A timer to fire later (`at`) needs a world and is refused as a defect, so a device entry can wait but never
|
|
8
|
+
* schedule.
|
|
9
|
+
*/
|
|
10
|
+
export declare const TimersLive: Layer.Layer<Timers, never, never>;
|
|
11
|
+
/**
|
|
12
|
+
* Timers kept in memory, fired by whoever drives the world's clock: `Timers` and `DurableTimers` over one table, so a
|
|
13
|
+
* sleep and a durable timer due at one instant fire in the order they were set. A durable timer fires through the
|
|
14
|
+
* host's `TimerWakes`; with none provided, setting one is a defect. A world's Durable Object keeps the table (M8.07).
|
|
15
|
+
*/
|
|
16
|
+
export declare const TimersMemory: Layer.Layer<Timers | DurableTimers, never, Busy>;
|
|
17
|
+
/** The host's `TimerWakes`, one handler per wake kind. A kind with no handler is a defect: nothing set it legally. */
|
|
18
|
+
export declare const wakesByKind: (handlers: Readonly<Record<string, (timer: DurableTimer) => Effect.Effect<void>>>) => Layer.Layer<TimerWakes, never, never>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Clock, Context, Deferred, Duration, Effect, Layer, Option } from "effect";
|
|
2
|
+
import { Busy } from "#modules/work/types.js";
|
|
3
|
+
import { DurableTimers, TimerWakes, Timers } from "./types.js";
|
|
4
|
+
import { BusyBoxRef } from "#modules/work/types.js";
|
|
5
|
+
export { TimersWorld } from "./internal/world.js";
|
|
6
|
+
const timerOf = (t) => ({ id: t.id, at: t.at, owner: t.owner, name: t.name });
|
|
7
|
+
const durableOf = (t) => ({ ...timerOf(t), wake: t.wake });
|
|
8
|
+
/**
|
|
9
|
+
* Timers for code that runs on a device or in a plain process, with no world to drive its clock: `sleep` waits on the
|
|
10
|
+
* real clock. A timer to fire later (`at`) needs a world and is refused as a defect, so a device entry can wait but never
|
|
11
|
+
* schedule.
|
|
12
|
+
*/
|
|
13
|
+
export const TimersLive = Layer.succeed(Timers, Timers.of({
|
|
14
|
+
at: () => Effect.die(new Error("a device has no world to fire a timer: it can wait, not schedule")),
|
|
15
|
+
sleep: (_owner, span) => Effect.flatMap(Clock.Clock, (clock) => clock.sleep(Duration.fromInputUnsafe(span))),
|
|
16
|
+
due: () => Effect.succeed(undefined),
|
|
17
|
+
fire: () => Effect.succeed([]),
|
|
18
|
+
pending: Effect.succeed([]),
|
|
19
|
+
}));
|
|
20
|
+
/**
|
|
21
|
+
* Timers kept in memory, fired by whoever drives the world's clock: `Timers` and `DurableTimers` over one table, so a
|
|
22
|
+
* sleep and a durable timer due at one instant fire in the order they were set. A durable timer fires through the
|
|
23
|
+
* host's `TimerWakes`; with none provided, setting one is a defect. A world's Durable Object keeps the table (M8.07).
|
|
24
|
+
*/
|
|
25
|
+
export const TimersMemory = Layer.effectContext(Effect.gen(function* () {
|
|
26
|
+
const busy = yield* Busy;
|
|
27
|
+
const wakes = yield* Effect.serviceOption(TimerWakes);
|
|
28
|
+
const table = [];
|
|
29
|
+
let next = 1;
|
|
30
|
+
const add = (at, owner, name, run, wake) => Effect.sync(() => {
|
|
31
|
+
const id = next++;
|
|
32
|
+
table.push(wake === undefined ? { id, at, owner, name, run } : { id, at, owner, name, run, wake });
|
|
33
|
+
return id;
|
|
34
|
+
});
|
|
35
|
+
const remove = (id) => {
|
|
36
|
+
const pending = table.findIndex((timer) => timer.id === id);
|
|
37
|
+
if (pending === -1)
|
|
38
|
+
return false;
|
|
39
|
+
table.splice(pending, 1);
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
const durable = DurableTimers.of({
|
|
43
|
+
set: (at, owner, name, wake) => Effect.suspend(() => {
|
|
44
|
+
if (!Number.isFinite(at))
|
|
45
|
+
return Effect.die(new Error(`a durable timer for ${owner} ${name} needs a finite instant, not ${at}`));
|
|
46
|
+
if (Option.isNone(wakes))
|
|
47
|
+
return Effect.die(new Error(`no TimerWakes to fire ${owner} ${name}: provide the host's`));
|
|
48
|
+
const host = wakes.value;
|
|
49
|
+
// One synchronous step: the pending check, the id and the insert, so two setters of one (owner, name) make one row.
|
|
50
|
+
const pending = table.find((t) => t.wake !== undefined && t.owner === owner && t.name === name);
|
|
51
|
+
if (pending !== undefined)
|
|
52
|
+
return Effect.succeed(pending.id);
|
|
53
|
+
const id = next++;
|
|
54
|
+
const timer = { id, at, owner, name, wake };
|
|
55
|
+
// A wake that fails puts its row back and fails the firing, so the driver sees it and nothing is lost; unless the
|
|
56
|
+
// same (owner, name) was set again while it ran, when the newer one stands and there is still only one.
|
|
57
|
+
const restore = Effect.sync(() => {
|
|
58
|
+
if (!table.some((t) => t.wake !== undefined && t.owner === owner && t.name === name))
|
|
59
|
+
table.push(row);
|
|
60
|
+
});
|
|
61
|
+
const run = busy.counted(host.wake(timer)).pipe(Effect.onError(() => restore));
|
|
62
|
+
const row = { ...timer, run };
|
|
63
|
+
table.push(row);
|
|
64
|
+
return Effect.succeed(id);
|
|
65
|
+
}),
|
|
66
|
+
clear: (owner, name) => Effect.sync(() => {
|
|
67
|
+
const pending = table.find((t) => t.wake !== undefined && t.owner === owner && t.name === name);
|
|
68
|
+
return pending === undefined ? false : remove(pending.id);
|
|
69
|
+
}),
|
|
70
|
+
pending: Effect.sync(() => table.flatMap((t) => (t.wake === undefined ? [] : [durableOf({ ...t, wake: t.wake })])).sort((a, b) => a.at - b.at || a.id - b.id)),
|
|
71
|
+
});
|
|
72
|
+
const timers = Timers.of({
|
|
73
|
+
at: (at, owner, name, work) => add(at, owner, name, busy.spawn(Effect.ignore(work))),
|
|
74
|
+
sleep: (owner, span) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
75
|
+
const now = yield* Clock.currentTimeMillis;
|
|
76
|
+
const woken = yield* Deferred.make();
|
|
77
|
+
let lease;
|
|
78
|
+
// The timer takes the sleeper's token back before waking it, so a woken fiber is never uncounted.
|
|
79
|
+
const id = yield* add(now + Duration.toMillis(span), owner, "sleep", Effect.suspend(() => Effect.andThen(lease?.reclaim ?? Effect.void, Deferred.succeed(woken, undefined))));
|
|
80
|
+
lease = yield* busy.lend;
|
|
81
|
+
const mine = lease;
|
|
82
|
+
yield* restore(Deferred.await(woken)).pipe(Effect.onInterrupt(() => Effect.gen(function* () {
|
|
83
|
+
if (remove(id))
|
|
84
|
+
yield* mine.reclaim;
|
|
85
|
+
// A firing timer has already claimed ownership of the wakeup.
|
|
86
|
+
else
|
|
87
|
+
yield* Deferred.await(woken);
|
|
88
|
+
})));
|
|
89
|
+
})),
|
|
90
|
+
within: (owner, span, wait) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
91
|
+
const now = yield* Clock.currentTimeMillis;
|
|
92
|
+
const ended = yield* Deferred.make();
|
|
93
|
+
let lease;
|
|
94
|
+
const id = yield* add(now + Duration.toMillis(span), owner, "deadline", Effect.suspend(() => Effect.andThen(lease?.reclaim ?? Effect.void, Deferred.succeed(ended, undefined))));
|
|
95
|
+
// Publish the deadline before lending our token out so the world can advance it.
|
|
96
|
+
lease = yield* busy.lend;
|
|
97
|
+
const mine = lease;
|
|
98
|
+
return yield* restore(Effect.provideService(Effect.raceFirst(Effect.map(wait, Option.some), Deferred.await(ended).pipe(Effect.as(Option.none()))), BusyBoxRef, undefined)).pipe(Effect.ensuring(Effect.sync(() => remove(id)).pipe(Effect.andThen(mine.reclaim))));
|
|
99
|
+
})),
|
|
100
|
+
due: (target) => Effect.sync(() => table.reduce((best, t) => (t.at <= target && (best === undefined || t.at < best) ? t.at : best), undefined)),
|
|
101
|
+
fire: (at) => Effect.gen(function* () {
|
|
102
|
+
const hit = table.filter((t) => t.at === at).sort((a, b) => a.id - b.id);
|
|
103
|
+
const fired = [];
|
|
104
|
+
// Each row leaves the table only as its own firing begins, so a firing that fails leaves the rest pending.
|
|
105
|
+
for (const t of hit) {
|
|
106
|
+
const row = table.indexOf(t);
|
|
107
|
+
if (row === -1)
|
|
108
|
+
continue;
|
|
109
|
+
table.splice(row, 1);
|
|
110
|
+
yield* t.run;
|
|
111
|
+
fired.push(timerOf(t));
|
|
112
|
+
}
|
|
113
|
+
return fired;
|
|
114
|
+
}),
|
|
115
|
+
pending: Effect.sync(() => table.map(timerOf).sort((a, b) => a.at - b.at || a.id - b.id)),
|
|
116
|
+
});
|
|
117
|
+
return Context.make(Timers, timers).pipe(Context.add(DurableTimers, durable));
|
|
118
|
+
}));
|
|
119
|
+
/** The host's `TimerWakes`, one handler per wake kind. A kind with no handler is a defect: nothing set it legally. */
|
|
120
|
+
export const wakesByKind = (handlers) => Layer.succeed(TimerWakes, TimerWakes.of({
|
|
121
|
+
wake: (timer) => {
|
|
122
|
+
const handler = Object.hasOwn(handlers, timer.wake.kind) ? handlers[timer.wake.kind] : undefined;
|
|
123
|
+
return handler === undefined ? Effect.die(new Error(`no wake handler for kind ${timer.wake.kind}`)) : handler(timer);
|
|
124
|
+
},
|
|
125
|
+
}));
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Context, type Duration, type Effect, type Option } from "effect";
|
|
2
|
+
import { Busy } from "#modules/work/types.js";
|
|
3
|
+
export interface Timer {
|
|
4
|
+
readonly id: number;
|
|
5
|
+
/** Epoch milliseconds on the world's clock. */
|
|
6
|
+
readonly at: number;
|
|
7
|
+
/** Who set it, as a path: `agents/run/<runId>`, `workflows/<workflow>/<execution>`, `keeper/<thing>.<move>/<id>`. */
|
|
8
|
+
readonly owner: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
}
|
|
11
|
+
declare const Timers_base: Context.ServiceClass<Timers, "@orthograph/language-effect/Timers", {
|
|
12
|
+
/** Fire `work` at the instant `at`, as counted work. In-process only: `work` is a closure. */
|
|
13
|
+
readonly at: <E>(at: number, owner: string, name: string, work: Effect.Effect<void, E>) => Effect.Effect<number>;
|
|
14
|
+
/** A durable sleep: park until the world fires it. Must be called from counted work. */
|
|
15
|
+
readonly sleep: (owner: string, span: Duration.Input) => Effect.Effect<void>;
|
|
16
|
+
/** Park counted work while awaiting an interruptible port call. None means its timer won. Every world's Timers has it. */
|
|
17
|
+
readonly within?: <A, E, R>(owner: string, span: Duration.Input, wait: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>;
|
|
18
|
+
/** The world's driver only: the earliest instant a timer is due at or before `target`, if any. */
|
|
19
|
+
readonly due: (target: number) => Effect.Effect<number | undefined>;
|
|
20
|
+
/** The world's driver only: fire every timer due at exactly `at`, in order (set first, then owner); returns what fired. */
|
|
21
|
+
readonly fire: (at: number) => Effect.Effect<ReadonlyArray<Timer>>;
|
|
22
|
+
readonly pending: Effect.Effect<ReadonlyArray<Timer>>;
|
|
23
|
+
}>;
|
|
24
|
+
export declare class Timers extends Timers_base {
|
|
25
|
+
/**
|
|
26
|
+
* `Effect.all`, for work that sleeps in parallel with other work: the parent only waits, so it is not counted while it does, and the world can fire the sleeper
|
|
27
|
+
* once the working branches are done. A plain `Effect.all` stays counted, with its children, until every one is done (a move meanwhile
|
|
28
|
+
* fails `NeverQuiet`, naming the request).
|
|
29
|
+
*/
|
|
30
|
+
static readonly all: <A, E, R>(effects: ReadonlyArray<Effect.Effect<A, E, R>>, options?: {
|
|
31
|
+
readonly concurrency?: number | "unbounded" | undefined;
|
|
32
|
+
}) => Effect.Effect<ReadonlyArray<A>, E, R | Busy>;
|
|
33
|
+
/** `Effect.raceFirst`, as a join. */
|
|
34
|
+
static readonly raceFirst: <A, E, R, A2, E2, R2>(a: Effect.Effect<A, E, R>, b: Effect.Effect<A2, E2, R2>) => Effect.Effect<A | A2, E | E2, R | R2 | Busy>;
|
|
35
|
+
}
|
|
36
|
+
/** JSON, as the world's table stores it. */
|
|
37
|
+
export type Json = null | boolean | number | string | ReadonlyArray<Json> | {
|
|
38
|
+
readonly [key: string]: Json;
|
|
39
|
+
};
|
|
40
|
+
/** What a durable timer hands back when it fires: a kind the host routes on, and JSON for the woken code. */
|
|
41
|
+
export interface Wake {
|
|
42
|
+
/** One handler per kind in the host: `"workflow"`, `"agent"`, `"keeper"`. */
|
|
43
|
+
readonly kind: string;
|
|
44
|
+
/** For a workflow, the DurableDeferred's token; for an agent run, the run's id. */
|
|
45
|
+
readonly payload: Json;
|
|
46
|
+
}
|
|
47
|
+
export interface DurableTimer extends Timer {
|
|
48
|
+
readonly wake: Wake;
|
|
49
|
+
}
|
|
50
|
+
declare const DurableTimers_base: Context.ServiceClass<DurableTimers, "@orthograph/language-effect/DurableTimers", {
|
|
51
|
+
/**
|
|
52
|
+
* Set a timer that outlives this fiber and this process: a row in the world's table. Once per `(owner, name)`: while
|
|
53
|
+
* one is pending, setting it again returns the first id and keeps its instant and wake (a replayed activity sets
|
|
54
|
+
* nothing new). To move one, `clear` it and set it again. In a real world the row is kept behind an alarm.
|
|
55
|
+
*/
|
|
56
|
+
readonly set: (at: number, owner: string, name: string, wake: Wake) => Effect.Effect<number>;
|
|
57
|
+
/** Clear it if it has not fired; false when none was pending. Leaving a state clears the keeper's timer this way. */
|
|
58
|
+
readonly clear: (owner: string, name: string) => Effect.Effect<boolean>;
|
|
59
|
+
readonly pending: Effect.Effect<ReadonlyArray<DurableTimer>>;
|
|
60
|
+
}>;
|
|
61
|
+
export declare class DurableTimers extends DurableTimers_base {
|
|
62
|
+
}
|
|
63
|
+
declare const TimerWakes_base: Context.ServiceClass<TimerWakes, "@orthograph/language-effect/TimerWakes", {
|
|
64
|
+
readonly wake: (timer: DurableTimer) => Effect.Effect<void>;
|
|
65
|
+
}>;
|
|
66
|
+
/**
|
|
67
|
+
* The host's side: how a fired durable timer reaches the code that set it, in whatever process now runs it (an evicted
|
|
68
|
+
* Durable Object is woken by its binding). The world calls `wake` at least once per firing, as counted work, and `wake`
|
|
69
|
+
* returns only after the work it woke holds its own token, so the world is never quiet between the two. A handler is
|
|
70
|
+
* idempotent by `(owner, name, at)`: a second firing changes nothing.
|
|
71
|
+
*/
|
|
72
|
+
export declare class TimerWakes extends TimerWakes_base {
|
|
73
|
+
}
|
|
74
|
+
declare const WorldTable_base: Context.ServiceClass<WorldTable, "@orthograph/language-effect/WorldTable", {
|
|
75
|
+
/**
|
|
76
|
+
* Park a row for a sleeper: `at` on the world's clock. `holder` names the token the world holds for the sleeper once the
|
|
77
|
+
* row fires. Returns its id.
|
|
78
|
+
*/
|
|
79
|
+
readonly add: (owner: string, name: string, at: number, holder: string) => Effect.Effect<number>;
|
|
80
|
+
/**
|
|
81
|
+
* Resolve when the world fires the row. The world holds a token of its own for the sleeper from the firing, and the host has
|
|
82
|
+
* put this request on the world's instant at the firing, so the caller reads the instant it woke at.
|
|
83
|
+
*/
|
|
84
|
+
readonly wait: (id: number) => Effect.Effect<void>;
|
|
85
|
+
/**
|
|
86
|
+
* The sleeper is back (it holds its own token again) or gave up: take the row back if it has not fired, and let go of the
|
|
87
|
+
* token the world held for it if it has. Idempotent; a row the world no longer knows is a no-op.
|
|
88
|
+
*/
|
|
89
|
+
readonly cancel: (id: number) => Effect.Effect<void>;
|
|
90
|
+
readonly sleeping: Effect.Effect<ReadonlyArray<Timer>>;
|
|
91
|
+
readonly set: (at: number, owner: string, name: string, wake: Wake) => Effect.Effect<number>;
|
|
92
|
+
readonly clear: (owner: string, name: string) => Effect.Effect<boolean>;
|
|
93
|
+
readonly durable: Effect.Effect<ReadonlyArray<DurableTimer>>;
|
|
94
|
+
}>;
|
|
95
|
+
/**
|
|
96
|
+
* The world's timer table as another process sees it (M8.07): rows kept in the world's Durable Object, which fires them
|
|
97
|
+
* when its clock reaches them. A sleeping row lives as long as the process that parked it; a durable row outlives it.
|
|
98
|
+
* The host provides this (host-cloudflare, over the world's RPC); `TimersWorld` is `Timers` and `DurableTimers` on it.
|
|
99
|
+
*/
|
|
100
|
+
export declare class WorldTable extends WorldTable_base {
|
|
101
|
+
}
|
|
102
|
+
export {};
|