@@ -5,7 +5,7 @@ import * as SchemaAST from 'effect/SchemaAST';
5
5
import { TypeIdsSymbol } from '../constants.js' ;
6
6
import { deepMerge } from '../utils/internal/deep-merge.js' ;
7
7
import { canonicalize } from '../utils/jsc.js' ;
8
- import { type DecodedEntitiesCacheEntry , decodedEntitiesCache , type QueryEntry } from './decodedEntitiesCache.js' ;
8
+ import { decodedEntitiesCache , type DecodedEntitiesCacheEntry , type QueryEntry } from './decodedEntitiesCache.js' ;
9
9
import { decodeFromGrc20Json } from './entity.js' ;
10
10
import { entityRelationParentsMap } from './entityRelationParentsMap.js' ;
11
11
import { getEntityRelations } from './getEntityRelations.js' ;
@@ -72,9 +72,11 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
72
72
for ( const entityId of changedEntities ) {
73
73
const entity = doc . entities ?. [ entityId ] ;
74
74
if ( ! hasValidTypesProperty ( entity ) ) continue ;
75
- for ( const typeName of entity [ '@@types@@' ] ) {
76
- if ( typeof typeName !== 'string' ) continue ;
77
- const cacheEntry = decodedEntitiesCache . get ( typeName ) ;
75
+ for ( const typeId of entity [ '@@types@@' ] ) {
76
+ console . log ( 'typeId' , typeId ) ;
77
+ if ( typeof typeId !== 'string' ) continue ;
78
+ const cacheEntry = decodedEntitiesCache . get ( typeId ) ;
79
+ console . log ( 'cacheEntry' , cacheEntry ) ;
78
80
if ( ! cacheEntry ) continue ;
79
81
80
82
let includeFromAllQueries = { } ;
@@ -101,6 +103,8 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
101
103
console . error ( 'error' , error ) ;
102
104
}
103
105
106
+ console . log ( 'decoded' , decoded ) ;
107
+
104
108
if ( oldDecodedEntry ) {
105
109
// collect all the Ids for relation entries in the `oldDecodedEntry`
106
110
const deletedRelationIds = new Set < string > ( ) ;
@@ -144,11 +148,11 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
144
148
}
145
149
146
150
for ( const [ queryKey , query ] of cacheEntry . queries ) {
147
- touchedQueries . add ( [ typeName , queryKey ] ) ;
151
+ touchedQueries . add ( [ typeId , queryKey ] ) ;
148
152
query . isInvalidated = true ;
149
153
}
150
154
151
- entityTypes . add ( typeName ) ;
155
+ entityTypes . add ( typeId ) ;
152
156
153
157
// gather all the decodedEntitiesCacheEntries that have a relation to this entity to
154
158
// invoke their query listeners below
@@ -165,17 +169,17 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
165
169
166
170
// loop over all deleted entities and remove them from the cache
167
171
for ( const entityId of deletedEntities ) {
168
- for ( const [ affectedTypeName , cacheEntry ] of decodedEntitiesCache ) {
172
+ for ( const [ affectedTypeId , cacheEntry ] of decodedEntitiesCache ) {
169
173
if ( cacheEntry . entities . has ( entityId ) ) {
170
- entityTypes . add ( affectedTypeName ) ;
174
+ entityTypes . add ( affectedTypeId ) ;
171
175
cacheEntry . entities . delete ( entityId ) ;
172
176
173
177
for ( const [ queryKey , query ] of cacheEntry . queries ) {
174
178
// find the entity in the query and remove it using splice
175
179
const index = query . data . findIndex ( ( entity ) => entity . id === entityId ) ;
176
180
if ( index !== - 1 ) {
177
181
query . data . splice ( index , 1 ) ;
178
- touchedQueries . add ( [ affectedTypeName , queryKey ] ) ;
182
+ touchedQueries . add ( [ affectedTypeId , queryKey ] ) ;
179
183
}
180
184
}
181
185
}
@@ -195,8 +199,8 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
195
199
}
196
200
197
201
// update the queries affected queries
198
- for ( const [ typeName , queryKey ] of touchedQueries ) {
199
- const cacheEntry = decodedEntitiesCache . get ( typeName ) ;
202
+ for ( const [ typeId , queryKey ] of touchedQueries ) {
203
+ const cacheEntry = decodedEntitiesCache . get ( typeId ) ;
200
204
if ( ! cacheEntry ) continue ;
201
205
202
206
const query = cacheEntry . queries . get ( queryKey ) ;
@@ -206,8 +210,8 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
206
210
}
207
211
208
212
// invoke all the listeners per type
209
- for ( const typeName of entityTypes ) {
210
- const cacheEntry = decodedEntitiesCache . get ( typeName ) ;
213
+ for ( const typeId of entityTypes ) {
214
+ const cacheEntry = decodedEntitiesCache . get ( typeId ) ;
211
215
if ( ! cacheEntry ) continue ;
212
216
213
217
for ( const query of cacheEntry . queries . values ( ) ) {
@@ -246,7 +250,7 @@ export function findMany<const S extends Schema.Schema.AnyNoContext>(
246
250
filter : EntityFilter < Schema . Schema . Type < S > > | undefined ,
247
251
include : { [ K in keyof Schema . Schema . Type < S > ] ?: Record < string , Record < string , never > > } | undefined ,
248
252
) : { entities : Readonly < Array < Entity < S > > > ; corruptEntityIds : Readonly < Array < string > > } {
249
- const typeIds = SchemaAST . getAnnotation < string [ ] > ( TypeIdsSymbol ) ( type . ast as SchemaAST . TypeLiteral ) . pipe (
253
+ const typeId = SchemaAST . getAnnotation < string [ ] > ( TypeIdsSymbol ) ( type . ast as SchemaAST . TypeLiteral ) . pipe (
250
254
Option . getOrElse ( ( ) => [ ] ) ,
251
255
) ;
252
256
@@ -363,7 +367,7 @@ export function findMany<const S extends Schema.Schema.AnyNoContext>(
363
367
const entity = entities [ id ] ;
364
368
if (
365
369
hasValidTypesProperty ( entity ) &&
366
- typeIds . every ( ( typeId ) => {
370
+ typeId . every ( ( typeId ) => {
367
371
return entity [ '@@types@@' ] . includes ( typeId ) ;
368
372
} )
369
373
) {
@@ -421,7 +425,7 @@ export function subscribeToFindMany<const S extends Schema.Schema.AnyNoContext>(
421
425
) ;
422
426
423
427
const getEntities = ( ) => {
424
- const cacheEntry = decodedEntitiesCache . get ( canonicalize ( typeIds ) ) ;
428
+ const cacheEntry = decodedEntitiesCache . get ( typeIds [ 0 ] ) ;
425
429
if ( ! cacheEntry ) return stableEmptyArray ;
426
430
427
431
const query = cacheEntry . queries . get ( queryKey ) ;
@@ -446,47 +450,50 @@ export function subscribeToFindMany<const S extends Schema.Schema.AnyNoContext>(
446
450
} ;
447
451
448
452
const subscribe = ( callback : ( ) => void ) => {
449
- let cacheEntry = decodedEntitiesCache . get ( canonicalize ( typeIds ) ) ;
450
-
451
- if ( ! cacheEntry ) {
452
- const entitiesMap = new Map ( ) ;
453
- const queries = new Map < string , QueryEntry > ( ) ;
454
-
455
- queries . set ( queryKey , {
456
- data : [ ] ,
457
- listeners : [ ] ,
458
- isInvalidated : true ,
459
- include : include ?? { } ,
460
- } ) ;
461
-
462
- cacheEntry = {
463
- type,
464
- entities : entitiesMap ,
465
- queries,
466
- isInvalidated : true ,
467
- } ;
468
-
469
- decodedEntitiesCache . set ( canonicalize ( typeIds ) , cacheEntry ) ;
470
- }
453
+ if ( typeIds . length > 1 ) {
454
+ console . error ( 'Schema with multiple type ids is not supported' , typeIds ) ;
455
+ } else {
456
+ let cacheEntry = decodedEntitiesCache . get ( typeIds [ 0 ] ) ;
457
+ if ( ! cacheEntry ) {
458
+ const entitiesMap = new Map ( ) ;
459
+ const queries = new Map < string , QueryEntry > ( ) ;
460
+
461
+ queries . set ( queryKey , {
462
+ data : [ ] ,
463
+ listeners : [ ] ,
464
+ isInvalidated : true ,
465
+ include : include ?? { } ,
466
+ } ) ;
467
+
468
+ cacheEntry = {
469
+ type,
470
+ entities : entitiesMap ,
471
+ queries,
472
+ isInvalidated : true ,
473
+ } ;
471
474
472
- let query = cacheEntry . queries . get ( queryKey ) ;
473
- if ( ! query ) {
474
- query = {
475
- data : [ ] ,
476
- listeners : [ ] ,
477
- isInvalidated : true ,
478
- include : include ?? { } ,
479
- } ;
480
- // we just set up the query and expect it to correctly set itself up in findMany
481
- cacheEntry . queries . set ( queryKey , query ) ;
482
- }
475
+ decodedEntitiesCache . set ( typeIds [ 0 ] , cacheEntry ) ;
476
+ }
483
477
484
- if ( query ?. listeners ) {
485
- query . listeners . push ( callback ) ;
478
+ let query = cacheEntry . queries . get ( queryKey ) ;
479
+ if ( ! query ) {
480
+ query = {
481
+ data : [ ] ,
482
+ listeners : [ ] ,
483
+ isInvalidated : true ,
484
+ include : include ?? { } ,
485
+ } ;
486
+ // we just set up the query and expect it to correctly set itself up in findMany
487
+ cacheEntry . queries . set ( queryKey , query ) ;
488
+ }
489
+
490
+ if ( query ?. listeners ) {
491
+ query . listeners . push ( callback ) ;
492
+ }
486
493
}
487
494
488
495
return ( ) => {
489
- const cacheEntry = decodedEntitiesCache . get ( canonicalize ( typeIds ) ) ;
496
+ const cacheEntry = decodedEntitiesCache . get ( typeIds [ 0 ] ) ;
490
497
if ( cacheEntry ) {
491
498
// first cleanup the queries
492
499
const query = cacheEntry . queries . get ( queryKey ) ;
@@ -508,7 +515,7 @@ export function subscribeToFindMany<const S extends Schema.Schema.AnyNoContext>(
508
515
entityRelationParentsMap . delete ( key ) ;
509
516
}
510
517
} ) ;
511
- decodedEntitiesCache . delete ( canonicalize ( typeIds ) ) ;
518
+ decodedEntitiesCache . delete ( typeIds [ 0 ] ) ;
512
519
}
513
520
}
514
521
0 commit comments