1- import { onWatcherCleanup , shallowRef , watchEffect } from "vue"
1+ import { computed , onScopeDispose , shallowRef } from "vue"
22import { compileQuery , queryBuilder } from "@tanstack/db"
33import { shallow } from "./useStore"
44import type {
@@ -23,51 +23,61 @@ export function useLiveQuery<
2323 q : InitialQueryBuilder < Context < Schema > >
2424 ) => QueryBuilder < TResultContext >
2525) : UseLiveQueryReturn < ResultsFromContext < TResultContext > > {
26- const results = shallowRef ( )
27- const state = shallowRef ( )
28- const data = shallowRef ( )
26+ const NOOP = ( ) => { }
27+ let unsubCompiled = NOOP
28+ let unsubDerivedState = NOOP
29+ let unsubDerivedArray = NOOP
2930
30- watchEffect ( ( ) => {
31- const query = queryFn ( queryBuilder ( ) )
32- const compiled = compileQuery ( query )
33- compiled . start ( )
31+ const compiled = computed < ReturnType < typeof compileQuery < TResultContext > > > (
32+ ( ) => {
33+ unsubCompiled ( )
34+ const compiledRef = compileQuery ( queryFn ( queryBuilder ( ) ) )
35+ unsubCompiled = compiledRef . start ( )
36+ return compiledRef
37+ }
38+ )
3439
35- const resultsRef = compiled . results
36- results . value = resultsRef
37-
38- const derivedState = resultsRef . derivedState
39- const derivedArray = resultsRef . derivedArray
40+ const state = computed ( ( ) => {
41+ const derivedState = compiled . value . results . derivedState
4042 let stateRef = derivedState . state
41- let dataRef = derivedArray . state
42- state . value = stateRef
43- data . value = dataRef
43+ const ret = shallowRef ( stateRef )
4444
45- const unsubDerivedState = derivedState . subscribe ( ( ) => {
45+ unsubDerivedState ( )
46+ unsubDerivedState = derivedState . subscribe ( ( ) => {
4647 const newValue = derivedState . state
4748 if ( shallow ( stateRef , newValue ) ) return
4849
4950 stateRef = newValue
50- state . value = newValue
51+ ret . value = newValue
5152 } )
53+ return ret
54+ } )
5255
53- const unsubDerivedArray = derivedArray . subscribe ( ( ) => {
56+ const data = computed ( ( ) => {
57+ const derivedArray = compiled . value . results . derivedArray
58+ let stateRef = derivedArray . state
59+ const ret = shallowRef ( stateRef )
60+
61+ unsubDerivedArray ( )
62+ unsubDerivedArray = derivedArray . subscribe ( ( ) => {
5463 const newValue = derivedArray . state
55- if ( shallow ( dataRef , newValue ) ) return
64+ if ( shallow ( stateRef , newValue ) ) return
5665
57- dataRef = newValue
58- data . value = newValue
66+ stateRef = newValue
67+ ret . value = newValue
5968 } )
69+ return ret
70+ } )
6071
61- onWatcherCleanup ( ( ) => {
62- compiled . stop ( )
63- unsubDerivedState ( )
64- unsubDerivedArray ( )
65- } )
72+ onScopeDispose ( ( ) => {
73+ unsubCompiled ( )
74+ unsubDerivedState ( )
75+ unsubDerivedArray ( )
6676 } )
6777
6878 return {
69- state : ( ) => state . value ,
70- data : ( ) => data . value ,
71- collection : ( ) => results . value ,
79+ state : ( ) => state . value . value ,
80+ data : ( ) => data . value . value ,
81+ collection : ( ) => compiled . value . results ,
7282 }
7383}
0 commit comments