-
Notifications
You must be signed in to change notification settings - Fork 454
fix(vue): Prevent TDZ error in watch callbacks #7743
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dccbf5e
fix(vue): prevent TDZ error in watch callbacks
wobsoriano ab92883
chore: add changeset
wobsoriano fec12a5
chore: add conditional watch with manual stop
wobsoriano e89f525
add unit test
wobsoriano c664395
Merge branch 'main' into rob/prevent-tdz-error
wobsoriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/vue": patch | ||
| --- | ||
|
|
||
| Fixed an error occurring in the composables where watchers attempted to call unwatch() within their own initialization. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/vue/src/utils/__tests__/useClerkLoaded.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import type { Clerk } from '@clerk/shared/types'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { nextTick, ref, type ShallowRef } from 'vue'; | ||
|
|
||
| import * as composables from '../../composables'; | ||
| import { useClerkLoaded } from '../useClerkLoaded'; | ||
|
|
||
| // Mock the useClerk composable | ||
| vi.mock('../../composables', () => ({ | ||
| useClerk: vi.fn(), | ||
| })); | ||
|
|
||
| describe('useClerkLoaded', () => { | ||
| let clerkRef: ShallowRef<Clerk | null>; | ||
| let mockClerk: Partial<Clerk>; | ||
|
|
||
| beforeEach(() => { | ||
| clerkRef = ref(null) as ShallowRef<Clerk | null>; | ||
| mockClerk = { | ||
| loaded: false, | ||
| }; | ||
| vi.mocked(composables.useClerk).mockReturnValue(clerkRef); | ||
| }); | ||
|
|
||
| it('should not call callback when clerk is null', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(callback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not call callback when clerk exists but not loaded', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| // Set clerk instance but not loaded | ||
| clerkRef.value = { ...mockClerk, loaded: false } as Clerk; | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(callback).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call callback when clerk becomes loaded', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| // Set clerk instance as loaded | ||
| const loadedClerk = { ...mockClerk, loaded: true } as Clerk; | ||
| clerkRef.value = loadedClerk; | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(callback).toHaveBeenCalledOnce(); | ||
| expect(callback).toHaveBeenCalledWith(loadedClerk); | ||
| }); | ||
|
|
||
| it('should call callback immediately if clerk is already loaded', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Set clerk instance as loaded before calling useClerkLoaded | ||
| const loadedClerk = { ...mockClerk, loaded: true } as Clerk; | ||
| clerkRef.value = loadedClerk; | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(callback).toHaveBeenCalledOnce(); | ||
| expect(callback).toHaveBeenCalledWith(loadedClerk); | ||
| }); | ||
|
|
||
| it('should only call callback once even if clerk updates multiple times', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| // Set clerk instance as loaded | ||
| const loadedClerk1 = { ...mockClerk, loaded: true, client: { id: 'client_1' } } as Clerk; | ||
| clerkRef.value = loadedClerk1; | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(callback).toHaveBeenCalledOnce(); | ||
|
|
||
| // Update clerk instance again (simulating a resource update) | ||
| const loadedClerk2 = { ...mockClerk, loaded: true, client: { id: 'client_2' } } as Clerk; | ||
| clerkRef.value = loadedClerk2; | ||
|
|
||
| await nextTick(); | ||
|
|
||
| // Should still only be called once due to unwatch() | ||
| expect(callback).toHaveBeenCalledOnce(); | ||
| expect(callback).toHaveBeenCalledWith(loadedClerk1); | ||
| }); | ||
|
|
||
| it('should handle transition from null -> not loaded -> loaded', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| // Initial state: null | ||
| expect(callback).not.toHaveBeenCalled(); | ||
|
|
||
| // Clerk instance created but not loaded | ||
| clerkRef.value = { ...mockClerk, loaded: false } as Clerk; | ||
| await nextTick(); | ||
| expect(callback).not.toHaveBeenCalled(); | ||
|
|
||
| // Clerk becomes loaded | ||
| clerkRef.value = { ...mockClerk, loaded: true } as Clerk; | ||
| await nextTick(); | ||
|
|
||
| expect(callback).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('should properly clean up watcher after callback is called', async () => { | ||
| const callback = vi.fn(); | ||
|
|
||
| // Call useClerkLoaded | ||
| useClerkLoaded(callback); | ||
|
|
||
| // Set clerk as loaded | ||
| const loadedClerk = { ...mockClerk, loaded: true } as Clerk; | ||
| clerkRef.value = loadedClerk; | ||
|
|
||
| await nextTick(); | ||
|
|
||
| expect(callback).toHaveBeenCalledOnce(); | ||
|
|
||
| // Simulate multiple updates (watcher should be cleaned up) | ||
| for (let i = 0; i < 5; i++) { | ||
| clerkRef.value = { ...mockClerk, loaded: true, session: { id: `sess_${i}` } } as Clerk; | ||
| await nextTick(); | ||
| } | ||
|
|
||
| // Should still only be called once | ||
| expect(callback).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,17 @@ import { useClerk } from '../composables'; | |
| export const useClerkLoaded = (callback: (clerk: LoadedClerk) => void) => { | ||
| const clerk = useClerk(); | ||
|
|
||
| watch( | ||
| let unwatch: (() => void) | undefined; | ||
| // eslint-disable-next-line prefer-const | ||
| unwatch = watch( | ||
| clerk, | ||
| unwrappedClerk => { | ||
| if (!unwrappedClerk?.loaded) { | ||
| return; | ||
| } | ||
|
|
||
| callback(unwrappedClerk as LoadedClerk); | ||
| unwatch?.(); | ||
| }, | ||
| { immediate: true }, | ||
|
Comment on lines
+20
to
32
Member
Author
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. We are not using the |
||
| ); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Q: Why not use the
useClerkLoadedhelper?A: Telemetry can be recorded as soon as the clerk instance exists. We don't have to wait for
Clerkto beloaded