Skip to content

Commit fec1a0d

Browse files
chore: Improve reactive containers
1 parent cd2d81f commit fec1a0d

File tree

3 files changed

+63
-39
lines changed

3 files changed

+63
-39
lines changed

packages/svelte-query/src/containers.svelte.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,36 @@ import { createSubscriber } from 'svelte/reactivity'
22

33
type VoidFn = () => void
44
type Subscriber = (update: VoidFn) => void | VoidFn
5+
type Effect =
6+
| { type: 'pre'; fn: Subscriber }
7+
| { type: 'regular'; fn: Subscriber }
58

69
export class ReactiveValue<T> {
710
#fn
811
#subscribe
912

10-
constructor(fn: () => T, onSubscribe: Subscriber) {
13+
constructor(
14+
fn: () => T,
15+
onSubscribe: Subscriber,
16+
effects: Array<Effect> = [],
17+
) {
1118
this.#fn = fn
12-
this.#subscribe = createSubscriber(onSubscribe)
19+
this.#subscribe = createSubscriber((update) => {
20+
const cleanup = $effect.root(() => {
21+
for (const effect of effects) {
22+
if (effect.type === 'pre') {
23+
$effect.pre(() => effect.fn(update))
24+
} else {
25+
$effect(() => effect.fn(update))
26+
}
27+
}
28+
})
29+
const off = onSubscribe(update)
30+
return () => {
31+
cleanup()
32+
off?.()
33+
}
34+
})
1335
}
1436

1537
get current() {
@@ -18,7 +40,11 @@ export class ReactiveValue<T> {
1840
}
1941
}
2042

21-
export function createReactiveThunk<T>(fn: () => T, onSubscribe: Subscriber) {
22-
const reactiveValue = new ReactiveValue(fn, onSubscribe)
43+
export function createReactiveThunk<T>(
44+
fn: () => T,
45+
onSubscribe: Subscriber,
46+
effects?: Array<Effect>,
47+
) {
48+
const reactiveValue = new ReactiveValue(fn, onSubscribe, effects)
2349
return () => reactiveValue.current
2450
}

packages/svelte-query/src/createBaseQuery.svelte.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export function createBaseQuery<
3333
return opts
3434
})
3535

36-
let updateEffects = () => {}
37-
3836
/** Creates the observer */
3937
const observer = new Observer<
4038
TQueryFnData,
@@ -44,12 +42,6 @@ export function createBaseQuery<
4442
TQueryKey
4543
>(client, resolvedOptions)
4644

47-
/** Subscribe to changes in result and defaultedOptions */
48-
$effect.pre(() => {
49-
observer.setOptions(resolvedOptions, { listeners: false })
50-
updateEffects()
51-
})
52-
5345
return createReactiveThunk(
5446
() => {
5547
const result = observer.getOptimisticResult(resolvedOptions)
@@ -58,9 +50,15 @@ export function createBaseQuery<
5850
}
5951
return result
6052
},
61-
(update) => {
62-
updateEffects = update
63-
return observer.subscribe(update)
64-
},
53+
(update) => observer.subscribe(update),
54+
[
55+
{
56+
type: 'pre',
57+
fn: (update) => {
58+
observer.setOptions(resolvedOptions, { listeners: false })
59+
update()
60+
},
61+
},
62+
],
6563
)
6664
}

packages/svelte-query/src/createQueries.svelte.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -218,26 +218,6 @@ export function createQueries<
218218
combine as QueriesObserverOptions<TCombinedResult>,
219219
)
220220

221-
let updateEffects = () => {}
222-
223-
$effect.pre(() => {
224-
// Do not notify on updates because of changes in the options because
225-
// these changes should already be reflected in the optimistic result.
226-
observer.setQueries(
227-
resolvedQueries,
228-
{ combine } as QueriesObserverOptions<TCombinedResult>,
229-
{ listeners: false },
230-
)
231-
updateEffects()
232-
})
233-
234-
$effect(() => {
235-
if (isRestoring || subscribed === false) {
236-
return
237-
}
238-
observer.subscribe(updateEffects)
239-
})
240-
241221
return createReactiveThunk(
242222
() => {
243223
const [_, getCombinedResult, trackResult] = observer.getOptimisticResult(
@@ -246,8 +226,28 @@ export function createQueries<
246226
)
247227
return getCombinedResult(trackResult())
248228
},
249-
(update) => {
250-
updateEffects = update
251-
},
229+
() => {},
230+
[
231+
{
232+
type: 'pre',
233+
fn: (update) => {
234+
observer.setQueries(
235+
resolvedQueries,
236+
{ combine } as QueriesObserverOptions<TCombinedResult>,
237+
{ listeners: false },
238+
)
239+
update()
240+
},
241+
},
242+
{
243+
type: 'regular',
244+
fn: (update) => {
245+
if (isRestoring || subscribed === false) {
246+
return
247+
}
248+
observer.subscribe(update)
249+
},
250+
},
251+
],
252252
)
253253
}

0 commit comments

Comments
 (0)