@@ -266,6 +266,7 @@ export function subscribeToFindMany<const S extends AnyNoContext>(
266266 const getEntities = ( ) => {
267267 const cacheEntry = decodedEntitiesCache . get ( typeName ) ;
268268 if ( ! cacheEntry ) return stableEmptyArray ;
269+
269270 const query = cacheEntry . queries . get ( queryKey ) ;
270271 if ( ! query ) return stableEmptyArray ;
271272
@@ -292,58 +293,62 @@ export function subscribeToFindMany<const S extends AnyNoContext>(
292293 return query . data ;
293294 } ;
294295
295- if ( ! decodedEntitiesCache . has ( typeName ) ) {
296- const { entities } = findMany ( handle , type ) ;
297- const entitiesMap = new Map ( ) ;
298- for ( const entity of entities ) {
299- entitiesMap . set ( entity . id , entity ) ;
296+ const allTypes = new Set < S > ( ) ;
297+ for ( const [ _key , field ] of Object . entries ( type . fields ) ) {
298+ if ( isReferenceField ( field ) ) {
299+ allTypes . add ( field as S ) ;
300300 }
301+ }
301302
302- const queries = new Map < string , QueryEntry > ( ) ;
303-
304- queries . set ( queryKey , {
305- data : [ ...entities ] ,
306- listeners : [ ] ,
307- isInvalidated : false ,
308- } ) ;
309-
310- const cacheEntry : DecodedEntitiesCacheEntry = {
311- decoder : decode ,
312- type,
313- entities : entitiesMap ,
314- queries,
315- isInvalidated : false ,
316- } ;
317-
318- decodedEntitiesCache . set ( typeName , cacheEntry ) ;
303+ const subscribe = ( callback : ( ) => void ) => {
304+ let cacheEntry = decodedEntitiesCache . get ( typeName ) ;
305+
306+ if ( ! cacheEntry ) {
307+ console . log ( 'setup type' , typeName ) ;
308+ const { entities } = findMany ( handle , type ) ;
309+ const entitiesMap = new Map ( ) ;
310+ for ( const entity of entities ) {
311+ entitiesMap . set ( entity . id , entity ) ;
312+ }
319313
320- for ( const entity of entities ) {
321- for ( const [ , value ] of Object . entries ( entity ) ) {
322- if ( Array . isArray ( value ) ) {
323- for ( const relationEntity of value ) {
324- let relationParentEntry = entityRelationParentsMap . get ( relationEntity . id ) ;
325- if ( relationParentEntry ) {
326- relationParentEntry . set ( cacheEntry , ( relationParentEntry . get ( cacheEntry ) ?? 0 ) + 1 ) ;
327- } else {
328- relationParentEntry = new Map ( ) ;
329- entityRelationParentsMap . set ( relationEntity . id , relationParentEntry ) ;
330- relationParentEntry . set ( cacheEntry , 1 ) ;
314+ const queries = new Map < string , QueryEntry > ( ) ;
315+
316+ queries . set ( queryKey , {
317+ data : [ ...entities ] ,
318+ listeners : [ ] ,
319+ isInvalidated : false ,
320+ } ) ;
321+
322+ cacheEntry = {
323+ decoder : decode ,
324+ type,
325+ entities : entitiesMap ,
326+ queries,
327+ isInvalidated : false ,
328+ } ;
329+
330+ decodedEntitiesCache . set ( typeName , cacheEntry ) ;
331+
332+ for ( const entity of entities ) {
333+ for ( const [ , value ] of Object . entries ( entity ) ) {
334+ if ( Array . isArray ( value ) ) {
335+ for ( const relationEntity of value ) {
336+ let relationParentEntry = entityRelationParentsMap . get ( relationEntity . id ) ;
337+ if ( relationParentEntry ) {
338+ relationParentEntry . set ( cacheEntry , ( relationParentEntry . get ( cacheEntry ) ?? 0 ) + 1 ) ;
339+ } else {
340+ relationParentEntry = new Map ( ) ;
341+ entityRelationParentsMap . set ( relationEntity . id , relationParentEntry ) ;
342+ relationParentEntry . set ( cacheEntry , 1 ) ;
343+ }
331344 }
332345 }
333346 }
334347 }
335348 }
336- }
337349
338- const allTypes = new Set < S > ( ) ;
339- for ( const [ _key , field ] of Object . entries ( type . fields ) ) {
340- if ( isReferenceField ( field ) ) {
341- allTypes . add ( field as S ) ;
342- }
343- }
350+ const query = cacheEntry . queries . get ( queryKey ) ;
344351
345- const subscribe = ( callback : ( ) => void ) => {
346- const query = decodedEntitiesCache . get ( typeName ) ?. queries . get ( queryKey ) ;
347352 if ( query ?. listeners ) {
348353 query . listeners . push ( callback ) ;
349354 }
0 commit comments