-
Notifications
You must be signed in to change notification settings - Fork 370
feat(clerk-js,clerk-react,types): Introduce state signals #6450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
415f283
fbd1f03
2d00936
e71897c
66f9a55
45e3607
57ef049
a3614d7
0ed14b6
d82111c
253bd9c
bd5b8e6
18d4b57
63c4a45
1bd1005
1e3bf21
e08b6aa
37f2eca
3ee4911
7c828e4
bc8457b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/clerk-react': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
[Experimental] Signals |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
import { createEventBus } from '@clerk/shared/eventBus'; | ||
import type { TokenResource } from '@clerk/types'; | ||
|
||
import type { BaseResource } from './resources/Base'; | ||
|
||
export const events = { | ||
TokenUpdate: 'token:update', | ||
UserSignOut: 'user:signOut', | ||
EnvironmentUpdate: 'environment:update', | ||
SessionTokenResolved: 'session:tokenResolved', | ||
ResourceUpdate: 'resource:update', | ||
ResourceError: 'resource:error', | ||
} as const; | ||
|
||
type TokenUpdatePayload = { token: TokenResource | null }; | ||
export type ResourceUpdatePayload = { resource: BaseResource }; | ||
export type ResourceErrorPayload = { resource: BaseResource; error: unknown }; | ||
|
||
type InternalEvents = { | ||
[events.TokenUpdate]: TokenUpdatePayload; | ||
[events.UserSignOut]: null; | ||
[events.EnvironmentUpdate]: null; | ||
[events.SessionTokenResolved]: null; | ||
[events.ResourceUpdate]: ResourceUpdatePayload; | ||
[events.ResourceError]: ResourceErrorPayload; | ||
}; | ||
|
||
export const eventBus = createEventBus<InternalEvents>(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { computed, signal } from 'alien-signals'; | ||
|
||
import type { SignIn } from './resources/SignIn'; | ||
|
||
export const signInSignal = signal<{ resource: SignIn | null }>({ resource: null }); | ||
export const signInErrorSignal = signal<{ errors: unknown }>({ errors: null }); | ||
|
||
export const signInComputedSignal = computed(() => { | ||
const signIn = signInSignal().resource; | ||
const errors = signInErrorSignal().errors; | ||
|
||
if (!signIn) { | ||
return { errors: null, signIn: null }; | ||
} | ||
|
||
return { errors, signIn: signIn.__internal_future }; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { State as StateInterface } from '@clerk/types'; | ||
import { computed, effect } from 'alien-signals'; | ||
|
||
import { eventBus } from './events'; | ||
import type { BaseResource } from './resources/Base'; | ||
import { SignIn } from './resources/SignIn'; | ||
import { signInComputedSignal, signInErrorSignal, signInSignal } from './signals'; | ||
|
||
export class State implements StateInterface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSDoc would be good here 👀 what's it for? Maybe it belongs on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put it on the |
||
signInResourceSignal = signInSignal; | ||
signInErrorSignal = signInErrorSignal; | ||
signInSignal = signInComputedSignal; | ||
|
||
__internal_effect = effect; | ||
__internal_computed = computed; | ||
|
||
constructor() { | ||
eventBus.on('resource:update', this.onResourceUpdated); | ||
eventBus.on('resource:error', this.onResourceError); | ||
} | ||
|
||
private onResourceError = (payload: { resource: BaseResource; error: unknown }) => { | ||
if (payload.resource instanceof SignIn) { | ||
this.signInErrorSignal({ errors: payload.error }); | ||
} | ||
}; | ||
|
||
private onResourceUpdated = (payload: { resource: BaseResource }) => { | ||
if (payload.resource instanceof SignIn) { | ||
this.signInResourceSignal({ resource: payload.resource }); | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { SignInFutureResource } from '@clerk/types'; | ||
import { useCallback, useSyncExternalStore } from 'react'; | ||
|
||
import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext'; | ||
import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider'; | ||
|
||
function useClerkSignal(signal: 'signIn'): { errors: unknown; signIn: SignInFutureResource | null } | null { | ||
useAssertWrappedByClerkProvider('useSignInSignal'); | ||
|
||
const clerk = useIsomorphicClerkContext(); | ||
|
||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
if (!clerk.loaded || !clerk.__internal_state) { | ||
return () => {}; | ||
} | ||
|
||
return clerk.__internal_state.__internal_effect(() => { | ||
switch (signal) { | ||
case 'signIn': | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- we know that the state is defined | ||
clerk.__internal_state!.signInSignal(); | ||
break; | ||
default: | ||
throw new Error(`Unknown signal: ${signal}`); | ||
} | ||
callback(); | ||
}); | ||
}, | ||
[clerk, clerk.loaded, clerk.__internal_state], | ||
); | ||
const getSnapshot = useCallback(() => { | ||
if (!clerk.__internal_state) { | ||
return null; | ||
} | ||
|
||
switch (signal) { | ||
case 'signIn': | ||
return clerk.__internal_state.signInSignal(); | ||
default: | ||
throw new Error(`Unknown signal: ${signal}`); | ||
} | ||
}, [clerk.__internal_state]); | ||
|
||
const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); | ||
|
||
return value; | ||
} | ||
|
||
export function useSignInSignal() { | ||
return useClerkSignal('signIn'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add JSDocs now or later to the methods here, but we should do it eventually! Saves us time later too 😄