@@ -12,15 +12,20 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
1212 private result : QueryObserverResult [ ]
1313 private queries : QueryObserverOptions [ ]
1414 private observers : QueryObserver [ ]
15+ private observersMap : Record < string , QueryObserver >
1516
1617 constructor ( client : QueryClient , queries ?: QueryObserverOptions [ ] ) {
1718 super ( )
1819
1920 this . client = client
20- this . queries = queries || [ ]
21+ this . queries = [ ]
2122 this . result = [ ]
2223 this . observers = [ ]
23- this . setQueries ( this . queries )
24+ this . observersMap = { }
25+
26+ if ( queries ) {
27+ this . setQueries ( queries )
28+ }
2429 }
2530
2631 protected onSubscribe ( ) : void {
@@ -59,66 +64,62 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
5964 }
6065
6166 getOptimisticResult ( queries : QueryObserverOptions [ ] ) : QueryObserverResult [ ] {
62- return queries . map ( ( options , i ) => {
63- let observer : QueryObserver | undefined = this . observers [ i ]
64-
67+ return queries . map ( options => {
6568 const defaultedOptions = this . client . defaultQueryObserverOptions ( options )
66-
67- if (
68- ! observer ||
69- observer . getCurrentQuery ( ) . queryHash !== defaultedOptions . queryHash
70- ) {
71- observer = this . observers . find (
72- x => x . getCurrentQuery ( ) . queryHash === defaultedOptions . queryHash
73- )
74- }
75-
76- if ( ! observer ) {
77- observer = new QueryObserver ( this . client , defaultedOptions )
78- }
79-
80- return observer . getOptimisticResult ( defaultedOptions )
69+ return this . getObserver ( defaultedOptions ) . getOptimisticResult (
70+ defaultedOptions
71+ )
8172 } )
8273 }
8374
75+ private getObserver ( options : QueryObserverOptions ) : QueryObserver {
76+ const defaultedOptions = this . client . defaultQueryObserverOptions ( options )
77+ return (
78+ this . observersMap [ defaultedOptions . queryHash ! ] ||
79+ new QueryObserver ( this . client , defaultedOptions )
80+ )
81+ }
82+
8483 private updateObservers ( notifyOptions ?: NotifyOptions ) : void {
8584 notifyManager . batch ( ( ) => {
8685 let hasIndexChange = false
8786
8887 const prevObservers = this . observers
89- const newObservers = this . queries . map ( ( options , i ) => {
90- let observer : QueryObserver | undefined = prevObservers [ i ]
88+ const prevOberversMap = this . observersMap
89+
90+ const newResult : QueryObserverResult [ ] = [ ]
91+ const newObservers : QueryObserver [ ] = [ ]
92+ const newObserversMap : Record < string , QueryObserver > = { }
9193
94+ this . queries . forEach ( ( options , i ) => {
9295 const defaultedOptions = this . client . defaultQueryObserverOptions (
9396 options
9497 )
98+ const queryHash = defaultedOptions . queryHash !
99+ const observer = this . getObserver ( defaultedOptions )
95100
96- if (
97- ! observer ||
98- observer . getCurrentQuery ( ) . queryHash !== defaultedOptions . queryHash
99- ) {
100- hasIndexChange = true
101- observer = prevObservers . find (
102- x => x . getCurrentQuery ( ) . queryHash === defaultedOptions . queryHash
103- )
101+ if ( prevOberversMap [ queryHash ] ) {
102+ observer . setOptions ( defaultedOptions , notifyOptions )
104103 }
105104
106- if ( observer ) {
107- observer . setOptions ( defaultedOptions , notifyOptions )
108- return observer
105+ if ( observer !== prevObservers [ i ] ) {
106+ hasIndexChange = true
109107 }
110108
111- return new QueryObserver ( this . client , defaultedOptions )
109+ newObservers . push ( observer )
110+ newResult . push ( observer . getCurrentResult ( ) )
111+ newObserversMap [ queryHash ] = observer
112112 } )
113113
114114 if ( prevObservers . length === newObservers . length && ! hasIndexChange ) {
115115 return
116116 }
117117
118118 this . observers = newObservers
119- this . result = newObservers . map ( observer => observer . getCurrentResult ( ) )
119+ this . observersMap = newObserversMap
120+ this . result = newResult
120121
121- if ( ! this . listeners . length ) {
122+ if ( ! this . hasListeners ( ) ) {
122123 return
123124 }
124125
0 commit comments