@astrale-os/kernel-client 1.0.0-beta.26 → 1.0.0-beta.28
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/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +16 -3
- package/dist/client/client.js +36 -15
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +1 -1
- package/dist/client/routing-control.d.ts +11 -15
- package/dist/client/routing-control.js +21 -29
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/query-input.js +2 -1
- package/dist/errors/response-too-large-error.d.ts +11 -0
- package/dist/errors/response-too-large-error.js +15 -0
- package/dist/errors/stale-route-error.d.ts +1 -1
- package/dist/errors/stale-route-error.js +1 -1
- package/dist/graph/page-contract.d.ts +6 -1
- package/dist/graph/query.js +9 -3
- package/dist/session/admission.js +4 -2
- package/dist/session/credential/credentials.js +32 -28
- package/dist/session/discovery/bundle.d.ts +9 -4
- package/dist/session/discovery/bundle.js +28 -35
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +7 -10
- package/dist/session/discovery/release.js +18 -28
- package/dist/session/discovery/transport.d.ts +35 -3
- package/dist/session/discovery/transport.js +40 -1
- package/dist/session/exchange/exchange.d.ts +3 -2
- package/dist/session/exchange/exchange.js +10 -7
- package/dist/session/exchange/index.d.ts +1 -1
- package/dist/session/exchange/index.js +1 -1
- package/dist/session/exchange/presentation.js +28 -10
- package/dist/session/exchange/source.d.ts +7 -7
- package/dist/session/exchange/source.js +26 -27
- package/dist/session/routing/auth.d.ts +9 -3
- package/dist/session/routing/auth.js +4 -3
- package/dist/session/routing/cache.d.ts +12 -5
- package/dist/session/routing/cache.js +56 -48
- package/dist/session/routing/dispatch.d.ts +2 -1
- package/dist/session/routing/dispatch.js +83 -61
- package/dist/session/routing/index.d.ts +1 -1
- package/dist/session/routing/recovery.d.ts +3 -6
- package/dist/session/routing/recovery.js +3 -14
- package/dist/session/routing/store.js +4 -5
- package/dist/store/memory.d.ts +1 -3
- package/dist/store/memory.js +61 -112
- package/dist/store/shape.d.ts +33 -12
- package/dist/store/shape.js +125 -16
- package/dist/store/store.js +1 -1
- package/dist/transport/auto/decision.d.ts +26 -14
- package/dist/transport/auto/decision.js +35 -32
- package/dist/transport/auto/fallback.d.ts +1 -1
- package/dist/transport/auto/fallback.js +5 -10
- package/dist/transport/auto/transport.d.ts +0 -2
- package/dist/transport/auto/transport.js +24 -32
- package/dist/transport/failure.d.ts +28 -1
- package/dist/transport/failure.js +63 -1
- package/dist/transport/http/admission.js +2 -2
- package/dist/transport/http/binary.js +3 -3
- package/dist/transport/http/byte-stream.d.ts +2 -1
- package/dist/transport/http/byte-stream.js +12 -8
- package/dist/transport/http/response.d.ts +3 -0
- package/dist/transport/http/response.js +7 -3
- package/dist/transport/http/stream.d.ts +8 -13
- package/dist/transport/http/stream.js +34 -81
- package/dist/transport/http/transport.js +11 -22
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/unary.d.ts +3 -2
- package/dist/transport/unary.js +3 -3
- package/dist/transport/websocket/connection.d.ts +7 -3
- package/dist/transport/websocket/connection.js +102 -85
- package/dist/transport/websocket/exchange.d.ts +10 -19
- package/dist/transport/websocket/exchange.js +72 -153
- package/dist/transport/websocket/transport.js +11 -19
- package/package.json +5 -5
package/dist/store/memory.js
CHANGED
|
@@ -22,7 +22,7 @@ export class GraphMemory {
|
|
|
22
22
|
const existing = this.shapes.get(key);
|
|
23
23
|
const now = this.options.now();
|
|
24
24
|
if (existing !== undefined) {
|
|
25
|
-
|
|
25
|
+
existing.touch(now);
|
|
26
26
|
return existing;
|
|
27
27
|
}
|
|
28
28
|
this.releaseExpired(now);
|
|
@@ -31,87 +31,83 @@ export class GraphMemory {
|
|
|
31
31
|
if (this.shapes.size >= this.options.maximumShapes) {
|
|
32
32
|
throw new ClientError('GraphStore shape capacity is exhausted by observed or in-flight queries.');
|
|
33
33
|
}
|
|
34
|
-
const shape = new QueryShape(key, ast, page, expected, now);
|
|
35
|
-
shape.releasedAt = now;
|
|
34
|
+
const shape = new QueryShape(key, ast, page, expected, now, (error) => this.report(error));
|
|
36
35
|
this.shapes.set(key, shape);
|
|
37
36
|
return shape;
|
|
38
37
|
}
|
|
39
38
|
get(key) {
|
|
40
39
|
const shape = this.shapes.get(key);
|
|
41
40
|
if (shape !== undefined)
|
|
42
|
-
|
|
41
|
+
shape.touch(this.options.now());
|
|
43
42
|
return shape;
|
|
44
43
|
}
|
|
45
44
|
async read(shape, options) {
|
|
46
45
|
this.options.requireOpen();
|
|
47
46
|
requireReadLive(options.signal, this.options.lifecycle);
|
|
48
|
-
const
|
|
47
|
+
const prior = shape.answer;
|
|
48
|
+
const reusable = prior !== undefined &&
|
|
49
49
|
shape.state === 'ready' &&
|
|
50
|
-
|
|
51
|
-
this.options.now() - shape.refreshedAt < this.options.ttlMs;
|
|
50
|
+
this.options.now() - prior.refreshedAt < this.options.ttlMs;
|
|
52
51
|
if (options.freshness !== 'refresh' && reusable)
|
|
53
|
-
return
|
|
52
|
+
return prior.response;
|
|
54
53
|
if (shape.inflight !== undefined) {
|
|
55
54
|
return observeRead(shape.inflight, options, this.options.lifecycle);
|
|
56
55
|
}
|
|
57
|
-
const prior = shape.response;
|
|
58
56
|
const invalidation = shape.invalidation;
|
|
59
|
-
|
|
57
|
+
shape.publish(prior === undefined ? 'loading' : this.overlay.hasAny() ? 'optimistic' : 'refreshing');
|
|
60
58
|
const inflight = this.schedule(async () => {
|
|
61
59
|
const result = await this.options.graph.query(shape.ast, queryOptions(shape.page, this.options.lifecycle, shape.expected));
|
|
62
60
|
this.options.requireOpen();
|
|
63
61
|
if (shape.invalidation !== invalidation) {
|
|
64
|
-
|
|
62
|
+
shape.publish(prior === undefined ? 'cold' : this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
65
63
|
return result;
|
|
66
64
|
}
|
|
67
65
|
// An uncertified read may hold facts the active schema refutes: it stays raw on its own
|
|
68
66
|
// shape and never enters, or is derived from, the shared entity cache.
|
|
69
67
|
if (result.certification === 'uncertified') {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
const answer = {
|
|
69
|
+
confirmed: result,
|
|
70
|
+
references: NO_REFERENCES,
|
|
71
|
+
response: result,
|
|
72
|
+
refreshedAt: this.options.now(),
|
|
73
|
+
};
|
|
74
|
+
shape.settle(answer, 'ready');
|
|
75
|
+
return answer.response;
|
|
77
76
|
}
|
|
78
77
|
const integration = this.entities.integrate(result.result);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
78
|
+
const answer = {
|
|
79
|
+
confirmed: result,
|
|
80
|
+
references: integration.references,
|
|
81
|
+
response: this.materialize(result, integration.references),
|
|
82
|
+
refreshedAt: this.options.now(),
|
|
83
|
+
};
|
|
84
|
+
shape.settle(answer, this.overlay.hasAny() ? 'optimistic' : 'ready');
|
|
85
85
|
for (const other of this.shapes.values()) {
|
|
86
|
-
if (other === shape
|
|
86
|
+
if (other === shape)
|
|
87
87
|
continue;
|
|
88
88
|
if (other.referencesAny(integration.changedNodes, integration.changedEdges)) {
|
|
89
|
-
|
|
90
|
-
this.publish(other, other.state);
|
|
89
|
+
this.rematerialize(other, other.state);
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
|
-
return
|
|
92
|
+
return answer.response;
|
|
94
93
|
})
|
|
95
94
|
.catch((error) => {
|
|
96
95
|
if (prior === undefined)
|
|
97
|
-
|
|
98
|
-
else
|
|
99
|
-
shape.
|
|
100
|
-
this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
101
|
-
}
|
|
96
|
+
shape.publish('failed', error);
|
|
97
|
+
else
|
|
98
|
+
shape.show(prior, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
102
99
|
throw error;
|
|
103
100
|
})
|
|
104
101
|
.finally(() => {
|
|
105
|
-
if (shape.inflight
|
|
102
|
+
if (!shape.finish(inflight))
|
|
106
103
|
return;
|
|
107
|
-
shape.inflight = undefined;
|
|
108
104
|
if (shape.invalidation !== invalidation &&
|
|
109
105
|
shape.observed &&
|
|
110
106
|
!this.options.lifecycle.aborted) {
|
|
111
107
|
void this.read(shape, { page: shape.page }).catch((error) => this.report(error));
|
|
112
108
|
}
|
|
113
109
|
});
|
|
114
|
-
shape.inflight
|
|
110
|
+
shape.begin(inflight);
|
|
115
111
|
return observeRead(inflight, options, this.options.lifecycle);
|
|
116
112
|
}
|
|
117
113
|
snapshot(shape) {
|
|
@@ -119,22 +115,20 @@ export class GraphMemory {
|
|
|
119
115
|
}
|
|
120
116
|
subscribe(shape, listener) {
|
|
121
117
|
this.options.requireOpen();
|
|
122
|
-
shape.
|
|
123
|
-
shape.accessedAt = this.options.now();
|
|
124
|
-
shape.releasedAt = undefined;
|
|
118
|
+
shape.listen(listener, this.options.now());
|
|
125
119
|
return () => {
|
|
126
|
-
shape.
|
|
120
|
+
shape.unlisten(listener);
|
|
127
121
|
if (!shape.observed)
|
|
128
|
-
shape.
|
|
122
|
+
shape.release(this.options.now());
|
|
129
123
|
};
|
|
130
124
|
}
|
|
131
125
|
invalidate(shape) {
|
|
132
126
|
this.options.requireOpen();
|
|
133
127
|
for (const candidate of shape === undefined ? this.shapes.values() : [shape]) {
|
|
134
|
-
candidate.
|
|
135
|
-
if (candidate.
|
|
128
|
+
candidate.invalidate();
|
|
129
|
+
if (candidate.answer === undefined)
|
|
136
130
|
continue;
|
|
137
|
-
|
|
131
|
+
candidate.publish(this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
138
132
|
if (candidate.observed && candidate.inflight === undefined) {
|
|
139
133
|
void this.read(candidate, { page: candidate.page }).catch((error) => this.report(error));
|
|
140
134
|
}
|
|
@@ -142,27 +136,31 @@ export class GraphMemory {
|
|
|
142
136
|
}
|
|
143
137
|
optimistic() {
|
|
144
138
|
for (const shape of this.shapes.values()) {
|
|
145
|
-
if (shape.
|
|
139
|
+
if (shape.answer?.confirmed.certification === 'uncertified')
|
|
146
140
|
continue;
|
|
147
|
-
|
|
148
|
-
this.publish(shape, 'optimistic');
|
|
141
|
+
this.rematerialize(shape, 'optimistic');
|
|
149
142
|
}
|
|
150
143
|
}
|
|
151
144
|
settleOptimistic() {
|
|
152
145
|
for (const shape of this.shapes.values()) {
|
|
153
|
-
|
|
154
|
-
continue;
|
|
155
|
-
shape.response = this.materialize(shape.confirmed, shape.references);
|
|
156
|
-
this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
146
|
+
this.rematerialize(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
157
147
|
}
|
|
158
148
|
}
|
|
159
149
|
sweep() {
|
|
160
150
|
this.options.requireOpen();
|
|
161
151
|
this.releaseExpired(this.options.now());
|
|
162
152
|
}
|
|
153
|
+
rematerialize(shape, state) {
|
|
154
|
+
const answer = shape.answer;
|
|
155
|
+
if (answer === undefined)
|
|
156
|
+
return;
|
|
157
|
+
const response = this.materialize(answer.confirmed, answer.references);
|
|
158
|
+
shape.show({ ...answer, response }, state);
|
|
159
|
+
}
|
|
163
160
|
releaseExpired(now) {
|
|
164
161
|
for (const [key, shape] of this.shapes) {
|
|
165
|
-
|
|
162
|
+
const idleSince = shape.idleSince();
|
|
163
|
+
if (idleSince !== undefined && now - idleSince >= this.options.ttlMs) {
|
|
166
164
|
this.shapes.delete(key);
|
|
167
165
|
}
|
|
168
166
|
}
|
|
@@ -171,7 +169,7 @@ export class GraphMemory {
|
|
|
171
169
|
evictLeastRecentlyUsed() {
|
|
172
170
|
let oldest;
|
|
173
171
|
for (const shape of this.shapes.values()) {
|
|
174
|
-
if (
|
|
172
|
+
if (shape.idleSince() === undefined)
|
|
175
173
|
continue;
|
|
176
174
|
if (oldest === undefined || shape.accessedAt < oldest.accessedAt)
|
|
177
175
|
oldest = shape;
|
|
@@ -181,28 +179,18 @@ export class GraphMemory {
|
|
|
181
179
|
this.sweepEntities();
|
|
182
180
|
}
|
|
183
181
|
}
|
|
184
|
-
releasable(shape) {
|
|
185
|
-
return !shape.observed && shape.inflight === undefined && shape.releasedAt !== undefined;
|
|
186
|
-
}
|
|
187
|
-
touch(shape, now) {
|
|
188
|
-
shape.accessedAt = now;
|
|
189
|
-
if (!shape.observed)
|
|
190
|
-
shape.releasedAt = now;
|
|
191
|
-
}
|
|
192
182
|
sweepEntities() {
|
|
193
183
|
const nodes = new Set();
|
|
194
184
|
const edges = new Set();
|
|
195
185
|
for (const shape of this.shapes.values()) {
|
|
196
|
-
shape.references
|
|
197
|
-
shape.references
|
|
186
|
+
shape.answer?.references.nodes.forEach((id) => nodes.add(id));
|
|
187
|
+
shape.answer?.references.edges.forEach((key) => edges.add(key));
|
|
198
188
|
}
|
|
199
189
|
this.entities.sweep(nodes, edges);
|
|
200
190
|
}
|
|
201
191
|
close(error) {
|
|
202
|
-
for (const shape of this.shapes.values())
|
|
203
|
-
shape.
|
|
204
|
-
shape.inflight?.catch(() => undefined);
|
|
205
|
-
}
|
|
192
|
+
for (const shape of this.shapes.values())
|
|
193
|
+
shape.close();
|
|
206
194
|
this.shapes.clear();
|
|
207
195
|
this.entities.clear();
|
|
208
196
|
this.overlay.clear();
|
|
@@ -289,21 +277,6 @@ export class GraphMemory {
|
|
|
289
277
|
? Object.freeze({ kind: 'graph', graph, selection })
|
|
290
278
|
: Object.freeze({ kind: 'graph', graph, selection });
|
|
291
279
|
}
|
|
292
|
-
publish(shape, state, error) {
|
|
293
|
-
const next = snapshotFor(state, shape.response, error);
|
|
294
|
-
if (sameSnapshot(shape.snapshot, next))
|
|
295
|
-
return;
|
|
296
|
-
shape.state = state;
|
|
297
|
-
shape.snapshot = next;
|
|
298
|
-
for (const listener of Array.from(shape.listeners)) {
|
|
299
|
-
try {
|
|
300
|
-
listener();
|
|
301
|
-
}
|
|
302
|
-
catch (error) {
|
|
303
|
-
this.report(error);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
280
|
schedule(task) {
|
|
308
281
|
return new Promise((resolve, reject) => {
|
|
309
282
|
const run = () => {
|
|
@@ -320,8 +293,11 @@ export class GraphMemory {
|
|
|
320
293
|
});
|
|
321
294
|
}
|
|
322
295
|
drain() {
|
|
323
|
-
while (this.active < this.options.concurrency
|
|
324
|
-
this.pending.shift()
|
|
296
|
+
while (this.active < this.options.concurrency) {
|
|
297
|
+
const task = this.pending.shift();
|
|
298
|
+
if (task === undefined)
|
|
299
|
+
return;
|
|
300
|
+
task.run();
|
|
325
301
|
}
|
|
326
302
|
}
|
|
327
303
|
report(error) {
|
|
@@ -368,7 +344,7 @@ function observeRead(pending, options, lifecycle) {
|
|
|
368
344
|
}
|
|
369
345
|
return new Promise((resolve, reject) => {
|
|
370
346
|
let timeout;
|
|
371
|
-
const callerCancelled = () => settleReject(cancellation(options.signal
|
|
347
|
+
const callerCancelled = () => settleReject(cancellation(options.signal?.reason));
|
|
372
348
|
const storeClosed = () => settleReject(cancellation(lifecycle.reason));
|
|
373
349
|
let settled = false;
|
|
374
350
|
const cleanup = () => {
|
|
@@ -410,30 +386,3 @@ function cancellation(reason) {
|
|
|
410
386
|
? reason
|
|
411
387
|
: new ClientError('Store read was cancelled.', { cause: reason });
|
|
412
388
|
}
|
|
413
|
-
function snapshotFor(state, response, error) {
|
|
414
|
-
switch (state) {
|
|
415
|
-
case 'cold':
|
|
416
|
-
return Object.freeze({ state });
|
|
417
|
-
case 'loading':
|
|
418
|
-
return Object.freeze({ state });
|
|
419
|
-
case 'failed':
|
|
420
|
-
return Object.freeze({ state, error, ...(response === undefined ? {} : { response }) });
|
|
421
|
-
case 'ready':
|
|
422
|
-
case 'stale':
|
|
423
|
-
case 'refreshing':
|
|
424
|
-
case 'optimistic':
|
|
425
|
-
if (response === undefined)
|
|
426
|
-
throw new TypeError(`Store ${state} snapshot requires a response.`);
|
|
427
|
-
return Object.freeze({ state, response });
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
function sameSnapshot(left, right) {
|
|
431
|
-
if (left.state !== right.state)
|
|
432
|
-
return false;
|
|
433
|
-
if ('response' in left || 'response' in right) {
|
|
434
|
-
if (!('response' in left) || !('response' in right) || left.response !== right.response) {
|
|
435
|
-
return false;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
return left.state !== 'failed' || right.state !== 'failed' || left.error === right.error;
|
|
439
|
-
}
|
package/dist/store/shape.d.ts
CHANGED
|
@@ -5,25 +5,46 @@ import type { QueryPageRequest } from '../graph/index.js';
|
|
|
5
5
|
import type { FactReferences } from './entity-index.js';
|
|
6
6
|
import type { StoreQueryResponse, StoreSnapshot } from './store.js';
|
|
7
7
|
type ShapeState = StoreSnapshot['state'];
|
|
8
|
+
/** A confirmed server response with the facts it references and the response displayed from it. */
|
|
9
|
+
export interface QueryAnswer {
|
|
10
|
+
readonly confirmed: StoreQueryResponse;
|
|
11
|
+
readonly references: FactReferences;
|
|
12
|
+
readonly response: StoreQueryResponse;
|
|
13
|
+
readonly refreshedAt: number;
|
|
14
|
+
}
|
|
15
|
+
/** One cached query: its answer, snapshot, observers, in-flight read and retention clock. */
|
|
8
16
|
export declare class QueryShape {
|
|
17
|
+
#private;
|
|
9
18
|
readonly key: string;
|
|
10
19
|
readonly ast: QueryAST;
|
|
11
20
|
readonly page: QueryPageRequest;
|
|
12
21
|
readonly expected: readonly DomainDependency[] | undefined;
|
|
13
|
-
readonly
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
releasedAt?: number;
|
|
21
|
-
accessedAt: number;
|
|
22
|
-
refreshedAt?: number;
|
|
23
|
-
snapshot: StoreSnapshot;
|
|
24
|
-
constructor(key: string, ast: QueryAST, page: QueryPageRequest, expected: readonly DomainDependency[] | undefined, now: number);
|
|
22
|
+
constructor(key: string, ast: QueryAST, page: QueryPageRequest, expected: readonly DomainDependency[] | undefined, now: number, report: (error: unknown) => void);
|
|
23
|
+
get snapshot(): StoreSnapshot;
|
|
24
|
+
get state(): ShapeState;
|
|
25
|
+
get answer(): QueryAnswer | undefined;
|
|
26
|
+
get inflight(): Promise<StoreQueryResponse> | undefined;
|
|
27
|
+
get invalidation(): number;
|
|
28
|
+
get accessedAt(): number;
|
|
25
29
|
get observed(): boolean;
|
|
30
|
+
/** When the shape became unobserved and idle, or undefined while it is observed or reading. */
|
|
31
|
+
idleSince(): number | undefined;
|
|
26
32
|
referencesAny(nodes: ReadonlySet<NodeId>, edges: ReadonlySet<string>): boolean;
|
|
33
|
+
touch(now: number): void;
|
|
34
|
+
listen(listener: () => void, now: number): void;
|
|
35
|
+
/** Removes a listener; the caller releases the shape once it is no longer observed. */
|
|
36
|
+
unlisten(listener: () => void): void;
|
|
37
|
+
release(now: number): void;
|
|
38
|
+
begin(read: Promise<StoreQueryResponse>): void;
|
|
39
|
+
/** Ends `read` if it is still the shape's read and reports whether it was. */
|
|
40
|
+
finish(read: Promise<StoreQueryResponse>): boolean;
|
|
41
|
+
invalidate(): void;
|
|
42
|
+
/** Records a newly confirmed answer, then publishes `state`. */
|
|
43
|
+
settle(answer: QueryAnswer, state: ShapeState): void;
|
|
44
|
+
/** Replaces the displayed answer without a new server confirmation, then publishes `state`. */
|
|
45
|
+
show(answer: QueryAnswer, state: ShapeState): void;
|
|
46
|
+
publish(state: ShapeState, error?: unknown): void;
|
|
47
|
+
close(): void;
|
|
27
48
|
}
|
|
28
49
|
export declare const COLD_SNAPSHOT: StoreSnapshot;
|
|
29
50
|
export {};
|
package/dist/store/shape.js
CHANGED
|
@@ -1,32 +1,141 @@
|
|
|
1
|
+
/** One cached query: its answer, snapshot, observers, in-flight read and retention clock. */
|
|
1
2
|
export class QueryShape {
|
|
2
3
|
key;
|
|
3
4
|
ast;
|
|
4
5
|
page;
|
|
5
6
|
expected;
|
|
6
|
-
listeners = new Set();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
releasedAt;
|
|
14
|
-
|
|
15
|
-
refreshedAt;
|
|
16
|
-
snapshot = COLD_SNAPSHOT;
|
|
17
|
-
constructor(key, ast, page, expected, now) {
|
|
7
|
+
#listeners = new Set();
|
|
8
|
+
#report;
|
|
9
|
+
#snapshot = COLD_SNAPSHOT;
|
|
10
|
+
#answer;
|
|
11
|
+
#inflight;
|
|
12
|
+
#invalidation = 0;
|
|
13
|
+
#accessedAt;
|
|
14
|
+
#releasedAt;
|
|
15
|
+
constructor(key, ast, page, expected, now, report) {
|
|
18
16
|
this.key = key;
|
|
19
17
|
this.ast = ast;
|
|
20
18
|
this.page = page;
|
|
21
19
|
this.expected = expected;
|
|
22
|
-
this
|
|
20
|
+
this.#accessedAt = now;
|
|
21
|
+
this.#releasedAt = now;
|
|
22
|
+
this.#report = report;
|
|
23
|
+
}
|
|
24
|
+
get snapshot() {
|
|
25
|
+
return this.#snapshot;
|
|
26
|
+
}
|
|
27
|
+
get state() {
|
|
28
|
+
return this.#snapshot.state;
|
|
29
|
+
}
|
|
30
|
+
get answer() {
|
|
31
|
+
return this.#answer;
|
|
32
|
+
}
|
|
33
|
+
get inflight() {
|
|
34
|
+
return this.#inflight;
|
|
35
|
+
}
|
|
36
|
+
get invalidation() {
|
|
37
|
+
return this.#invalidation;
|
|
38
|
+
}
|
|
39
|
+
get accessedAt() {
|
|
40
|
+
return this.#accessedAt;
|
|
23
41
|
}
|
|
24
42
|
get observed() {
|
|
25
|
-
return this
|
|
43
|
+
return this.#listeners.size > 0;
|
|
44
|
+
}
|
|
45
|
+
/** When the shape became unobserved and idle, or undefined while it is observed or reading. */
|
|
46
|
+
idleSince() {
|
|
47
|
+
return this.observed || this.#inflight !== undefined ? undefined : this.#releasedAt;
|
|
26
48
|
}
|
|
27
49
|
referencesAny(nodes, edges) {
|
|
28
|
-
|
|
29
|
-
|
|
50
|
+
const references = this.#answer?.references;
|
|
51
|
+
return (references?.nodes.some((id) => nodes.has(id)) === true ||
|
|
52
|
+
references?.edges.some((key) => edges.has(key)) === true);
|
|
53
|
+
}
|
|
54
|
+
touch(now) {
|
|
55
|
+
this.#accessedAt = now;
|
|
56
|
+
if (!this.observed)
|
|
57
|
+
this.#releasedAt = now;
|
|
58
|
+
}
|
|
59
|
+
listen(listener, now) {
|
|
60
|
+
this.#listeners.add(listener);
|
|
61
|
+
this.#accessedAt = now;
|
|
62
|
+
this.#releasedAt = undefined;
|
|
63
|
+
}
|
|
64
|
+
/** Removes a listener; the caller releases the shape once it is no longer observed. */
|
|
65
|
+
unlisten(listener) {
|
|
66
|
+
this.#listeners.delete(listener);
|
|
67
|
+
}
|
|
68
|
+
release(now) {
|
|
69
|
+
this.#releasedAt = now;
|
|
70
|
+
}
|
|
71
|
+
begin(read) {
|
|
72
|
+
this.#inflight = read;
|
|
73
|
+
}
|
|
74
|
+
/** Ends `read` if it is still the shape's read and reports whether it was. */
|
|
75
|
+
finish(read) {
|
|
76
|
+
if (this.#inflight !== read)
|
|
77
|
+
return false;
|
|
78
|
+
this.#inflight = undefined;
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
invalidate() {
|
|
82
|
+
this.#invalidation += 1;
|
|
83
|
+
}
|
|
84
|
+
/** Records a newly confirmed answer, then publishes `state`. */
|
|
85
|
+
settle(answer, state) {
|
|
86
|
+
this.#answer = answer;
|
|
87
|
+
this.touch(answer.refreshedAt);
|
|
88
|
+
this.publish(state);
|
|
89
|
+
}
|
|
90
|
+
/** Replaces the displayed answer without a new server confirmation, then publishes `state`. */
|
|
91
|
+
show(answer, state) {
|
|
92
|
+
this.#answer = answer;
|
|
93
|
+
this.publish(state);
|
|
94
|
+
}
|
|
95
|
+
publish(state, error) {
|
|
96
|
+
const next = snapshotFor(state, this.#answer?.response, error);
|
|
97
|
+
if (sameSnapshot(this.#snapshot, next))
|
|
98
|
+
return;
|
|
99
|
+
this.#snapshot = next;
|
|
100
|
+
for (const listener of Array.from(this.#listeners)) {
|
|
101
|
+
try {
|
|
102
|
+
listener();
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
this.#report(error);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
close() {
|
|
110
|
+
this.#listeners.clear();
|
|
111
|
+
this.#inflight?.catch(() => undefined);
|
|
30
112
|
}
|
|
31
113
|
}
|
|
32
114
|
export const COLD_SNAPSHOT = Object.freeze({ state: 'cold' });
|
|
115
|
+
function snapshotFor(state, response, error) {
|
|
116
|
+
switch (state) {
|
|
117
|
+
case 'cold':
|
|
118
|
+
return Object.freeze({ state });
|
|
119
|
+
case 'loading':
|
|
120
|
+
return Object.freeze({ state });
|
|
121
|
+
case 'failed':
|
|
122
|
+
return Object.freeze({ state, error, ...(response === undefined ? {} : { response }) });
|
|
123
|
+
case 'ready':
|
|
124
|
+
case 'stale':
|
|
125
|
+
case 'refreshing':
|
|
126
|
+
case 'optimistic':
|
|
127
|
+
if (response === undefined)
|
|
128
|
+
throw new TypeError(`Store ${state} snapshot requires a response.`);
|
|
129
|
+
return Object.freeze({ state, response });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function sameSnapshot(left, right) {
|
|
133
|
+
if (left.state !== right.state)
|
|
134
|
+
return false;
|
|
135
|
+
if ('response' in left || 'response' in right) {
|
|
136
|
+
if (!('response' in left) || !('response' in right) || left.response !== right.response) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return left.state !== 'failed' || right.state !== 'failed' || left.error === right.error;
|
|
141
|
+
}
|
package/dist/store/store.js
CHANGED
|
@@ -56,7 +56,7 @@ export class GraphStore {
|
|
|
56
56
|
}
|
|
57
57
|
peek(query, options) {
|
|
58
58
|
const { key } = this.identity(query, options);
|
|
59
|
-
return this.memory.get(key)?.response;
|
|
59
|
+
return this.memory.get(key)?.answer?.response;
|
|
60
60
|
}
|
|
61
61
|
async preload(query, options) {
|
|
62
62
|
await this.read(query, options);
|
|
@@ -1,23 +1,35 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
readonly
|
|
1
|
+
export type AutoVia = 'websocket' | 'http';
|
|
2
|
+
/** A state the Auto transport choice can rest in. */
|
|
3
|
+
export type AutoSettled = {
|
|
4
|
+
readonly kind: 'undecided';
|
|
5
5
|
} | {
|
|
6
|
-
readonly kind: '
|
|
7
|
-
readonly
|
|
6
|
+
readonly kind: 'chosen';
|
|
7
|
+
readonly via: AutoVia;
|
|
8
8
|
} | {
|
|
9
|
-
readonly kind: '
|
|
9
|
+
readonly kind: 'closed';
|
|
10
10
|
};
|
|
11
|
+
/** What `begin` grants: the probe itself, or the state the caller must follow. */
|
|
12
|
+
export type AutoTicket = {
|
|
13
|
+
readonly kind: 'probe';
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: 'probing';
|
|
16
|
+
readonly settled: Promise<AutoSettled>;
|
|
17
|
+
} | Exclude<AutoSettled, {
|
|
18
|
+
kind: 'undecided';
|
|
19
|
+
}>;
|
|
11
20
|
/** Serializes the one initial transport decision without retaining invocation values. */
|
|
12
21
|
export declare class AutoDecision {
|
|
13
|
-
private
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
begin():
|
|
17
|
-
|
|
22
|
+
private state;
|
|
23
|
+
get closed(): boolean;
|
|
24
|
+
/** Claim the probe when undecided; otherwise report the state to follow. */
|
|
25
|
+
begin(): AutoTicket;
|
|
26
|
+
/** The probe proved WebSocket usable. */
|
|
27
|
+
selectWebSocket(): void;
|
|
18
28
|
/** Permanently degrade a prior or pending WebSocket selection to HTTP. */
|
|
19
29
|
demote(): void;
|
|
30
|
+
/** The probe proved nothing; the next caller probes again. */
|
|
20
31
|
release(): void;
|
|
21
|
-
|
|
32
|
+
/** Returns whether this call closed the decision. */
|
|
33
|
+
close(): boolean;
|
|
34
|
+
private move;
|
|
22
35
|
}
|
|
23
|
-
export {};
|
|
@@ -1,48 +1,51 @@
|
|
|
1
|
+
const UNDECIDED = Object.freeze({ kind: 'undecided' });
|
|
2
|
+
const CLOSED = Object.freeze({ kind: 'closed' });
|
|
1
3
|
/** Serializes the one initial transport decision without retaining invocation values. */
|
|
2
4
|
export class AutoDecision {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
state = UNDECIDED;
|
|
6
|
+
get closed() {
|
|
7
|
+
return this.state.kind === 'closed';
|
|
8
|
+
}
|
|
9
|
+
/** Claim the probe when undecided; otherwise report the state to follow. */
|
|
6
10
|
begin() {
|
|
7
|
-
if (this.
|
|
8
|
-
return
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return { kind: 'wait', selection: this.pending.promise };
|
|
13
|
-
let resolve;
|
|
14
|
-
const promise = new Promise((settle) => {
|
|
15
|
-
resolve = settle;
|
|
11
|
+
if (this.state.kind !== 'undecided')
|
|
12
|
+
return this.state;
|
|
13
|
+
let settle;
|
|
14
|
+
const settled = new Promise((resolve) => {
|
|
15
|
+
settle = resolve;
|
|
16
16
|
});
|
|
17
|
-
this.
|
|
17
|
+
this.state = { kind: 'probing', settled, settle };
|
|
18
18
|
return { kind: 'probe' };
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.pending?.resolve(selection);
|
|
25
|
-
this.pending = undefined;
|
|
20
|
+
/** The probe proved WebSocket usable. */
|
|
21
|
+
selectWebSocket() {
|
|
22
|
+
if (this.state.kind === 'probing')
|
|
23
|
+
this.move({ kind: 'chosen', via: 'websocket' });
|
|
26
24
|
}
|
|
27
25
|
/** Permanently degrade a prior or pending WebSocket selection to HTTP. */
|
|
28
26
|
demote() {
|
|
29
|
-
if (this.
|
|
27
|
+
if (this.state.kind === 'closed')
|
|
28
|
+
return;
|
|
29
|
+
if (this.state.kind === 'chosen' && this.state.via === 'http')
|
|
30
30
|
return;
|
|
31
|
-
this.
|
|
32
|
-
this.pending?.resolve('http');
|
|
33
|
-
this.pending = undefined;
|
|
31
|
+
this.move({ kind: 'chosen', via: 'http' });
|
|
34
32
|
}
|
|
33
|
+
/** The probe proved nothing; the next caller probes again. */
|
|
35
34
|
release() {
|
|
36
|
-
if (this.
|
|
37
|
-
|
|
38
|
-
this.pending?.resolve(undefined);
|
|
39
|
-
this.pending = undefined;
|
|
35
|
+
if (this.state.kind === 'probing')
|
|
36
|
+
this.move(UNDECIDED);
|
|
40
37
|
}
|
|
38
|
+
/** Returns whether this call closed the decision. */
|
|
41
39
|
close() {
|
|
42
|
-
if (this.closed)
|
|
43
|
-
return;
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
if (this.state.kind === 'closed')
|
|
41
|
+
return false;
|
|
42
|
+
this.move(CLOSED);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
move(next) {
|
|
46
|
+
const previous = this.state;
|
|
47
|
+
this.state = next;
|
|
48
|
+
if (previous.kind === 'probing')
|
|
49
|
+
previous.settle(next);
|
|
47
50
|
}
|
|
48
51
|
}
|