Open History to see the last commit
1import type { EnvironmentRpcSubscriptionObservation } from "@t3tools/client-runtime/rpc";
2import { type BackgroundScope, type EnvironmentId, WS_METHODS } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4
5interface RetainedScope {
6 readonly environmentId: EnvironmentId;
7 readonly scope: BackgroundScope;
8 refCount: number;
9}
10
11const retainedScopes = new Map<string, RetainedScope>();
12const listeners = new Set<() => void>();
13
14function notify(): void {
15 for (const listener of listeners) {
16 try {
17 listener();
18 } catch {
19 // A failing observer must not corrupt retained-scope lifetime.
20 }
21 }
22}
23
24function stableScopeKey(environmentId: EnvironmentId, scope: BackgroundScope): string {
25 switch (scope.type) {
26 case "server-config":
27 case "diagnostics":
28 return JSON.stringify([environmentId, scope.type]);
29 case "provider-status":
30 return JSON.stringify([environmentId, scope.type, scope.instanceId ?? null]);
31 case "vcs-status":
32 case "git-refs":
33 return JSON.stringify([environmentId, scope.type, scope.cwd]);
34 case "thread":
35 return JSON.stringify([environmentId, scope.type, scope.threadId]);
36 }
37}
38
39function scopeForSubscription(
40 observation: EnvironmentRpcSubscriptionObservation,
41): BackgroundScope | null {
42 if (observation.method === WS_METHODS.subscribeResourceTelemetry) {
43 return { type: "diagnostics" };
44 }
45 if (observation.method !== WS_METHODS.subscribeVcsStatus) {
46 return null;
47 }
48 const input = observation.input as { readonly cwd?: unknown };
49 return typeof input.cwd === "string" ? { type: "vcs-status", cwd: input.cwd } : null;
50}
51
52export function retainedMobileBackgroundScopes(
53 environmentId: EnvironmentId,
54): ReadonlyArray<BackgroundScope> {
55 return Array.from(retainedScopes.values(), (entry) =>
56 entry.environmentId === environmentId ? entry.scope : null,
57 ).filter((scope): scope is BackgroundScope => scope !== null);
58}
59
60export function observeMobileBackgroundActivitySubscription(
61 observation: EnvironmentRpcSubscriptionObservation,
62): Effect.Effect<Effect.Effect<void>> {
63 const scope = scopeForSubscription(observation);
64 if (scope === null) return Effect.succeed(Effect.void);
65 return Effect.sync(() => {
66 const environmentId = observation.environmentId as EnvironmentId;
67 const key = stableScopeKey(environmentId, scope);
68 const current = retainedScopes.get(key);
69 if (current) {
70 current.refCount += 1;
71 } else {
72 retainedScopes.set(key, { environmentId, scope, refCount: 1 });
73 notify();
74 }
75 return Effect.sync(() => {
76 const retained = retainedScopes.get(key);
77 if (!retained) return;
78 retained.refCount -= 1;
79 if (retained.refCount <= 0) {
80 retainedScopes.delete(key);
81 notify();
82 }
83 });
84 });
85}
86
87export function onRetainedMobileBackgroundScopesChange(listener: () => void): () => void {
88 listeners.add(listener);
89 return () => {
90 listeners.delete(listener);
91 };
92}