Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 469
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
415f283fbd1f032d00936e71897c66f9a5545e360757ef049a3614d70ed14b6d82111c253bd9cbd5b8e618d4b5763c4a451bd10051e3bf21e08b6aa37f2eca3ee49117c828e4bc8457bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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 { | ||
Member 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 MemberAuthor 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'); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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 😄