@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
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { Access, Bundle, Eid, Match, Plugin, ReadTx } from "@yaks/graph";
|
|
2
|
+
import { type EffectDecl, type Vocab } from "@yaks/vocab";
|
|
3
|
+
import { type Event, type Kind } from "./trace.js";
|
|
4
|
+
import { type Write } from "./write.js";
|
|
5
|
+
import { type Description, type Policy, type Registration } from "./registration.js";
|
|
6
|
+
import { type Attempt, type PoolOpts, type Run, type WorkOpts } from "./pool.js";
|
|
7
|
+
export type { Description, Policy, Registration } from "./registration.js";
|
|
8
|
+
/** A post-commit handler: what happened, a detached transaction to read
|
|
9
|
+
* through, the callback to write through ({@link Write}), and, for a run the
|
|
10
|
+
* pool claimed, which attempt it is ({@link Attempt}). Its return value is
|
|
11
|
+
* awaited when it is a promise. */ export type Handler = (event: Event, tx: ReadTx, write: Write, attempt?: Attempt) => unknown;
|
|
12
|
+
type DeclaredHandler = Handler & {
|
|
13
|
+
/** A dependency this process knows is still unavailable. Runs deferred
|
|
14
|
+
* with this reason stay pending without being claimed again. */ waiting?: () => string | undefined;
|
|
15
|
+
};
|
|
16
|
+
/** Where a `fifo` effect's pending runs stand in its queue: a number for each
|
|
17
|
+
* run, read whenever the queue's head is chosen. The lowest goes first; runs
|
|
18
|
+
* ranked alike, or not at all, go oldest first. */ export type Rank = (runs: Bundle[], tx: ReadTx) => Promise<(number | undefined)[]>;
|
|
19
|
+
/** The code for declared effects, by the name each is declared under. */ export type Handlers = Record<string, DeclaredHandler>;
|
|
20
|
+
/**
|
|
21
|
+
* A `fifo` effect's handler that orders its queue by `rank`.
|
|
22
|
+
*
|
|
23
|
+
* ```ts ignore
|
|
24
|
+
* fx.handle({ start: ranked(starting, (runs) => urgency(runs)) })
|
|
25
|
+
* ```
|
|
26
|
+
*/ export declare let ranked: (run: Handler, rank: Rank) => Handler & {
|
|
27
|
+
rank: Rank;
|
|
28
|
+
};
|
|
29
|
+
/** One registration: the component it watches, what has to happen to it, and
|
|
30
|
+
* what runs. A declared effect with several triggers is several slots under
|
|
31
|
+
* one name. */ export type Slot = Policy & {
|
|
32
|
+
/** a declared effect's name, or an observer's `post.created`,
|
|
33
|
+
* `post.changed.published`, `post.removed` (with `#2` for a second one) */ id: string;
|
|
34
|
+
/** Related observers registered in one on() call. */ group?: string;
|
|
35
|
+
/** the component name it watches */ comp: string;
|
|
36
|
+
/** what has to happen to that component */ kind: Kind;
|
|
37
|
+
/** for a `changed` slot, the properties one of which has to have moved;
|
|
38
|
+
* absent means any */ props?: string[];
|
|
39
|
+
/** a `matched` slot's pattern: the parsed query it was registered with */ plan?: Match;
|
|
40
|
+
/** the components that pattern reads — a batch that moved none of them
|
|
41
|
+
* cannot have changed whether it holds, so it is not asked */ watch?: string[];
|
|
42
|
+
/** `-comp` — the components the pattern requires this batch to have
|
|
43
|
+
* removed, answered through `tx.bindings` with the batch passed in */ gone?: string[];
|
|
44
|
+
/** the declaration, for a declared effect */ effect?: EffectDecl;
|
|
45
|
+
/** A call or a match that supplies a callable effect's inputs. */ invocation?: true | Record<string, unknown>;
|
|
46
|
+
declaration?: string;
|
|
47
|
+
/** Whether this subject still owes the work at dispatch. */ eligible?: (eid: Eid, tx: ReadTx) => boolean | Promise<boolean>;
|
|
48
|
+
/** the code: always for an observer, once handled for an effect */ run?: DeclaredHandler;
|
|
49
|
+
/** what orders a `fifo` effect's queue, when its code is {@link ranked} */ rank?: Rank;
|
|
50
|
+
};
|
|
51
|
+
/** A handler or registry failure, for a {@link Report}. */ export type Job = {
|
|
52
|
+
/** the slot's id */ handler: string;
|
|
53
|
+
/** the committed change it is running for; absent when finding work failed */ event?: Event;
|
|
54
|
+
/** the registration itself, where there is one */ slot?: Slot;
|
|
55
|
+
};
|
|
56
|
+
/** Where a failing handler goes. A report is telemetry: it is called instead
|
|
57
|
+
* of the error being thrown, and must not throw itself. */ export type Report = (err: unknown, ctx: Job) => void;
|
|
58
|
+
/** How a registry is built. */ export type Opts = Partial<PoolOpts> & {
|
|
59
|
+
/** where a failing handler is reported (default: `console.warn`) */ report?: Report;
|
|
60
|
+
/** the plugin's name, for diagnostics (default: `@yaks/effects`) */ name?: string;
|
|
61
|
+
/** how a handler writes back: one new batch through the graph's own
|
|
62
|
+
* `apply()`, trusted (see {@link Write}). Without it a handler that tries to
|
|
63
|
+
* write is reported rather than quietly writing past the pipeline. */ write?: Write;
|
|
64
|
+
/** which declared effects a commit owes runs for: only those this process
|
|
65
|
+
* handles (`handled`, the default: a process that runs every effect itself,
|
|
66
|
+
* as a yaks.app store does), or every declared one (`declared`: a process
|
|
67
|
+
* that writes for another to run, and cannot see the code that one holds) */ owes?: 'handled' | 'declared';
|
|
68
|
+
/** how many generations of effect-written batches still owe runs (default:
|
|
69
|
+
* `2`). A batch from a client is generation 0 and an effect's own write is
|
|
70
|
+
* 1. The default lets an output effect write an entity, its change effect
|
|
71
|
+
* start a new session, and that session's effect see the request. */ depth?: number;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* A registry: a {@link https://jsr.io/@yaks/graph | @yaks/graph} plugin, plus
|
|
75
|
+
* the methods that register on it. Registration is chainable and may happen
|
|
76
|
+
* at any time — before the graph is built, or after, which is how a handler
|
|
77
|
+
* closes over the graph it writes back through.
|
|
78
|
+
*/ export type Effects = Plugin & {
|
|
79
|
+
/** observe an entity gaining this component */ created: (comp: string, run: Handler, policy?: Policy) => Effects;
|
|
80
|
+
/** observe this component being patched — for one property, or for any */ changed: (comp: string, prop: string | Handler, run?: Handler, policy?: Policy) => Effects;
|
|
81
|
+
/** observe this component going, by its own deletion or with its entity */ removed: (comp: string, run: Handler, policy?: Policy) => Effects;
|
|
82
|
+
/**
|
|
83
|
+
* Observe. Two forms, because there are two questions:
|
|
84
|
+
*
|
|
85
|
+
* fx.on('post', { created, changed: { published }, removed })
|
|
86
|
+
* fx.on('$call .call, !results', (e) => run(e.entity.eid))
|
|
87
|
+
*
|
|
88
|
+
* The first names a component and what has to happen to it; the second is a
|
|
89
|
+
* pattern — any query, any number of entities — and it runs wherever the
|
|
90
|
+
* batch just made it true.
|
|
91
|
+
*/ on: {
|
|
92
|
+
(comp: string, registration: Registration) : Effects;
|
|
93
|
+
(pattern: string | Match, run: Handler, policy?: Policy) : Effects;
|
|
94
|
+
};
|
|
95
|
+
/** the code for declared effects, by name. A name the vocabulary does not
|
|
96
|
+
* declare is an error, so a typo never goes quietly unrun. */ handle: (handlers: Handlers) => Effects;
|
|
97
|
+
/** every registration, declared ones first */ slots: () => Slot[];
|
|
98
|
+
/** Settle unclaimed pending runs no longer owed by this declaration/config. */ settle: (g: Access, handler: string) => Promise<number>;
|
|
99
|
+
/** Documentation derived from the registered slots. */ docs: () => Description[];
|
|
100
|
+
/** Owe each handled start-up effect, targeting owner, without joining the
|
|
101
|
+
* pool. Where the vocabulary keeps `effect` rows, persist fresh runs;
|
|
102
|
+
* otherwise run the handlers inline. Persistence or inline handler failures
|
|
103
|
+
* reject; inline failures are also reported. The host calls this when its
|
|
104
|
+
* start-up work is owed. */ startup: (g: Access) => Promise<void>;
|
|
105
|
+
/** Work the pool: claim what is owed and run it (./pool.ts). A live signal
|
|
106
|
+
* keeps working until it aborts; an aborted one is one pass, taken only
|
|
107
|
+
* where no process that stays up is working the pool. `startup: true` also
|
|
108
|
+
* owes fresh start-up runs, delivering them inline where the vocabulary
|
|
109
|
+
* keeps no `effect` rows. A finite pool drain answers whether it admitted
|
|
110
|
+
* or settled any work. */ work: (g: Access, signal?: AbortSignal, options?: number | WorkOpts) => Promise<boolean>;
|
|
111
|
+
/** Look at the pool again now, rather than at the next pass. */ wake: () => void;
|
|
112
|
+
/** Settles once every run this process started has, and a worker whose
|
|
113
|
+
* signal aborted has stopped. */ idle: () => Promise<void>;
|
|
114
|
+
/** Leave the pool: claim nothing more, so what this process commits from now
|
|
115
|
+
* on is left for the others, and settle what it started. What a process
|
|
116
|
+
* does on its way out. */ stop: () => Promise<void>;
|
|
117
|
+
/** On forced exit, leave unfinished claims due for the next worker at
|
|
118
|
+
* `at`. The retiring worker must exit before they become due. */ handoff: (at: number) => Promise<void>;
|
|
119
|
+
/** The runs going in this process, oldest first: what a process winding
|
|
120
|
+
* down is waiting on. */ running: () => Run[];
|
|
121
|
+
/** When the soonest run this process can take falls due, in ms: `now` when
|
|
122
|
+
* one is due already, null when none is owed or there is no pool
|
|
123
|
+
* (./pool.ts `next`). */ next: (g: Access, now?: number) => Promise<number | null>;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* A registry of post-commit effects and observers, as a plugin:
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* import { graph } from '@yaks/graph'
|
|
130
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
131
|
+
* import { ram } from '@yaks/ram'
|
|
132
|
+
* import { effects } from '@yaks/effects'
|
|
133
|
+
*
|
|
134
|
+
* let title = { type: 'string' }
|
|
135
|
+
* let vocab = loadVocab([{
|
|
136
|
+
* $defs: {
|
|
137
|
+
* post: { component: true, properties: { title } },
|
|
138
|
+
* send_receipt: { effect: true, created: ['post'] },
|
|
139
|
+
* },
|
|
140
|
+
* }])
|
|
141
|
+
* let fx = effects(vocab)
|
|
142
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [fx] })
|
|
143
|
+
*
|
|
144
|
+
* fx.handle({ send_receipt: (e) => console.log('mail', e.entity.eid) })
|
|
145
|
+
* fx.created('post', (e) => console.log('a post appeared', e.comp?.title))
|
|
146
|
+
* g.apply([{ entity: { eid: 'p1' }, post: { title: 'Hello' } }])
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* It requires the loaded vocabulary: the effects it declares are what a commit
|
|
150
|
+
* owes, and knowing what a cascade will delete — and therefore which
|
|
151
|
+
* components a casualty carried — is a question only a vocabulary can answer.
|
|
152
|
+
*/ export declare let effects: (vocab: Vocab, opts?: Opts) => Effects;
|
|
153
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sources":["../registry.ts"],"names":[],"mappings":"AA6CA,cACE,MAAM,EACN,MAAM,EAEN,GAAG,EAGH,KAAK,EACL,MAAM,EACN,MAAM,2BACuB;AAkB/B,SACE,KAAK,UAAU,EAGf,KAAK,KAAK,2BACmB;AAE/B,SAIE,KAAK,KAAK,EAEV,KAAK,IAAI,qBAGQ;AACnB,SAA6C,KAAK,KAAK,qBAAoB;AAG3E,SAEE,KAAK,WAAW,EAChB,KAAK,MAAM,EACX,KAAK,YAAY,4BACO;AAC1B,SACE,KAAK,OAAO,EAIZ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ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Bundle, Graph } from "@yaks/graph";
|
|
2
|
+
import type { Runs } from "@yaks/graph/tools";
|
|
3
|
+
import { type Vocab } from "@yaks/vocab";
|
|
4
|
+
/** What configuration this package's check accepts. */ export type Options = {
|
|
5
|
+
/** how long a run may sit pending past its due instant before that means
|
|
6
|
+
* nothing is working them, in minutes (default 10) */ minutes?: number;
|
|
7
|
+
/** how many of the failed runs to name (default 5) */ sample?: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The write that retries (failed runs back to pending with a fresh allowance)
|
|
11
|
+
* or drops (failed runs, and pending ones no handler is declared for, deleted)
|
|
12
|
+
* the runs `of` names: a handler, or one run's id. Each change is guarded by
|
|
13
|
+
* the state read, so a run a worker has since touched is left alone. Empty
|
|
14
|
+
* when `of` names none. What `effect_retry` and `effect_drop` apply, and what
|
|
15
|
+
* a host without those tools applies the same way (yaks.app's
|
|
16
|
+
* `store_effect_retry`).
|
|
17
|
+
*/ export declare let settling: (graph: Pick<Graph, 'read' | 'get' | 'address'>, vocab: Vocab, act: 'retry' | 'drop', of: string) => Promise<Bundle[]>;
|
|
18
|
+
/** The implementation behind the tool ./vocab.json declares. */ export declare let runs: (host: {
|
|
19
|
+
vocab: Vocab;
|
|
20
|
+
}, options?: Options) => Runs;
|
|
21
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sources":["../tools.ts"],"names":[],"mappings":"AA2BA,cAAc,MAAM,EAAQ,KAAK,2BAAgC;AAIjE,cAAc,IAAI,iCAAsC;AAMxD,SAAoB,KAAK,KAAK,2BAAgC;AAK9D,qDAAqD,GACrD,YAAY;EACV;sDACoD,GACpD,UAAU,MAAM;EAChB,oDAAoD,GACpD,SAAS,MAAM;;AAgDjB;;;;;;;;CAQC,GACD,OAAO,YAAI,WACT,OAAO,KAAK,OAAO,SAAS,QAAQ,YACpC,OAAO,OACP,KAAK,UAAU,QACf,IAAI,MAAM,KACT,QAAQ,UAUV;AAkBD,8DAA8D,GAC9D,OAAO,YAAI,OACT;EAAQ,OAAO;GACf,UAAS,YACR,KAsFF"}
|
package/_dist/trace.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Actor, Ask, Binding, Bundle, Comp, Eid, Entity, Tx } from "@yaks/graph";
|
|
2
|
+
import type { Vocab } from "@yaks/vocab";
|
|
3
|
+
import type { Invocation } from "./invocation.js";
|
|
4
|
+
/** What triggered a handler: one of the three things that can happen to a
|
|
5
|
+
* component, or a pattern that is now true over what the batch committed. */ export type Kind = 'created' | 'changed' | 'removed' | 'matched' | 'started';
|
|
6
|
+
/**
|
|
7
|
+
* One committed component change, as an effect sees it.
|
|
8
|
+
*
|
|
9
|
+
* `comp` is the patch as applied, which is what makes a change readable: on a
|
|
10
|
+
* `created` event it is the whole birth row, on a `changed` event only the
|
|
11
|
+
* properties that moved, and on a `removed` event there is nothing left to
|
|
12
|
+
* carry. What went is `before` instead: the component, as it stood before the
|
|
13
|
+
* commit.
|
|
14
|
+
*/ export type Event = {
|
|
15
|
+
/** When the committed change owed this durable run, preserved across retries. */ at?: string;
|
|
16
|
+
/** This execution's durable pool row. */ run?: Eid;
|
|
17
|
+
/** Inputs captured when the declared match committed. */ invocation?: Invocation;
|
|
18
|
+
/** what happened */ kind: Kind;
|
|
19
|
+
/** the entity it happened to, with the `num` storage minted */ entity: Entity;
|
|
20
|
+
/** the component's name */ name: string;
|
|
21
|
+
/** Components this commit moved on the same entity, including removals. */ touched?: string[];
|
|
22
|
+
/** the properties the change carried (absent on `removed`) */ comp?: Comp;
|
|
23
|
+
/** on `removed`, the entity holding the component that went, as the commit
|
|
24
|
+
* found it, and no other. Absent where nothing was read before the commit
|
|
25
|
+
* (a component born and removed in one batch). */ before?: Bundle;
|
|
26
|
+
/** what each variable bound, for a `matched` event whose pattern named some */ vars?: Record<string, unknown>;
|
|
27
|
+
/** the whole multi-entity match, including its collected members */ binding?: Binding;
|
|
28
|
+
/** Who wrote the commit, as its batch signed the entity: absent for
|
|
29
|
+
* automation. A run that writes on the commit's behalf (a call it asks
|
|
30
|
+
* for) signs with it, `signed(bundles, event.actor)`, as a rule inside
|
|
31
|
+
* the write is; what it writes unsigned is automation. */ actor?: Actor;
|
|
32
|
+
};
|
|
33
|
+
/** What each entity carried before the batch: its row, by eid. */ export type Before = Record<Eid, Bundle>;
|
|
34
|
+
/** The key the reading rides forward on. Not a component: `$`-prefixed keys
|
|
35
|
+
* are `apply()`'s pipeline, never properties, so storage never sees it. */ export declare let BEFORE: string;
|
|
36
|
+
/**
|
|
37
|
+
* Read what the batch's entities — and everything that will die with them —
|
|
38
|
+
* carry right now, and ride it forward on the batch. Registered on the
|
|
39
|
+
* `precondition` phase, so it reads inside the transaction and before a single
|
|
40
|
+
* patch has gone in.
|
|
41
|
+
*/ export declare let before: (vocab: Vocab) => any;
|
|
42
|
+
/**
|
|
43
|
+
* What {@link before} is about to read, declared before it reads it: every
|
|
44
|
+
* entity the batch is about, and — for a delete — everything pointing at what
|
|
45
|
+
* is being deleted, which is what the `doomed()` walk asks. Declared as the
|
|
46
|
+
* plugin's `wants` so @yaks/graph gathers both in one pass.
|
|
47
|
+
*/ export declare let wanting: (vocab: Vocab) => any;
|
|
48
|
+
/**
|
|
49
|
+
* Take the reading back off, so the batch a caller gets back is the batch as
|
|
50
|
+
* applied and nothing else. Done in place, deliberately: `apply()` returns the
|
|
51
|
+
* bundles it committed rather than what the effect phase returns, so a copy
|
|
52
|
+
* made here would be a copy nobody reads. The bundles it edits are the ones
|
|
53
|
+
* `before()` copied, never the caller's own.
|
|
54
|
+
*/ export declare let strip: (bundles: Bundle[]) => Bundle[];
|
|
55
|
+
/**
|
|
56
|
+
* The committed batch, read as what happened. Replays the bundles in order
|
|
57
|
+
* against the reading `before()` took: a component the entity did not carry is
|
|
58
|
+
* `created` (once — a second patch for it in the same batch is a `changed`), a
|
|
59
|
+
* component it did carry is `changed`, a null component or a deleted entity is
|
|
60
|
+
* `removed`, one event per component the casualty carried, each carrying
|
|
61
|
+
* `before`: that component, as the reading found it.
|
|
62
|
+
*/ export declare let events: (bundles: Bundle[]) => Event[];
|
|
63
|
+
//# sourceMappingURL=trace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.d.ts","sources":["../trace.ts"],"names":[],"mappings":"AAwBA,cACE,KAAK,EACL,GAAG,EACH,OAAO,EACP,MAAM,EACN,IAAI,EACJ,GAAG,EACH,MAAM,EACN,EAAE,2BAC2B;AAI/B,cAAc,KAAK,2BAAgC;AACnD,cAAc,UAAU,0BAAyB;AAEjD;2EAC2E,GAC3E,YAAY,OAAO,YAAY,YAAY,YAAY,YAAY;AAEnE;;;;;;;;CAQC,GACD,YAAY;EACV,+EAA+E,GAC/E,KAAK,MAAM;EACX,uCAAuC,GACvC,MAAM;EACN,uDAAuD,GACvD,aAAa;EACb,kBAAkB,GAClB,MAAM;EACN,6DAA6D,GAC7D,QAAQ;EACR,yBAAyB,GACzB,MAAM,MAAM;EACZ,yEAAyE,GACzE,UAAU,MAAM;EAChB,4DAA4D,GAC5D,OAAO;EACP;;kDAEgD,GAChD,SAAS;EACT,6EAA6E,GAC7E,OAAO,OAAO,MAAM,EAAE,OAAO;EAC7B,kEAAkE,GAClE,UAAU;EACV;;;0DAGwD,GACxD,QAAQ;;AAGV,gEAAgE,GAChE,YAAY,SAAS,OAAO,KAAK;AAEjC;yEACyE,GACzE,OAAO,YAAI,eAAkB;AAO7B;;;;;CAKC,GACD,OAAO,YAAI,SACT,OAAO,cAgBR;AAED;;;;;CAKC,GACD,OAAO,YAAI,UAAW,OAAO,cAW5B;AAED;;;;;;CAMC,GACD,OAAO,YAAI,QAAS,SAAS,aAAW,SAGvC;AAED;;;;;;;CAOC,GACD,OAAO,YAAI,SAAU,SAAS,aAAW,QAuDxC"}
|
package/_dist/views.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { View } from "@yaks/inspect";
|
|
2
|
+
// @ts-types="@yaks/ui/lucide"
|
|
3
|
+
import Gauge from "lucide/dist/esm/icons/gauge.mjs";
|
|
4
|
+
/** The page, as the inspector draws it. */ export declare let inspectViews: () => Promise<View[]>;
|
|
5
|
+
/** The page the sidebar offers: the workers, what each is running and how
|
|
6
|
+
* busy it is, and the runs owed behind them. */ export declare let destinations: readonly [{
|
|
7
|
+
readonly key: string;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly icon: string;
|
|
10
|
+
readonly view: string;
|
|
11
|
+
}];
|
|
12
|
+
/** The glyph it wears there. */ export declare let icons: {
|
|
13
|
+
readonly gauge: any;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=views.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"views.d.ts","sources":["../views.ts"],"names":[],"mappings":"AAEA,cAAc,IAAI,6BAAkC;AACpD,8BAA8B;AAC9B,OAAO,6CAAuD;AAE9D,yCAAyC,GACzC,OAAO,YAAI,oBAAmB,QAAQ,QACW;AAEjD;8CAC8C,GAC9C,OAAO,YAAI;aACP;aAAgB;aAAiB;aAAe;KACnD;AAED,8BAA8B,GAC9B,OAAO,YAAI;WAAU;EAAc"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
import { effectDoc } from "./docs.js";
|
|
3
|
+
import { provisionalDoc } from "./provisional.js";
|
|
4
|
+
export { effectDoc, provisionalDoc };
|
|
5
|
+
/** What this package is: its manifest's description, the one place it is
|
|
6
|
+
* written, which a host stamps on each document here. */ export declare let description: string;
|
|
7
|
+
/** Every document this plugin declares — the pool's `effect` rows, which an
|
|
8
|
+
* application loads when effects are to be written down and worked by any
|
|
9
|
+
* process, the `lease` a duty is held under, and the `provisional` mark. A
|
|
10
|
+
* vocabulary that wants only the mark loads `provisionalDoc` instead. */ export declare let docs: VocabDoc[];
|
|
11
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AACtD,SAAS,SAAS,oBAAmB;AACrC,SAAS,cAAc,2BAA0B;AAGjD,SAAS,SAAS,EAAE,cAAc,GAAE;AAEpC;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD;;;uEAGuE,GACvE,OAAO,YAAI,MAAM,WAAwB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The component a worker says itself in, by the name ./vocab.json declares
|
|
2
|
+
* it. */ export declare let WORKER: string;
|
|
3
|
+
/** A run going in a worker, as it lists it. */ export type Listed = {
|
|
4
|
+
/** the run's `effect` row */ run: string;
|
|
5
|
+
handler: string;
|
|
6
|
+
target: string;
|
|
7
|
+
/** when it started, ISO-8601 */ since: string;
|
|
8
|
+
};
|
|
9
|
+
/** What a worker says: the `worker` component. */ export type Worker = {
|
|
10
|
+
at: string;
|
|
11
|
+
running: number;
|
|
12
|
+
runs: Listed[];
|
|
13
|
+
ran: number;
|
|
14
|
+
errors: number;
|
|
15
|
+
history: number[];
|
|
16
|
+
cpu?: number;
|
|
17
|
+
lag?: number;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sources":["../worker.ts"],"names":[],"mappings":"AAuBA;OACO,GACP,OAAO,YAAI,eAAiB;AAY5B,6CAA6C,GAC7C,YAAY;EACV,2BAA2B,GAC3B,KAAK,MAAM;EACX,SAAS,MAAM;EACf,QAAQ,MAAM;EACd,8BAA8B,GAC9B,OAAO,MAAM;;AAGf,gDAAgD,GAChD,YAAY;EACV,IAAI,MAAM;EACV,SAAS,MAAM;EACf,MAAM;EACN,KAAK,MAAM;EACX,QAAQ,MAAM;EACd,SAAS,MAAM;EACf,MAAM,MAAM;EACZ,MAAM,MAAM"}
|
package/_dist/write.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Bundle } from "@yaks/graph";
|
|
2
|
+
/**
|
|
3
|
+
* A write from inside an effect: one new batch through the graph's own
|
|
4
|
+
* `apply()`, post-commit, returning the batch as applied.
|
|
5
|
+
*
|
|
6
|
+
* The application supplies it ({@link Opts.write}), because only it knows which
|
|
7
|
+
* graph and in whose name. It writes as the kernel — the server itself, not the
|
|
8
|
+
* client whose batch triggered the effect — so it must be applied `trusted`: an
|
|
9
|
+
* outcome an effect records (`delivered`, `bounced`) is a server-owned
|
|
10
|
+
* property, and an untrusted apply would refuse the properties the effect
|
|
11
|
+
* exists to write, as in
|
|
12
|
+
* `effects(vocab, { write: (b) => g.apply(b, { trusted: true }) })`.
|
|
13
|
+
*/ export type Write = (bundles: Bundle[]) => Bundle[] | Promise<Bundle[]>;
|
|
14
|
+
/** The key a batch's generation rides under. Not a component: `$`-prefixed
|
|
15
|
+
* keys are `apply()`'s pipeline, never properties, so no storage adapter sees
|
|
16
|
+
* it. */ export declare let ORIGIN: string;
|
|
17
|
+
/** How deep in effect-written batches this one is: `0` for a batch that came
|
|
18
|
+
* from a client, `1` for an effect's own write, one more for each write an
|
|
19
|
+
* effect makes about that one. */ export declare let generation: (bundles: Bundle[]) => number;
|
|
20
|
+
/** The same batch, marked as an effect's own at generation `gen`. Copies, so
|
|
21
|
+
* the handler's own bundles are never edited under it. */ export declare let marked: (bundles: Bundle[], gen: number) => Bundle[];
|
|
22
|
+
/** Take the mark back off, so what a caller gets back is the batch as applied
|
|
23
|
+
* and nothing else. In place, like {@link strip} — the bundles it edits are
|
|
24
|
+
* the copies this package made. */ export declare let unmark: (bundles: Bundle[]) => Bundle[];
|
|
25
|
+
//# sourceMappingURL=write.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write.d.ts","sources":["../write.ts"],"names":[],"mappings":"AAqBA,cAAc,MAAM,2BAAgC;AAEpD;;;;;;;;;;;CAWC,GACD,YAAY,SAAS,SAAS,aAAa,WAAW,QAAQ;AAE9D;;OAEO,GACP,OAAO,YAAI,eAAkB;AAE7B;;gCAEgC,GAChC,OAAO,YAAI,aAAc,SAAS,aAAW,MAAM,CACY;AAE/D;wDACwD,GACxD,OAAO,YAAI,SAAU,SAAS,UAAU,KAAK,MAAM,KAAG,SACC;AAEvD;;iCAEiC,GACjC,OAAO,YAAI,SAAU,SAAS,aAAW,SAGxC"}
|
package/cleanup.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// One owner-invoked cleanup for legacy successful runs. Failed/pending work,
|
|
2
|
+
// other components on a run, and its target's human-readable data stay intact.
|
|
3
|
+
import { line } from "@yaks/query";
|
|
4
|
+
import { comps } from "@yaks/graph/bundle";
|
|
5
|
+
import { PROVENANCE } from "@yaks/graph/provenance";
|
|
6
|
+
import { Stale } from "@yaks/graph/errors";
|
|
7
|
+
import { token } from "@yaks/graph/sha256";
|
|
8
|
+
/** Guarded successful settlement, preserving any other data on the run. */ export let finishedRun = (row)=>{
|
|
9
|
+
let effect = row.effect;
|
|
10
|
+
let guards = Object.fromEntries(Object.entries({
|
|
11
|
+
lease_owner: null,
|
|
12
|
+
lease_token: null,
|
|
13
|
+
attempts: null,
|
|
14
|
+
...effect
|
|
15
|
+
}).map(([p, v])=>[
|
|
16
|
+
p,
|
|
17
|
+
token(v ?? null)
|
|
18
|
+
]));
|
|
19
|
+
let other = comps(row).some(([name])=>![
|
|
20
|
+
'effect',
|
|
21
|
+
...PROVENANCE
|
|
22
|
+
].includes(name));
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
entity: row.entity,
|
|
26
|
+
...other ? {
|
|
27
|
+
effect: null
|
|
28
|
+
} : {
|
|
29
|
+
$delete: true
|
|
30
|
+
},
|
|
31
|
+
$was: {
|
|
32
|
+
effect: guards
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
];
|
|
36
|
+
};
|
|
37
|
+
export let doneRun = (row)=>row.effect?.state == 'done' ? finishedRun(row) : [];
|
|
38
|
+
export let cleanupDone = async (g, batch = 100, limit = Infinity)=>{
|
|
39
|
+
if (!Number.isInteger(batch) || batch < 1) {
|
|
40
|
+
throw Error('positive cleanup batch required');
|
|
41
|
+
}
|
|
42
|
+
let removed = 0;
|
|
43
|
+
while(removed < limit){
|
|
44
|
+
let rows = await g.read(line`.effect.state=done&.limit=${Math.min(batch, limit - removed)}&*`);
|
|
45
|
+
if (!rows.length) return removed;
|
|
46
|
+
try {
|
|
47
|
+
await g.apply(rows.flatMap(doneRun), {
|
|
48
|
+
trusted: true
|
|
49
|
+
});
|
|
50
|
+
removed += rows.length;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if (!(e instanceof Stale)) throw e;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return removed;
|
|
56
|
+
};
|
|
57
|
+
/** Owner-invoked settlement of pending errands that the declaration no longer
|
|
58
|
+
* owes. The predicate is conservative; active claims and failures stay intact.
|
|
59
|
+
* It completes through the same guarded graph writes as successful-run cleanup. */ export let settleStale = async (g, handler, needed)=>{
|
|
60
|
+
let settled = 0;
|
|
61
|
+
for (let row of (await g.read(line`.effect.handler=${handler} .effect.state=pending *`))){
|
|
62
|
+
let effect = row.effect;
|
|
63
|
+
if (effect.lease_owner || await needed(effect)) continue;
|
|
64
|
+
try {
|
|
65
|
+
await g.apply(finishedRun(row), {
|
|
66
|
+
trusted: true
|
|
67
|
+
});
|
|
68
|
+
settled++;
|
|
69
|
+
} catch (e) {
|
|
70
|
+
if (!(e instanceof Stale)) throw e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return settled;
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=cleanup.js.map
|
package/cleanup.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanup.js","sources":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AAC,UAAU;QAAE,SAAS;MAAK;MACrD,WAAW,KAAK,MAAM;IACxB,EAAE,OAAO,GAAG;MACV,IAAI,CAAC,CAAC,aAAa,KAAK,GAAG,MAAM;IACnC;EACF;EACA,OAAO;AACT,EAAC;AAED;;iFAEiF,GACjF,OAAO,IAAI,cAAc,OACvB,GACA,SACA;EAEA,IAAI,UAAU;EACd,KACE,IAAI,OAAO,CAAA,MAAM,EAAE,IAAI,CACrB,IAAI,CAAC,gBAAgB,EAAE,QAAQ,wBAAwB,CAAC,CAC1D,EACA;IACA,IAAI,SAAS,IAAI,MAAM;IACvB,IAAI,OAAO,WAAW,IAAI,MAAM,OAAO,SAAS;IAChD,IAAI;MACF,MAAM,EAAE,KAAK,CAAC,YAAY,MAAM;QAAE,SAAS;MAAK;MAChD;IACF,EAAE,OAAO,GAAG;MACV,IAAI,CAAC,CAAC,aAAa,KAAK,GAAG,MAAM;IACnC;EACF;EACA,OAAO;AACT,EAAC"}
|
package/cleanup.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// One owner-invoked cleanup for legacy successful runs. Failed/pending work,
|
|
2
|
+
// other components on a run, and its target's human-readable data stay intact.
|
|
3
|
+
import { line } from '@yaks/query'
|
|
4
|
+
import type { Access, Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
|
|
5
|
+
import { comps } from '@yaks/graph/bundle'
|
|
6
|
+
import { PROVENANCE } from '@yaks/graph/provenance'
|
|
7
|
+
import { Stale } from '@yaks/graph/errors'
|
|
8
|
+
import { token } from '@yaks/graph/sha256'
|
|
9
|
+
|
|
10
|
+
/** Guarded successful settlement, preserving any other data on the run. */
|
|
11
|
+
export let finishedRun = (row: Bundle): Bundle[] => {
|
|
12
|
+
let effect = row.effect as Comp
|
|
13
|
+
let guards = Object.fromEntries(
|
|
14
|
+
Object.entries({
|
|
15
|
+
lease_owner: null,
|
|
16
|
+
lease_token: null,
|
|
17
|
+
attempts: null,
|
|
18
|
+
...effect,
|
|
19
|
+
})
|
|
20
|
+
.map(([p, v]) => [p, token(v ?? null)]),
|
|
21
|
+
)
|
|
22
|
+
let other = comps(row).some(([name]) =>
|
|
23
|
+
!['effect', ...PROVENANCE].includes(name)
|
|
24
|
+
)
|
|
25
|
+
return [{
|
|
26
|
+
entity: row.entity,
|
|
27
|
+
...(other ? { effect: null } : { $delete: true }),
|
|
28
|
+
$was: { effect: guards },
|
|
29
|
+
}]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export let doneRun = (row: Bundle): Bundle[] =>
|
|
33
|
+
(row.effect as Comp | undefined)?.state == 'done' ? finishedRun(row) : []
|
|
34
|
+
|
|
35
|
+
export let cleanupDone = async (
|
|
36
|
+
g: Access,
|
|
37
|
+
batch = 100,
|
|
38
|
+
limit = Infinity,
|
|
39
|
+
): Promise<number> => {
|
|
40
|
+
if (!Number.isInteger(batch) || batch < 1) {
|
|
41
|
+
throw Error('positive cleanup batch required')
|
|
42
|
+
}
|
|
43
|
+
let removed = 0
|
|
44
|
+
while (removed < limit) {
|
|
45
|
+
let rows = await g.read(
|
|
46
|
+
line`.effect.state=done&.limit=${Math.min(batch, limit - removed)}&*`,
|
|
47
|
+
)
|
|
48
|
+
if (!rows.length) return removed
|
|
49
|
+
try {
|
|
50
|
+
await g.apply(rows.flatMap(doneRun), { trusted: true })
|
|
51
|
+
removed += rows.length
|
|
52
|
+
} catch (e) {
|
|
53
|
+
if (!(e instanceof Stale)) throw e
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return removed
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Owner-invoked settlement of pending errands that the declaration no longer
|
|
60
|
+
* owes. The predicate is conservative; active claims and failures stay intact.
|
|
61
|
+
* It completes through the same guarded graph writes as successful-run cleanup. */
|
|
62
|
+
export let settleStale = async (
|
|
63
|
+
g: Access,
|
|
64
|
+
handler: string,
|
|
65
|
+
needed: (effect: Comp) => boolean | Promise<boolean>,
|
|
66
|
+
): Promise<number> => {
|
|
67
|
+
let settled = 0
|
|
68
|
+
for (
|
|
69
|
+
let row of await g.read(
|
|
70
|
+
line`.effect.handler=${handler} .effect.state=pending *`,
|
|
71
|
+
)
|
|
72
|
+
) {
|
|
73
|
+
let effect = row.effect as Comp
|
|
74
|
+
if (effect.lease_owner || await needed(effect)) continue
|
|
75
|
+
try {
|
|
76
|
+
await g.apply(finishedRun(row), { trusted: true })
|
|
77
|
+
settled++
|
|
78
|
+
} catch (e) {
|
|
79
|
+
if (!(e instanceof Stale)) throw e
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return settled
|
|
83
|
+
}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"publish":{"exclude":["**/*_test.ts","./testing.ts"]},"name":"@yaks/effects","version":"0.2.36","license":"Apache-2.0","description":"Runs application code after graph changes commit, with isolated failures, an optional pool for persisted runs, and leases for work held by one process.","exports":{".":"./mod.ts","./invocation":"./invocation.ts","./vocab":"./vocab.ts","./tools":"./tools.ts","./lease":"./lease.ts","./outage":"./outage.ts","./views":"./views.ts"},"imports":{"@yaks/effects":"./mod.ts","@yaks/effects/invocation":"./invocation.ts","@yaks/effects/vocab":"./vocab.ts","@yaks/effects/tools":"./tools.ts","@yaks/effects/lease":"./lease.ts","@yaks/effects/outage":"./outage.ts","@yaks/effects/views":"./views.ts"}}
|
package/docs.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Effect, lease and provisional declarations, independent of their workers.
|
|
2
|
+
import doc from './vocab.json' with {
|
|
3
|
+
type: 'json'
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* This package's components, to load beside your own when effects should be
|
|
7
|
+
* written down and worked by a pool: `loadVocab([effectDoc, ...mine])`. Four
|
|
8
|
+
* components, one document — `effect`, a run owed, whose every property is
|
|
9
|
+
* server-owned; `lease` (./lease.ts), a duty held by one process at a time;
|
|
10
|
+
* `worker` (./worker.ts), what a process working the pool says about itself;
|
|
11
|
+
* and `provisional` (./provisional.ts), an entity whose step is unfinished.
|
|
12
|
+
*/ export let effectDoc = doc;
|
|
13
|
+
//# sourceMappingURL=docs.js.map
|
package/docs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docs.js","sources":["./docs.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAE5E,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AAEpD;;;;;;;CAOC,GACD,OAAO,IAAI,YAAsB,IAAG"}
|
package/docs.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Effect, lease and provisional declarations, independent of their workers.
|
|
2
|
+
import type { VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
3
|
+
import doc from './vocab.json' with { type: 'json' }
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This package's components, to load beside your own when effects should be
|
|
7
|
+
* written down and worked by a pool: `loadVocab([effectDoc, ...mine])`. Four
|
|
8
|
+
* components, one document — `effect`, a run owed, whose every property is
|
|
9
|
+
* server-owned; `lease` (./lease.ts), a duty held by one process at a time;
|
|
10
|
+
* `worker` (./worker.ts), what a process working the pool says about itself;
|
|
11
|
+
* and `provisional` (./provisional.ts), an entity whose step is unfinished.
|
|
12
|
+
*/
|
|
13
|
+
export let effectDoc: VocabDoc = doc
|
package/enqueue.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Explicitly owed work uses the same durable rows as declared graph triggers.
|
|
2
|
+
import { mint } from "@yaks/graph";
|
|
3
|
+
import { Refused } from "@yaks/graph/errors";
|
|
4
|
+
import { token } from "@yaks/graph/sha256";
|
|
5
|
+
import { effectsIn } from "@yaks/vocab";
|
|
6
|
+
/** Owe one fresh invocation independently of a component transition. */ export let enqueue = (g, handler, target)=>{
|
|
7
|
+
if (!g.vocab.comp('effect')) throw new Refused('No durable effect pool');
|
|
8
|
+
if (!effectsIn(g.vocab.docs).some((effect)=>effect.name == handler)) {
|
|
9
|
+
throw new Refused('No declared effect: ' + handler);
|
|
10
|
+
}
|
|
11
|
+
return g.apply([
|
|
12
|
+
effectInvocation(handler, target)
|
|
13
|
+
], {
|
|
14
|
+
trusted: true
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
/** The durable invocation patch, for a trusted writer already in a batch,
|
|
18
|
+
* due at `next` (an ISO time) or at once. */ export let effectInvocation = (handler, target, next)=>({
|
|
19
|
+
entity: {
|
|
20
|
+
eid: mint()
|
|
21
|
+
},
|
|
22
|
+
effect: {
|
|
23
|
+
handler,
|
|
24
|
+
target,
|
|
25
|
+
kind: 'matched',
|
|
26
|
+
state: 'pending',
|
|
27
|
+
attempts: 0,
|
|
28
|
+
generation: 0,
|
|
29
|
+
at: new Date().toISOString(),
|
|
30
|
+
...next ? {
|
|
31
|
+
next
|
|
32
|
+
} : {}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
// Pending with a full allowance, as a run no one has started: the pool claims
|
|
36
|
+
// it, counts attempts and backs off exactly as it does a new one.
|
|
37
|
+
let RETRIED = {
|
|
38
|
+
state: 'pending',
|
|
39
|
+
attempts: 0,
|
|
40
|
+
next: null,
|
|
41
|
+
error: null,
|
|
42
|
+
waiting: null,
|
|
43
|
+
lease_owner: null,
|
|
44
|
+
lease_token: null,
|
|
45
|
+
lease_expiry: null
|
|
46
|
+
};
|
|
47
|
+
/** The patch that puts a run back to pending with a fresh allowance, guarded
|
|
48
|
+
* on the state it was read in (`was`, failed unless said), so a run a worker
|
|
49
|
+
* has since touched is left alone. */ export let effectRetry = (run, was = 'failed')=>({
|
|
50
|
+
entity: {
|
|
51
|
+
eid: run
|
|
52
|
+
},
|
|
53
|
+
effect: RETRIED,
|
|
54
|
+
$was: {
|
|
55
|
+
effect: {
|
|
56
|
+
state: token(was)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
// The transactions a hook owed a run in (`owe`), whose commit wakes the pool
|
|
61
|
+
// as a run written in the batch itself does (./registry.ts).
|
|
62
|
+
export let owing = new WeakSet();
|
|
63
|
+
/** Owe one fresh invocation from a hook inside a write's own transaction (a
|
|
64
|
+
* `commit` hook's `tx`), so the run commits or rolls back with the write that
|
|
65
|
+
* owes it, due at `next` or at once. */ export let owe = (tx, context, handler, target, next)=>{
|
|
66
|
+
if (context?.transaction) owing.add(context.transaction);
|
|
67
|
+
return tx.patch([
|
|
68
|
+
effectInvocation(handler, target, next)
|
|
69
|
+
]);
|
|
70
|
+
};
|
|
71
|
+
//# sourceMappingURL=enqueue.js.map
|
package/enqueue.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enqueue.js","sources":["./enqueue.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,SAKE,IAAI,2BAEyB;AAE/B,SAAS,OAAO,kCAAuC;AACvD,SAAS,KAAK,kCAAuC;AACrD,SAAS,SAAS,2BAAgC;AAElD,sEAAsE,GACtE,OAAO,IAAI,UAAU,CACnB,GACA,SACA;EAEA,IAAI,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,WAAW,MAAM,IAAI,QAAQ;EAC/C,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,SAAW,OAAO,IAAI,IAAI,UAAU;IACrE,MAAM,IAAI,QAAQ,yBAAyB;EAC7C;EACA,OAAO,EAAE,KAAK,CAAC;IAAC,iBAAiB,SAAS;GAAQ,EAAE;IAAE,SAAS;EAAK;AACtE,EAAC;AAED;2CAC2C,GAC3C,OAAO,IAAI,mBAAmB,CAC5B,SACA,QACA,OACW,CAAC;IACZ,QAAQ;MAAE,KAAK;IAAO;IACtB,QAAQ;MACN;MACA;MACA,MAAM;MACN,OAAO;MACP,UAAU;MACV,YAAY;MACZ,IAAI,IAAI,OAAO,WAAW;MAC1B,GAAG,OAAO;QAAE;MAAK,IAAI,CAAC,CAAC;IACzB;EACF,CAAC,EAAC;AAEF,8EAA8E;AAC9E,kEAAkE;AAClE,IAAI,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