@astrale-os/kernel-client 1.0.0-beta.26 → 1.0.0-beta.27

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.
Files changed (68) hide show
  1. package/dist/client/admission.js +3 -1
  2. package/dist/client/client.d.ts +16 -3
  3. package/dist/client/client.js +36 -15
  4. package/dist/client/index.d.ts +2 -2
  5. package/dist/client/index.js +1 -1
  6. package/dist/client/routing-control.d.ts +11 -15
  7. package/dist/client/routing-control.js +21 -29
  8. package/dist/errors/query-input.js +2 -1
  9. package/dist/errors/stale-route-error.d.ts +1 -1
  10. package/dist/errors/stale-route-error.js +1 -1
  11. package/dist/graph/page-contract.d.ts +6 -1
  12. package/dist/graph/query.js +9 -3
  13. package/dist/session/admission.js +4 -2
  14. package/dist/session/credential/credentials.js +32 -28
  15. package/dist/session/discovery/bundle.d.ts +9 -4
  16. package/dist/session/discovery/bundle.js +28 -35
  17. package/dist/session/discovery/discovery.d.ts +1 -1
  18. package/dist/session/discovery/discovery.js +7 -10
  19. package/dist/session/discovery/release.js +18 -28
  20. package/dist/session/discovery/transport.d.ts +35 -3
  21. package/dist/session/discovery/transport.js +40 -1
  22. package/dist/session/exchange/exchange.d.ts +3 -2
  23. package/dist/session/exchange/exchange.js +10 -7
  24. package/dist/session/exchange/index.d.ts +1 -1
  25. package/dist/session/exchange/index.js +1 -1
  26. package/dist/session/exchange/presentation.js +28 -10
  27. package/dist/session/exchange/source.d.ts +7 -7
  28. package/dist/session/exchange/source.js +26 -27
  29. package/dist/session/routing/auth.d.ts +9 -3
  30. package/dist/session/routing/auth.js +4 -3
  31. package/dist/session/routing/cache.d.ts +12 -5
  32. package/dist/session/routing/cache.js +56 -48
  33. package/dist/session/routing/dispatch.d.ts +2 -1
  34. package/dist/session/routing/dispatch.js +83 -61
  35. package/dist/session/routing/index.d.ts +1 -1
  36. package/dist/session/routing/recovery.d.ts +3 -6
  37. package/dist/session/routing/recovery.js +3 -14
  38. package/dist/session/routing/store.js +4 -5
  39. package/dist/store/memory.d.ts +1 -3
  40. package/dist/store/memory.js +61 -112
  41. package/dist/store/shape.d.ts +33 -12
  42. package/dist/store/shape.js +125 -16
  43. package/dist/store/store.js +1 -1
  44. package/dist/transport/auto/decision.d.ts +26 -14
  45. package/dist/transport/auto/decision.js +35 -32
  46. package/dist/transport/auto/fallback.d.ts +1 -1
  47. package/dist/transport/auto/fallback.js +5 -10
  48. package/dist/transport/auto/transport.d.ts +0 -2
  49. package/dist/transport/auto/transport.js +24 -32
  50. package/dist/transport/failure.d.ts +28 -1
  51. package/dist/transport/failure.js +57 -1
  52. package/dist/transport/http/admission.js +2 -2
  53. package/dist/transport/http/binary.js +1 -1
  54. package/dist/transport/http/byte-stream.d.ts +2 -1
  55. package/dist/transport/http/byte-stream.js +10 -7
  56. package/dist/transport/http/stream.d.ts +8 -13
  57. package/dist/transport/http/stream.js +32 -79
  58. package/dist/transport/http/transport.js +11 -22
  59. package/dist/transport/stream.d.ts +52 -0
  60. package/dist/transport/stream.js +101 -0
  61. package/dist/transport/unary.d.ts +3 -2
  62. package/dist/transport/unary.js +3 -3
  63. package/dist/transport/websocket/connection.d.ts +7 -3
  64. package/dist/transport/websocket/connection.js +102 -85
  65. package/dist/transport/websocket/exchange.d.ts +10 -19
  66. package/dist/transport/websocket/exchange.js +72 -153
  67. package/dist/transport/websocket/transport.js +11 -19
  68. package/package.json +5 -5
@@ -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
- this.touch(existing, now);
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
- this.touch(shape, this.options.now());
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 reusable = shape.response !== undefined &&
47
+ const prior = shape.answer;
48
+ const reusable = prior !== undefined &&
49
49
  shape.state === 'ready' &&
50
- shape.refreshedAt !== undefined &&
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 shape.response;
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
- this.publish(shape, prior === undefined ? 'loading' : this.overlay.hasAny() ? 'optimistic' : 'refreshing');
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
- this.publish(shape, prior === undefined ? 'cold' : this.overlay.hasAny() ? 'optimistic' : 'stale');
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
- shape.references = NO_REFERENCES;
71
- shape.confirmed = result;
72
- shape.response = result;
73
- shape.refreshedAt = this.options.now();
74
- this.touch(shape, shape.refreshedAt);
75
- this.publish(shape, 'ready');
76
- return shape.response;
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
- shape.references = integration.references;
80
- shape.confirmed = result;
81
- shape.response = this.materialize(result, integration.references);
82
- shape.refreshedAt = this.options.now();
83
- this.touch(shape, shape.refreshedAt);
84
- this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'ready');
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 || other.response === undefined)
86
+ if (other === shape)
87
87
  continue;
88
88
  if (other.referencesAny(integration.changedNodes, integration.changedEdges)) {
89
- other.response = this.materialize(other.confirmed, other.references);
90
- this.publish(other, other.state);
89
+ this.rematerialize(other, other.state);
91
90
  }
92
91
  }
93
- return shape.response;
92
+ return answer.response;
94
93
  })
95
94
  .catch((error) => {
96
95
  if (prior === undefined)
97
- this.publish(shape, 'failed', error);
98
- else {
99
- shape.response = prior;
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 !== 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 = 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.listeners.add(listener);
123
- shape.accessedAt = this.options.now();
124
- shape.releasedAt = undefined;
118
+ shape.listen(listener, this.options.now());
125
119
  return () => {
126
- shape.listeners.delete(listener);
120
+ shape.unlisten(listener);
127
121
  if (!shape.observed)
128
- shape.releasedAt = this.options.now();
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.invalidation += 1;
135
- if (candidate.response === undefined)
128
+ candidate.invalidate();
129
+ if (candidate.answer === undefined)
136
130
  continue;
137
- this.publish(candidate, this.overlay.hasAny() ? 'optimistic' : 'stale');
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.response === undefined || shape.confirmed?.certification === 'uncertified')
139
+ if (shape.answer?.confirmed.certification === 'uncertified')
146
140
  continue;
147
- shape.response = this.materialize(shape.confirmed, shape.references);
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
- if (shape.response === undefined)
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
- if (this.releasable(shape) && now - shape.releasedAt >= this.options.ttlMs) {
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 (!this.releasable(shape))
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?.nodes.forEach((id) => nodes.add(id));
197
- shape.references?.edges.forEach((key) => edges.add(key));
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.listeners.clear();
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 && this.pending.length > 0) {
324
- this.pending.shift().run();
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.reason));
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
- }
@@ -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 listeners: Set<() => void>;
14
- state: ShapeState;
15
- confirmed?: StoreQueryResponse;
16
- response?: StoreQueryResponse;
17
- references?: FactReferences;
18
- inflight?: Promise<StoreQueryResponse>;
19
- invalidation: number;
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 {};
@@ -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
- state = 'cold';
8
- confirmed;
9
- response;
10
- references;
11
- inflight;
12
- invalidation = 0;
13
- releasedAt;
14
- accessedAt;
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.accessedAt = now;
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.listeners.size > 0;
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
- return (this.references?.nodes.some((id) => nodes.has(id)) === true ||
29
- this.references?.edges.some((key) => edges.has(key)) === true);
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
+ }
@@ -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 AutoSelection = 'websocket' | 'http';
2
- type AutoDecisionTicket = {
3
- readonly kind: 'selected';
4
- readonly selection: AutoSelection;
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: 'wait';
7
- readonly selection: Promise<AutoSelection | undefined>;
6
+ readonly kind: 'chosen';
7
+ readonly via: AutoVia;
8
8
  } | {
9
- readonly kind: 'probe';
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 selected;
14
- private pending;
15
- private closed;
16
- begin(): AutoDecisionTicket;
17
- select(selection: AutoSelection): void;
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
- close(): void;
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
- selected;
4
- pending;
5
- closed = false;
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.closed)
8
- return { kind: 'wait', selection: Promise.resolve(undefined) };
9
- if (this.selected)
10
- return { kind: 'selected', selection: this.selected };
11
- if (this.pending)
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.pending = { promise, resolve };
17
+ this.state = { kind: 'probing', settled, settle };
18
18
  return { kind: 'probe' };
19
19
  }
20
- select(selection) {
21
- if (this.closed || this.selected)
22
- return;
23
- this.selected = selection;
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.closed || this.selected === 'http')
27
+ if (this.state.kind === 'closed')
28
+ return;
29
+ if (this.state.kind === 'chosen' && this.state.via === 'http')
30
30
  return;
31
- this.selected = 'http';
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.closed || this.selected)
37
- return;
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.closed = true;
45
- this.pending?.resolve(undefined);
46
- this.pending = undefined;
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
  }