Skip to content

Commit 3d9b3cb

Browse files
refactor(client): keep the cloud account service options mobile needs
The mobile cloud account service (#25150) uses prepare, onSignOutTransition, source/wasActive, isAccountTransition, getState and the Failed state, so they are not dead. This PR now only trims the VCS wake support and the dispatcher element scopes, which have no callers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 34243cd commit 3d9b3cb

2 files changed

Lines changed: 197 additions & 58 deletions

File tree

‎packages/client-runtime/src/relay/cloudAccountService.test.ts‎

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,26 @@ function makeService() {
2626
const log: string[] = [];
2727
const activated: Array<PromiseWithResolvers<void>> = [];
2828
const cleanedUp: Array<PromiseWithResolvers<void>> = [];
29-
const cleanUpGates = new Map<string, PromiseWithResolvers<void>>();
29+
const gates = new Map<
30+
string,
31+
{ readonly started: PromiseWithResolvers<void>; readonly release: PromiseWithResolvers<void> }
32+
>();
3033
const service = createCloudAccountService<CloudAccountSource>({
3134
deactivate: () => log.push("deactivate"),
3235
detach: () => log.push("detach"),
33-
cleanUp: async ({ accountId }) => {
34-
log.push(`cleanUp:${accountId}`);
35-
await cleanUpGates.get(accountId ?? "")?.promise;
36+
onSignOutTransition: () => log.push("signOutTransition"),
37+
cleanUp: async ({ accountId, wasActive }) => {
38+
log.push(`cleanUp:${accountId}:${wasActive}`);
3639
cleanedUp.shift()?.resolve();
3740
},
38-
activate: ({ accountId }) => {
39-
log.push(`activate:${accountId}`);
41+
prepare: async ({ accountId }) => {
42+
log.push(`prepare:${accountId}`);
43+
const gate = gates.get(accountId);
44+
gate?.started.resolve();
45+
await gate?.release.promise;
46+
},
47+
activate: ({ accountId, isAccountTransition }) => {
48+
log.push(`activate:${accountId}:${isAccountTransition}`);
4049
activated.shift()?.resolve();
4150
},
4251
});
@@ -50,24 +59,29 @@ function makeService() {
5059
cleanedUp.push(deferred);
5160
return deferred.promise;
5261
};
53-
const holdCleanUp = (accountId: string) => {
54-
const deferred = Promise.withResolvers<void>();
55-
cleanUpGates.set(accountId, deferred);
62+
const gate = (accountId: string) => {
63+
const deferred = {
64+
started: Promise.withResolvers<void>(),
65+
release: Promise.withResolvers<void>(),
66+
};
67+
gates.set(accountId, deferred);
5668
return deferred;
5769
};
58-
return { service, log, nextActivation, nextCleanUp, holdCleanUp };
70+
return { service, log, nextActivation, nextCleanUp, gate };
5971
}
6072

6173
describe("planCloudAccountStep", () => {
62-
it("plans the first observation from no previous account", () => {
74+
it("treats the first observation as a non-transition", () => {
6375
expect(planCloudAccountStep(undefined, "a")).toEqual({
6476
_tag: "SignIn",
6577
accountId: "a",
6678
switchFromAccountId: null,
79+
isAccountTransition: false,
6780
});
6881
expect(planCloudAccountStep(undefined, null)).toEqual({
6982
_tag: "SignOut",
7083
previousAccountId: null,
84+
isAccountTransition: false,
7185
});
7286
});
7387

@@ -78,6 +92,7 @@ describe("planCloudAccountStep", () => {
7892
_tag: "SignIn",
7993
accountId: "b",
8094
switchFromAccountId: "a",
95+
isAccountTransition: true,
8196
});
8297
});
8398
});
@@ -89,14 +104,15 @@ describe("createCloudAccountService", () => {
89104
service.attach(source);
90105

91106
emit(undefined);
92-
expect(log).toEqual([]);
107+
expect(service.getState()).toEqual({ _tag: "Unobserved" });
93108
const activation = nextActivation();
94109
emit({ id: "a" });
95110
emit({ id: "a" });
111+
expect(service.getState()).toEqual({ _tag: "Activating", accountId: "a" });
96112
await activation;
97-
emit({ id: "a" });
98113

99-
expect(log).toEqual(["activate:a"]);
114+
expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" });
115+
expect(log).toEqual(["prepare:a", "activate:a:false"]);
100116
});
101117

102118
it("drops credentials synchronously and cleans up before activating the next account", async () => {
@@ -113,7 +129,7 @@ describe("createCloudAccountService", () => {
113129
expect(log).toEqual(["deactivate"]);
114130
await second;
115131

116-
expect(log).toEqual(["deactivate", "cleanUp:a", "activate:b"]);
132+
expect(log).toEqual(["deactivate", "cleanUp:a:true", "prepare:b", "activate:b:true"]);
117133
});
118134

119135
it("cleans up on sign-out, including a signed-out first observation", async () => {
@@ -124,7 +140,7 @@ describe("createCloudAccountService", () => {
124140
const initialCleanUp = nextCleanUp();
125141
emit(null);
126142
await initialCleanUp;
127-
expect(log).toEqual(["deactivate", "cleanUp:null"]);
143+
expect(log).toEqual(["deactivate", "cleanUp:null:false"]);
128144

129145
const activation = nextActivation();
130146
emit({ id: "a" });
@@ -134,27 +150,70 @@ describe("createCloudAccountService", () => {
134150
const cleanUp = nextCleanUp();
135151
emit(null);
136152
await cleanUp;
137-
expect(log).toEqual(["deactivate", "cleanUp:a"]);
153+
expect(service.getState()).toEqual({ _tag: "SignedOut" });
154+
expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);
138155
});
139156

140157
it("never activates an account that signed out while it was activating", async () => {
141-
const { service, log, nextActivation, nextCleanUp, holdCleanUp } = makeService();
158+
const { service, log, nextCleanUp, gate } = makeService();
142159
const { source, emit } = makeSource();
143160
service.attach(source);
144-
const first = nextActivation();
161+
const prepared = gate("a");
145162
emit({ id: "a" });
146-
await first;
147-
log.length = 0;
163+
await prepared.started.promise;
164+
const cleanUp = nextCleanUp();
165+
emit(null);
166+
prepared.release.resolve();
167+
await cleanUp;
148168

149-
const heldCleanUp = holdCleanUp("a");
150-
emit({ id: "b" });
169+
expect(service.getState()).toEqual({ _tag: "SignedOut" });
170+
expect(log).toEqual(["prepare:a", "signOutTransition", "deactivate", "cleanUp:a:false"]);
171+
});
172+
173+
it("retries a rejected activation on the next emission for the same account", async () => {
174+
const log: string[] = [];
175+
const failed = Promise.withResolvers<void>();
176+
const activated = Promise.withResolvers<void>();
177+
let attempts = 0;
178+
const service = createCloudAccountService<CloudAccountSource>({
179+
deactivate: () => log.push("deactivate"),
180+
detach: () => log.push("detach"),
181+
cleanUp: async ({ accountId, wasActive }) => {
182+
log.push(`cleanUp:${accountId}:${wasActive}`);
183+
},
184+
prepare: async ({ accountId }) => {
185+
attempts += 1;
186+
log.push(`prepare:${accountId}`);
187+
if (attempts === 1) {
188+
// Queued before the rejection, so the service's catch runs before
189+
// the test's `await failed.promise` continuation.
190+
queueMicrotask(failed.resolve);
191+
throw new Error("restore failed");
192+
}
193+
},
194+
activate: ({ accountId, isAccountTransition }) => {
195+
log.push(`activate:${accountId}:${isAccountTransition}`);
196+
activated.resolve();
197+
},
198+
});
199+
const { source, emit } = makeSource();
200+
service.attach(source);
151201
emit(null);
152-
void nextCleanUp();
153-
const signOutCleanUp = nextCleanUp();
154-
heldCleanUp.resolve();
155-
await signOutCleanUp;
202+
emit({ id: "a" });
203+
await failed.promise;
204+
expect(service.getState()).toMatchObject({ _tag: "Failed", accountId: "a" });
156205

157-
expect(log).toEqual(["deactivate", "deactivate", "cleanUp:a", "cleanUp:b"]);
206+
emit({ id: "a" });
207+
await activated.promise;
208+
209+
expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" });
210+
expect(log).toEqual([
211+
"deactivate",
212+
"cleanUp:null:false",
213+
"prepare:a",
214+
"prepare:a",
215+
"activate:a:true",
216+
]);
158217
});
159218

160219
it("detaches from a replaced Clerk instance", () => {
@@ -179,6 +238,7 @@ describe("createCloudAccountService", () => {
179238

180239
detach();
181240
expect(isSubscribed()).toBe(false);
182-
expect(log).toEqual(["activate:a", "detach"]);
241+
expect(service.getState()).toEqual({ _tag: "Unobserved" });
242+
expect(log).toEqual(["prepare:a", "activate:a:false", "detach"]);
183243
});
184244
});

0 commit comments

Comments
 (0)