Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createCacheSubscriptionRegistry.test.ts

38 lines1.1 KB

1import { expect, it } from 'vitest'
2import { createRoot, createSignal } from 'solid-js'
3import { createCacheSubscriptionRegistry } from '../createCacheSubscriptionRegistry'
4​
5it('should skip disposed subscriptions without dropping other queued updates', () => {
6 const registry = createCacheSubscriptionRegistry<number, undefined>()
7 const queued: Array<() => void> = []
8 const [firstValue, setFirstValue] = createSignal(0)
9 const [secondValue, setSecondValue] = createSignal(0)
10 const firstDispose = createRoot((dispose) => {
11 registry.register((cache) => cache(), setFirstValue)
12 return dispose
13 })
14 const secondDispose = createRoot((dispose) => {
15 registry.register((cache) => cache(), setSecondValue)
16 return dispose
17 })
18​
19 try {
20 registry.notify(
21 () => 1,
22 undefined,
23 (update) => queued.push(update),
24 )
25 firstDispose()
26 queued.forEach((update) => update())
27​
28 expect(firstValue()).toBe(0)
29 expect(secondValue()).toBe(1)
30​
31 registry.notify(() => 2, undefined)
32 expect(firstValue()).toBe(0)
33 expect(secondValue()).toBe(2)
34 } finally {
35 firstDispose()
36 secondDispose()
37 }
38})