-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(stage-ui,server): use optimistic request for provider catalog #951
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 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
363d1f8
feat(stage-ui, server): use optimistic request for provider catalog
luoling8192 dce6b07
chore: type
luoling8192 219a716
Update packages/stage-ui/src/composables/use-optimistic.ts
luoling8192 328344f
feat: lazy
luoling8192 9fca185
Update packages/stage-ui/src/composables/use-optimistic.ts
nekomeowww 5f131de
Merge branch 'main' into dev/use-optimistic
luoling8192 c2bc128
Merge branch 'main' into dev/use-optimistic
nekomeowww e8d78ce
chore: rename
luoling8192 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
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
123 changes: 123 additions & 0 deletions
123
packages/stage-ui/src/composables/use-optimistic.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,123 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { nextTick, ref } from 'vue' | ||
|
|
||
| import { useOptimistic } from './use-optimistic' | ||
|
|
||
| describe('useOptimistic', () => { | ||
| it('should perform a successful optimistic update', async () => { | ||
| const state = ref('initial') | ||
| const actionResult = 'real-data' | ||
|
|
||
| const apply = vi.fn(() => { | ||
| const old = state.value | ||
| state.value = 'optimistic' | ||
| return () => { | ||
| state.value = old | ||
| } | ||
| }) | ||
|
|
||
| const action = vi.fn(async () => { | ||
| return actionResult | ||
| }) | ||
|
|
||
| const onSuccess = vi.fn((result: string) => { | ||
| state.value = `final-${result}` | ||
| return state.value | ||
| }) | ||
|
|
||
| const { state: resultState, isLoading } = useOptimistic({ | ||
| apply, | ||
| action, | ||
| onSuccess, | ||
| }) | ||
|
|
||
| // Immediate check | ||
| expect(state.value).toBe('optimistic') | ||
| expect(apply).toHaveBeenCalled() | ||
|
|
||
| // Wait for action to complete | ||
| await nextTick() | ||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
|
||
| expect(action).toHaveBeenCalled() | ||
| expect(onSuccess).toHaveBeenCalledWith(actionResult) | ||
| expect(state.value).toBe('final-real-data') | ||
| expect(resultState.value).toBe('final-real-data') | ||
| expect(isLoading.value).toBe(false) | ||
| }) | ||
|
|
||
| it('should rollback on action failure', async () => { | ||
| const state = ref('initial') | ||
| const error = new Error('action failed') | ||
|
|
||
| const rollback = vi.fn(() => { | ||
| state.value = 'initial' | ||
| }) | ||
|
|
||
| const apply = vi.fn(() => { | ||
| state.value = 'optimistic' | ||
| return rollback | ||
| }) | ||
|
|
||
| const action = vi.fn(async () => { | ||
| throw error | ||
| }) | ||
|
|
||
| const { error: errorState, isLoading } = useOptimistic({ | ||
| apply, | ||
| action, | ||
| }) | ||
|
|
||
| expect(state.value).toBe('optimistic') | ||
|
|
||
| // Wait for failure | ||
| await nextTick() | ||
| await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
|
||
| expect(rollback).toHaveBeenCalled() | ||
| expect(state.value).toBe('initial') | ||
| expect(errorState.value).toBe(error) | ||
| expect(isLoading.value).toBe(false) | ||
| }) | ||
|
|
||
| it('should handle async apply and rollback', async () => { | ||
| const state = ref('initial') | ||
|
|
||
| const apply = async () => { | ||
| await new Promise(resolve => setTimeout(resolve, 10)) | ||
| state.value = 'optimistic' | ||
| return async () => { | ||
| await new Promise(resolve => setTimeout(resolve, 10)) | ||
| state.value = 'initial' | ||
| } | ||
| } | ||
|
|
||
| const action = async () => { | ||
| throw new Error('fail') | ||
| } | ||
|
|
||
| const { execute } = useOptimistic({ | ||
| apply, | ||
| action, | ||
| }) | ||
|
|
||
| await execute() | ||
|
|
||
| expect(state.value).toBe('initial') | ||
| }) | ||
|
|
||
| it('should not throw if apply returns non-function', async () => { | ||
| const action = vi.fn(async () => { | ||
| throw new Error('fail') | ||
| }) | ||
|
|
||
| const { execute, error } = useOptimistic({ | ||
| // @ts-expect-error - testing invalid return | ||
| apply: () => null, | ||
| action, | ||
| }) | ||
|
|
||
| await execute() | ||
| expect(error.value).toBeDefined() | ||
| }) | ||
| }) |
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,50 @@ | ||
| import { useAsyncState } from './use-async-state' | ||
|
|
||
| export interface UseOptimisticOptions<T, R> { | ||
| /** | ||
| * The optimistic update logic. | ||
| * Should return a rollback function. | ||
| */ | ||
| apply: () => Promise<(() => Promise<void> | void)> | (() => Promise<void> | void) | ||
| /** | ||
| * The actual async task (e.g., API call). | ||
| */ | ||
| action: () => Promise<T> | ||
| /** | ||
| * Optional callback after successful action to refine state (e.g., replacing temp IDs). | ||
| */ | ||
| onSuccess?: (result: T) => Promise<R> | R | ||
| /** | ||
| * Optional callback on error. Rollback is handled automatically. | ||
| */ | ||
| onError?: (error: unknown) => void | Promise<void> | ||
| } | ||
|
|
||
| /** | ||
| * A wrapper for performing optimistic updates with automatic rollback. | ||
| * Integrates with useAsyncState for loading/error tracking. | ||
| */ | ||
| export function useOptimistic<T, R = void>(options: UseOptimisticOptions<T, R>) { | ||
| const { apply, action, onSuccess, onError } = options | ||
|
|
||
| return useAsyncState(async () => { | ||
| const rollback = await apply() | ||
|
|
||
| try { | ||
| const result = await action() | ||
| if (onSuccess) { | ||
| return await onSuccess(result) | ||
| } | ||
| return result as unknown as R | ||
| } | ||
| catch (err) { | ||
| if (typeof rollback === 'function') { | ||
| await rollback() | ||
| } | ||
| if (onError) { | ||
| await onError(err) | ||
| } | ||
| throw err | ||
| } | ||
| }, { immediate: true }) | ||
luoling8192 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.