@econ-v1/rpc 7.2.10 → 7.3.1
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.d.ts +26 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/describe-client-error.d.ts.map +1 -1
- package/dist/grants-contract.d.ts +7 -0
- package/dist/grants-contract.d.ts.map +1 -0
- package/dist/host-context-contract.d.ts +26 -0
- package/dist/host-context-contract.d.ts.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/message-port-client.d.ts +22 -4
- package/dist/message-port-client.d.ts.map +1 -1
- package/dist/model-picker-contract.d.ts +12 -4
- package/dist/model-picker-contract.d.ts.map +1 -1
- package/dist/protocol.d.ts +29 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/stage-context.d.ts +17 -1
- package/dist/stage-context.d.ts.map +1 -1
- package/dist/subscription-state.d.ts +35 -0
- package/dist/subscription-state.d.ts.map +1 -0
- package/package.json +3 -3
- package/src/client.js +21 -2
- package/src/describe-client-error.js +28 -1
- package/src/grants-contract.js +8 -0
- package/src/host-context-contract.js +60 -0
- package/src/index.js +10 -0
- package/src/message-port-client.js +170 -29
- package/src/model-picker-contract.js +10 -4
- package/src/protocol.js +44 -1
- package/src/stage-context.js +6 -2
- package/src/subscription-state.js +74 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/** @typedef {import("./client.js").SubscriptionState} SubscriptionState */
|
|
4
|
+
/** @typedef {{ readonly onState: (state: SubscriptionState) => void, told: boolean }} SubscriptionStateHolder */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One subscription's state and the holders watching it (ADR-039). Every layer that
|
|
8
|
+
* carries a subscription (a carrier, the managed client, the kernel lease, the RPC
|
|
9
|
+
* group) keeps one, so they all fold states the same way.
|
|
10
|
+
*
|
|
11
|
+
* Each `live` is the next generation, and a `live` reported as "subscribed" after an
|
|
12
|
+
* earlier one reads "resubscribed": whatever restarted below (a carrier, a kernel)
|
|
13
|
+
* counts from 1 again, but a holder never sees a generation twice. A repeated state
|
|
14
|
+
* that is not `live` is not re-sent. A holder that joins when there is a state gets
|
|
15
|
+
* it on a microtask, unless a transition reached it first.
|
|
16
|
+
*/
|
|
17
|
+
export class SubscriptionStateCell {
|
|
18
|
+
#generation = 0;
|
|
19
|
+
/** @type {Set<SubscriptionStateHolder>} */
|
|
20
|
+
#holders = new Set();
|
|
21
|
+
/** @type {SubscriptionState | undefined} */
|
|
22
|
+
#state;
|
|
23
|
+
get state() {
|
|
24
|
+
return this.#state;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @param {(state: SubscriptionState) => void} onState
|
|
28
|
+
* @returns {() => void}
|
|
29
|
+
*/
|
|
30
|
+
watch(onState) {
|
|
31
|
+
/** @type {SubscriptionStateHolder} */
|
|
32
|
+
const holder = { onState, told: false };
|
|
33
|
+
this.#holders.add(holder);
|
|
34
|
+
void Promise.resolve().then(() => {
|
|
35
|
+
if (this.#holders.has(holder) && !holder.told && this.#state !== undefined)
|
|
36
|
+
tell(holder, this.#state);
|
|
37
|
+
});
|
|
38
|
+
return () => {
|
|
39
|
+
this.#holders.delete(holder);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @param {SubscriptionState["status"]} status
|
|
44
|
+
* @param {boolean} [confirmed] ignored unless `live`
|
|
45
|
+
* @param {SubscriptionState["reason"]} [reason] ignored unless `live`
|
|
46
|
+
* @returns {void}
|
|
47
|
+
*/
|
|
48
|
+
report(status, confirmed = false, reason = "subscribed") {
|
|
49
|
+
if (status === "live") {
|
|
50
|
+
if (this.#generation++ > 0 && reason === "subscribed")
|
|
51
|
+
reason = "resubscribed";
|
|
52
|
+
}
|
|
53
|
+
else if (this.#state?.status === status)
|
|
54
|
+
return;
|
|
55
|
+
/** @type {SubscriptionState} */
|
|
56
|
+
const state = status === "live"
|
|
57
|
+
? { confirmed, generation: this.#generation, reason, status }
|
|
58
|
+
: { confirmed: false, generation: this.#generation, status };
|
|
59
|
+
this.#state = state;
|
|
60
|
+
for (const holder of [...this.#holders])
|
|
61
|
+
tell(holder, state);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @param {SubscriptionStateHolder} holder @param {SubscriptionState} state */
|
|
66
|
+
function tell(holder, state) {
|
|
67
|
+
holder.told = true;
|
|
68
|
+
try {
|
|
69
|
+
holder.onState(state);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
// One holder's failure must not starve the others.
|
|
73
|
+
}
|
|
74
|
+
}
|