Skip to content
Closed
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
3 changes: 3 additions & 0 deletions packages/svelte-query/src/containers.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export function createRawRef<T extends {} | Array<unknown>>(
const existingKeys = Object.keys(out)
const newKeys = Object.keys(newValue)
const keysToRemove = existingKeys.filter((key) => !newKeys.includes(key))
if (Array.isArray(out)) {
keysToRemove.sort((a, b) => Number(b) - Number(a))
}
for (const key of keysToRemove) {
// @ts-expect-error
delete out[key]
Expand Down
7 changes: 7 additions & 0 deletions packages/svelte-query/tests/containers.svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ describe('createRawRef', () => {
expect(ref).toEqual([7, 8, 9])
})

it('should handle removing multiple array items in one update', () => {
const [ref, update] = createRawRef([1, 2, 3, 4])

update([5, 6])
expect(ref).toEqual([5, 6])
})

it('should behave like a regular object when not using `update`', () => {
const [ref] = createRawRef<Record<string, unknown>>({ a: 1, b: 2 })

Expand Down
30 changes: 29 additions & 1 deletion packages/svelte-query/tests/createQueries.svelte.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, expectTypeOf, it, vi } from 'vitest'
import { QueryClient, createQueries } from '../src/index.js'
import { promiseWithResolvers, withEffectRoot } from './utils.svelte.js'
import { promiseWithResolvers, ref, withEffectRoot } from './utils.svelte.js'
import type {
CreateQueryOptions,
CreateQueryResult,
Expand Down Expand Up @@ -63,6 +63,34 @@ describe('createQueries', () => {
}),
)

it(
'should support removing multiple queries in one update',
withEffectRoot(async () => {
const queryIds = ref([1, 2, 3, 4])

const result = createQueries(
() => ({
queries: queryIds.value.map((id) => ({
queryKey: ['multi-remove', id] as const,
queryFn: () => Promise.resolve(id),
})),
}),
() => queryClient,
)

await vi.waitFor(() =>
expect(result.map((query) => query.data)).toEqual([1, 2, 3, 4]),
)

queryIds.value = [1, 2]

await vi.waitFor(() => expect(result).toHaveLength(2))
await vi.waitFor(() =>
expect(result.map((query) => query.data)).toEqual([1, 2]),
)
}),
)

it(
'handles type parameter - tuple of tuples',
withEffectRoot(() => {
Expand Down
Loading