@@ -7,6 +7,7 @@ import { canonicalize } from '../utils/jsc.js';
7
7
import { decodedEntitiesCache , type DecodedEntitiesCacheEntry , type QueryEntry } from './decodedEntitiesCacheNew.js' ;
8
8
import { decodeFromGrc20Json } from './entity-new.js' ;
9
9
import { entityRelationParentsMap } from './entityRelationParentsMap.js' ;
10
+ import { getEntityRelationsNew } from './getEntityRelationsNew.js' ;
10
11
import { hasValidTypesProperty } from './hasValidTypesProperty.js' ;
11
12
import { TypeIdsSymbol } from './internal-new.js' ;
12
13
import type {
@@ -82,15 +83,17 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
82
83
}
83
84
84
85
const oldDecodedEntry = cacheEntry . entities . get ( entityId ) ;
85
- // const relations = getEntityRelations (entityId, cacheEntry.type, doc, includeFromAllQueries);
86
+ const relations = getEntityRelationsNew ( entityId , cacheEntry . type , doc , includeFromAllQueries ) ;
86
87
let decoded : unknown | undefined ;
87
88
try {
88
- // decoded = cacheEntry.decoder({
89
89
decoded = decodeFromGrc20Json ( cacheEntry . type , {
90
90
...entity ,
91
- // ...relations,
92
91
id : entityId ,
93
92
} ) ;
93
+ decoded = {
94
+ ...decoded ,
95
+ ...relations ,
96
+ } ;
94
97
cacheEntry . entities . set ( entityId , decoded ) ;
95
98
} catch ( error ) {
96
99
// TODO: store the corrupt entity ids somewhere, so they can be read via the API
@@ -99,43 +102,43 @@ const subscribeToDocumentChanges = (handle: DocHandle<DocumentContent>) => {
99
102
100
103
if ( oldDecodedEntry ) {
101
104
// collect all the Ids for relation entries in the `oldDecodedEntry`
102
- // const deletedRelationIds = new Set<string>();
103
- // for (const [, value] of Object.entries(oldDecodedEntry)) {
104
- // if (Array.isArray(value)) {
105
- // for (const relationEntity of value) {
106
- // deletedRelationIds.add(relationEntity.id);
107
- // }
108
- // }
109
- // }
105
+ const deletedRelationIds = new Set < string > ( ) ;
106
+ for ( const [ , value ] of Object . entries ( oldDecodedEntry ) ) {
107
+ if ( Array . isArray ( value ) ) {
108
+ for ( const relationEntity of value ) {
109
+ deletedRelationIds . add ( relationEntity . id ) ;
110
+ }
111
+ }
112
+ }
110
113
// it's fine to remove all of them since they are re-added below
111
- // for (const deletedRelationId of deletedRelationIds) {
112
- // const deletedRelationEntry = entityRelationParentsMap.get(deletedRelationId);
113
- // if (deletedRelationEntry) {
114
- // deletedRelationEntry.set(cacheEntry, (deletedRelationEntry.get(cacheEntry) ?? 0) - 1);
115
- // if (deletedRelationEntry.get(cacheEntry) === 0) {
116
- // deletedRelationEntry.delete(cacheEntry);
117
- // }
118
- // if (deletedRelationEntry.size === 0) {
119
- // entityRelationParentsMap.delete(deletedRelationId);
120
- // }
121
- // }
122
- // }
114
+ for ( const deletedRelationId of deletedRelationIds ) {
115
+ const deletedRelationEntry = entityRelationParentsMap . get ( deletedRelationId ) ;
116
+ if ( deletedRelationEntry ) {
117
+ deletedRelationEntry . set ( cacheEntry , ( deletedRelationEntry . get ( cacheEntry ) ?? 0 ) - 1 ) ;
118
+ if ( deletedRelationEntry . get ( cacheEntry ) === 0 ) {
119
+ deletedRelationEntry . delete ( cacheEntry ) ;
120
+ }
121
+ if ( deletedRelationEntry . size === 0 ) {
122
+ entityRelationParentsMap . delete ( deletedRelationId ) ;
123
+ }
124
+ }
125
+ }
123
126
}
124
127
125
128
if ( decoded ) {
126
129
for ( const [ , value ] of Object . entries ( decoded ) ) {
127
- // if (Array.isArray(value)) {
128
- // for (const relationEntity of value) {
129
- // let relationParentEntry = entityRelationParentsMap.get(relationEntity.id);
130
- // if (relationParentEntry) {
131
- // relationParentEntry.set(cacheEntry, (relationParentEntry.get(cacheEntry) ?? 0) + 1);
132
- // } else {
133
- // relationParentEntry = new Map();
134
- // entityRelationParentsMap.set(relationEntity.id, relationParentEntry);
135
- // relationParentEntry.set(cacheEntry, 1);
136
- // }
137
- // }
138
- // }
130
+ if ( Array . isArray ( value ) ) {
131
+ for ( const relationEntity of value ) {
132
+ let relationParentEntry = entityRelationParentsMap . get ( relationEntity . id ) ;
133
+ if ( relationParentEntry ) {
134
+ relationParentEntry . set ( cacheEntry , ( relationParentEntry . get ( cacheEntry ) ?? 0 ) + 1 ) ;
135
+ } else {
136
+ relationParentEntry = new Map ( ) ;
137
+ entityRelationParentsMap . set ( relationEntity . id , relationParentEntry ) ;
138
+ relationParentEntry . set ( cacheEntry , 1 ) ;
139
+ }
140
+ }
141
+ }
139
142
}
140
143
}
141
144
@@ -247,7 +250,6 @@ export function findManyNew<const S extends Schema.Schema.AnyNoContext>(
247
250
) ;
248
251
249
252
const doc = handle . doc ( ) ;
250
- console . log ( 'doc' , doc ) ;
251
253
if ( ! doc ) {
252
254
return { entities : [ ] , corruptEntityIds : [ ] } ;
253
255
}
@@ -364,10 +366,13 @@ export function findManyNew<const S extends Schema.Schema.AnyNoContext>(
364
366
return entity [ '@@types@@' ] . includes ( typeId ) ;
365
367
} )
366
368
) {
367
- // const relations = getEntityRelations (id, type, doc, include);
369
+ const relations = getEntityRelationsNew ( id , type , doc , include ) ;
368
370
try {
369
- // const decoded = { ...decodeFromGrc20Json(type, { ...entity, ...relations, id }) };
370
- const decoded = { ...decodeFromGrc20Json ( type , { ...entity , id } ) } ;
371
+ let decoded = { ...decodeFromGrc20Json ( type , { ...entity , id } ) } ;
372
+ decoded = {
373
+ ...decoded ,
374
+ ...relations ,
375
+ } ;
371
376
372
377
if ( filter ) {
373
378
if ( evaluateEntityFilter ( filter , decoded ) ) {
@@ -426,7 +431,6 @@ export function subscribeToFindManyNew<const S extends Schema.Schema.AnyNoContex
426
431
}
427
432
428
433
const { entities } = findManyNew ( handle , type , filter , include ) ;
429
- console . log ( 'getEntities' , entities ) ;
430
434
431
435
for ( const entity of entities ) {
432
436
cacheEntry ?. entities . set ( entity . id , entity ) ;
0 commit comments