@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.
@@ -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
+ }