Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/i18n/src/locales/vi/tamagotchi/electron/tray.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ menu:
open_caption: Mở phụ đề...
caption_overlay: Lớp phủ phụ đề
follow_window: Theo cửa sổ
reset_position: Đặt lại vị trí
reset_position: Đặt lại vị trí
devtools: Công cụ phát triển
troubleshoot_beatsync: Sửa lỗi BeatSync...
quit: Thoát ra
27 changes: 27 additions & 0 deletions packages/stage-ui/src/composables/use-optimistic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,31 @@ describe('useOptimistic', () => {
await execute()
expect(error.value).toBeDefined()
})

it('should skip without applying optimistic state', async () => {
const state = ref('initial')
const apply = vi.fn(() => {
state.value = 'optimistic'
return () => {
state.value = 'initial'
}
})
const action = vi.fn(async () => 'should-not-run')
const skipActionIf = vi.fn(() => true)

const { execute, state: resultState } = useOptimisticMutation({
apply,
action,
skipActionIf,
lazy: true,
})

await execute()

expect(skipActionIf).toHaveBeenCalled()
expect(apply).not.toHaveBeenCalled()
expect(action).not.toHaveBeenCalled()
expect(state.value).toBe('initial')
expect(resultState.value).toBeUndefined()
})
})
3 changes: 2 additions & 1 deletion packages/stage-ui/src/composables/use-optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ export function useOptimisticMutation<T, R = T, E = unknown>(options: UseOptimis
} = options

return useAsyncState(async () => {
const rollback = await apply()
if (skipActionIf && await skipActionIf()) {
return undefined as R
}

const rollback = await apply()

try {
const result = await action()
if (onSuccess) {
Expand Down
Loading