Skip to content

Commit 825d79f

Browse files
authored
refactor(react): unify headless init with replayInterceptedInvocations (#8011)
1 parent 55d793a commit 825d79f

1 file changed

Lines changed: 6 additions & 34 deletions

File tree

packages/react/src/isomorphicClerk.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,12 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
178178
#publishableKey: string;
179179
#eventBus = createClerkEventBus();
180180
#stateProxy: StateProxy;
181-
#initialized = false;
182-
183181
get publishableKey(): string {
184182
return this.#publishableKey;
185183
}
186184

187185
get loaded(): boolean {
188-
// Consider loaded if either clerk is loaded OR we've initialized headlessly
189-
return this.clerkjs?.loaded || this.#initialized;
186+
return this.clerkjs?.loaded || false;
190187
}
191188

192189
get status(): ClerkStatus {
@@ -295,6 +292,7 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
295292

296293
/**
297294
* Initialize Clerk for headless/React Native environments where a Clerk instance is provided directly.
295+
* Only handles Clerk construction and loading — post-load wiring is shared via replayInterceptedInvocations.
298296
*/
299297
private loadHeadlessClerk(): void {
300298
const clerk = isConstructor<BrowserClerkConstructor | HeadlessBrowserClerkConstructor>(this.options.Clerk)
@@ -306,40 +304,14 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
306304
return;
307305
}
308306

309-
// Helper to finish initialization - marks as ready and triggers re-renders
310-
const finishInit = () => {
311-
this.#initialized = true;
312-
this.clerkjs = clerk;
313-
this.premountMethodCalls.forEach(cb => cb());
314-
this.premountAddListenerCalls.forEach((listenerExtras, listener) => {
315-
const unsubscribe = clerk.addListener(listener, listenerExtras.options);
316-
listenerExtras.handlers.nativeUnsubscribe = unsubscribe;
317-
});
318-
319-
// Emit current state to all listeners so React context gets updated with actual values
320-
// Use null instead of undefined for missing values to signal "loaded but empty"
321-
const currentState = {
322-
client: clerk.client ?? null,
323-
session: clerk.session ?? null,
324-
user: clerk.user ?? null,
325-
organization: clerk.organization ?? null,
326-
};
327-
if (currentState.client) {
328-
this.premountAddListenerCalls.forEach((_, listener) => {
329-
listener(currentState as Resources);
330-
});
331-
}
332-
333-
// Emit status through eventBus
334-
this.#eventBus.emit(clerkEvents.Status, 'ready');
335-
this.emitLoaded();
307+
const onLoaded = () => {
308+
this.replayInterceptedInvocations(clerk);
336309
};
337310

338-
// Try to load, but finish initialization regardless
339311
if (!clerk.loaded) {
340312
clerk
341313
.load(this.options)
342-
.then(() => finishInit())
314+
.then(() => onLoaded())
343315
.catch(err => {
344316
if (__DEV__) {
345317
console.error('Clerk: Failed to load:', err);
@@ -348,7 +320,7 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
348320
this.emitLoaded();
349321
});
350322
} else {
351-
finishInit();
323+
onLoaded();
352324
}
353325
}
354326

0 commit comments

Comments
 (0)