@@ -52,7 +52,7 @@ function getRefSerializer<Data, Variables>(
5252 ...queryRef . dataConnect . getSettings ( )
5353 }
5454 } ,
55- fetchTime : Date . now ( ) . toLocaleString ( ) ,
55+ fetchTime : Date . now ( ) . toLocaleString ( ) , // TODO: Fix the fetch time here.
5656 source
5757 } ;
5858 } ;
@@ -109,17 +109,13 @@ export class QueryManager {
109109 refType : QUERY_STR
110110 } ) ;
111111 const unsubscribe = ( ) : void => {
112- if ( ! this . callbacks . has ( key ) ) {
112+ if ( this . callbacks . has ( key ) ) {
113113 const callbackList = this . callbacks . get ( key ) ! ;
114- callbackList . forEach ( subscription => {
115- subscription . unsubscribe ( ) ;
116- } ) ;
117- this . callbacks . set ( key , [ ] ) ;
114+ this . callbacks . set ( key , callbackList . filter ( callback => callback . userCallback !== onResultCallback ) ) ;
118115 }
119116 } ;
120117
121118 if ( initialCache ) {
122- // TODO: The type might be wrong here
123119 this . updateSSR ( initialCache as QueryResult < Data , Variables > ) ;
124120 }
125121
@@ -161,21 +157,19 @@ export class QueryManager {
161157 variables : queryRef . variables ,
162158 refType : QUERY_STR
163159 } ) ;
164- // TODO: It seems like the cache isn't loading the data correctly.
165- // TODO: It seems like cache loading is async. That needs to be fixed.
166160 if (
167161 options ?. fetchPolicy !== QueryFetchPolicy . SERVER_ONLY &&
168162 ( await this . cache . containsResultTree ( key ) ) &&
169163 ! ( await this . cache . getResultTree ( key ) ) . isStale ( )
170164 ) {
171165 const cacheResult : Data = JSON . parse ( await this . cache . getResultJSON ( key ) ) ;
166+ const resultTree = await this . cache . getResultTree ( key ) ;
172167 const result : QueryResult < Data , Variables > = {
173- ...cacheResult ,
174168 source : SOURCE_CACHE ,
175169 ref : queryRef ,
176170 data : cacheResult ,
177171 toJSON : getRefSerializer ( queryRef , cacheResult , SOURCE_CACHE ) ,
178- fetchTime : new Date ( ) . toISOString ( )
172+ fetchTime : resultTree . cachedAt . toString ( ) ,
179173 } ;
180174 ( await this . cache . getResultTree ( key ) ) . updateAccessed ( ) ;
181175 logDebug (
@@ -207,18 +201,13 @@ export class QueryManager {
207201 ) ;
208202 const fetchTime = new Date ( ) . toString ( ) ;
209203 const result : QueryResult < Data , Variables > = {
210- ... res ,
204+ data : res . data ,
211205 source : SOURCE_SERVER ,
212206 ref : queryRef ,
213207 toJSON : getRefSerializer ( queryRef , res . data , SOURCE_SERVER ) ,
214208 fetchTime
215209 } ;
216- const subscribers = this . callbacks . get ( key ) ;
217- if ( subscribers !== undefined ) {
218- subscribers . forEach ( subscription => {
219- subscription . userCallback ( result ) ;
220- } ) ;
221- }
210+
222211 if ( await this . cache . containsResultTree ( key ) ) {
223212 ( await this . cache . getResultTree ( key ) ) . updateAccessed ( ) ;
224213 }
0 commit comments