-
Notifications
You must be signed in to change notification settings - Fork 402
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 2 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 |
|---|---|---|
| @@ -1,20 +1,25 @@ | ||
| import { createEventBus } from '@clerk/shared/eventBus'; | ||
| import type { TokenResource } from '@clerk/types'; | ||
|
|
||
| import type { SignIn } from './resources'; | ||
|
|
||
| export const events = { | ||
| TokenUpdate: 'token:update', | ||
| UserSignOut: 'user:signOut', | ||
| EnvironmentUpdate: 'environment:update', | ||
| SessionTokenResolved: 'session:tokenResolved', | ||
| SignInUpdate: 'signin:update', | ||
dstaley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } as const; | ||
|
|
||
| type TokenUpdatePayload = { token: TokenResource | null }; | ||
| export type SignInUpdatePayload = { resource: SignIn }; | ||
dstaley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| type InternalEvents = { | ||
| [events.TokenUpdate]: TokenUpdatePayload; | ||
| [events.UserSignOut]: null; | ||
| [events.EnvironmentUpdate]: null; | ||
| [events.SessionTokenResolved]: null; | ||
| [events.SignInUpdate]: SignInUpdatePayload; | ||
| }; | ||
|
|
||
| export const eventBus = createEventBus<InternalEvents>(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { signal } from 'alien-signals'; | ||
|
|
||
| import type { SignIn } from './resources/SignIn'; | ||
|
|
||
| export const signInSignal = signal<{ resource: SignIn | null }>({ resource: null }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type { State as StateInterface } from '@clerk/types'; | ||
| import { computed, effect } from 'alien-signals'; | ||
|
|
||
| import { eventBus, type SignInUpdatePayload } from './events'; | ||
| import { 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 |
||
| signInSignal = signInSignal; | ||
|
|
||
| __internal_effect = effect; | ||
| __internal_computed = computed; | ||
|
|
||
| constructor() { | ||
| eventBus.on('signin:update', this.onSignInUpdated); | ||
| } | ||
|
|
||
| private onSignInUpdated = (payload: SignInUpdatePayload) => { | ||
| this.signInSignal({ resource: payload.resource }); | ||
| }; | ||
dstaley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { SignInResource } from '@clerk/types'; | ||
| import { useCallback, useSyncExternalStore } from 'react'; | ||
|
|
||
| import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext'; | ||
| import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider'; | ||
|
|
||
| function useClerkSignal(signal: 'signIn'): { isLoaded: boolean; signIn: SignInResource | null } { | ||
dstaley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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().resource; | ||
| default: | ||
| throw new Error(`Unknown signal: ${signal}`); | ||
| } | ||
| }, [clerk.__internal_state]); | ||
|
|
||
| const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); | ||
|
|
||
| return { | ||
| isLoaded: clerk.loaded, | ||
| [signal]: value, | ||
| }; | ||
| } | ||
|
|
||
| export function useSignInSignal() { | ||
| return useClerkSignal('signIn'); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import type { SignInResource } from './signIn'; | ||
|
|
||
| export interface State { | ||
| signInSignal: { | ||
| (): { | ||
| resource: SignInResource | null; | ||
| }; | ||
| (value: { resource: SignInResource | null }): void; | ||
| }; | ||
| __internal_effect: (callback: () => void) => () => void; | ||
| __internal_computed: <T>(getter: (previousValue?: T) => T) => () => T; | ||
dstaley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
dstaley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.