Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/client-runtime/src/relay/cloudAccountService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
planCloudAccountStep,
} from "./cloudAccountService.ts";

type Emit = (user: { readonly id: string } | null | undefined) => void;
type Emit = (user: { readonly id: string } | null | undefined, clientId?: string) => void;

function makeSource() {
let listener: Parameters<CloudAccountSource["addListener"]>[0] | null = null;
Expand All @@ -18,7 +18,8 @@ function makeSource() {
};
},
};
const emit: Emit = (user) => listener?.({ user });
const emit: Emit = (user, clientId = "client_1") =>
listener?.({ user, client: { id: clientId } });
return { source, emit, isSubscribed: () => listener !== null };
}

Expand Down Expand Up @@ -154,6 +155,32 @@ describe("createCloudAccountService", () => {
expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);
});

it("waits for Clerk's API to confirm a signed-out launch before cleaning up", async () => {
const { service, log, nextActivation, nextCleanUp } = makeService();
const { source, emit } = makeSource();
service.attach(source);

// Offline: an id-less client, or the placeholder built from the session cookie.
emit(null, "");
emit(null, "client_init");
expect(service.getState()).toEqual({ _tag: "Unobserved" });
expect(log).toEqual([]);

const cleanUp = nextCleanUp();
emit(null);
await cleanUp;
expect(log).toEqual(["deactivate", "cleanUp:null:false"]);

const activation = nextActivation();
emit({ id: "a" });
await activation;
log.length = 0;
const signOut = nextCleanUp();
emit(null, "client_init");
await signOut;
expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]);
});

it("never activates an account that signed out while it was activating", async () => {
const { service, log, nextCleanUp, gate } = makeService();
const { source, emit } = makeSource();
Expand Down
29 changes: 26 additions & 3 deletions packages/client-runtime/src/relay/cloudAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import { reportAtomCommandResult, settlePromise } from "../state/runtime.ts";
*/
export interface CloudAccountSource {
readonly addListener: (
listener: (resources: { readonly user?: { readonly id: string } | null | undefined }) => void,
listener: (resources: {
readonly client?: { readonly id?: string | null | undefined } | null | undefined;
readonly user?: { readonly id: string } | null | undefined;
}) => void,
) => () => void;
}

/**
* Clerk's load status cannot prove its API answered: offline it emits an
* id-less client, and a failed launch fetch emits a "client_init" placeholder
* built from the session cookie. Only a real /v1/client response has an id.
*/
const isClientFromClerkApi = (
client: { readonly id?: string | null | undefined } | null | undefined,
) => Boolean(client?.id) && client?.id !== "client_init";

interface CloudAccountSignIn {
readonly _tag: "SignIn";
readonly accountId: string;
Expand All @@ -32,7 +44,8 @@ export type CloudAccountStep =
/**
* `previous` is undefined until Clerk's first emission. A first emission that
* is signed out still cleans up, so data left by an account that signed out
* while the app was closed does not linger.
* while the app was closed does not linger. The service only plans that step
* once Clerk's API has confirmed the sign-out.
*/
export function planCloudAccountStep(
previous: string | null | undefined,
Expand Down Expand Up @@ -200,7 +213,17 @@ export function createCloudAccountService<S extends CloudAccountSource>(
// Clerk reports `user: undefined` until it loads; pending sessions
// still carry a user and count as signed in.
if (resources.user === undefined) return;
observe(source, resources.user?.id ?? null);
const next = resources.user?.id ?? null;
// An unreachable Clerk API at launch looks signed out; stay unobserved
// until Clerk confirms it, so saved environments survive offline starts.
if (
next === null &&
state._tag === "Unobserved" &&
!isClientFromClerkApi(resources.client)
) {
return;
}
observe(source, next);
}),
};
attached = current;
Expand Down
Loading