1- import { EndpointsCache , EntityCache } from './types.js' ;
1+ import { EndpointsCache } from './types.js' ;
22import WeakDependencyMap , { type Dep } from './WeakDependencyMap.js' ;
33import type Cache from '../denormalize/cache.js' ;
44import type { GetEntity } from '../denormalize/getEntities.js' ;
55import type { INVALID } from '../denormalize/symbol.js' ;
66import type { EntityInterface , EntityPath } from '../interface.js' ;
7+ import type { GetEntityCache } from './entitiesCache.js' ;
78
89export default class GlobalCache implements Cache {
910 private dependencies : Dep < EntityPath > [ ] = [ ] ;
1011 private cycleCache : Map < string , Map < string , number > > = new Map ( ) ;
1112 private cycleIndex = - 1 ;
1213 private localCache : Map < string , Map < string , any > > = new Map ( ) ;
1314
14- declare private getCache : (
15- pk : string ,
16- schema : EntityInterface ,
17- ) => WeakDependencyMap < EntityPath , object , any > ;
15+ declare private _getCache : GetEntityCache ;
1816
1917 declare private _getEntity : GetEntity ;
20- declare private resultCache : EndpointsCache ;
18+ declare private _resultCache : EndpointsCache ;
2119
2220 constructor (
2321 getEntity : GetEntity ,
24- entityCache : EntityCache ,
22+ getCache : GetEntityCache ,
2523 resultCache : EndpointsCache ,
2624 ) {
2725 this . _getEntity = getEntity ;
28- this . getCache = getEntityCaches ( entityCache ) ;
29- this . resultCache = resultCache ;
26+ this . _getCache = getCache ;
27+ this . _resultCache = resultCache ;
3028 }
3129
3230 getEntity (
@@ -43,7 +41,7 @@ export default class GlobalCache implements Cache {
4341 EntityPath ,
4442 object ,
4543 EntityCacheValue
46- > = this . getCache ( pk , schema ) ;
44+ > = this . _getCache ( pk , schema ) ;
4745 const [ cacheValue , cachePath ] = globalCache . get ( entity , this . _getEntity ) ;
4846 // TODO: what if this just returned the deps - then we don't need to store them
4947
@@ -118,15 +116,15 @@ export default class GlobalCache implements Cache {
118116 return { data : computeValue ( ) , paths : this . paths ( ) } ;
119117 }
120118
121- let [ data , paths ] = this . resultCache . get ( input , this . _getEntity ) ;
119+ let [ data , paths ] = this . _resultCache . get ( input , this . _getEntity ) ;
122120
123121 if ( paths === undefined ) {
124122 data = computeValue ( ) ;
125123 // we want to do this before we add our 'input' entry
126124 paths = this . paths ( ) ;
127125 // for the first entry, `path` is ignored so empty members is fine
128126 this . dependencies . unshift ( { path : { key : '' , pk : '' } , entity : input } ) ;
129- this . resultCache . set ( this . dependencies , data ) ;
127+ this . _resultCache . set ( this . dependencies , data ) ;
130128 } else {
131129 paths . shift ( ) ;
132130 }
@@ -142,41 +140,3 @@ interface EntityCacheValue {
142140 dependencies : Dep < EntityPath > [ ] ;
143141 value : object | typeof INVALID | undefined ;
144142}
145-
146- const getEntityCaches = ( entityCache : EntityCache ) => {
147- return ( pk : string , schema : EntityInterface ) => {
148- const key = schema . key ;
149- // collections should use the entities they collect over
150- // TODO: this should be based on a public interface
151- const entityInstance : EntityInterface = ( schema . cacheWith as any ) ?? schema ;
152-
153- if ( ! entityCache . has ( key ) ) {
154- entityCache . set ( key , new Map ( ) ) ;
155- }
156- const entityCacheKey = entityCache . get ( key ) ! ;
157- if ( ! entityCacheKey . get ( pk ) )
158- entityCacheKey . set (
159- pk ,
160- new WeakMap <
161- EntityInterface ,
162- WeakDependencyMap < EntityPath , object , any >
163- > ( ) ,
164- ) ;
165-
166- const entityCachePk = entityCacheKey . get ( pk ) as WeakMap <
167- EntityInterface ,
168- WeakDependencyMap < EntityPath , object , any >
169- > ;
170- let wem = entityCachePk . get ( entityInstance ) as WeakDependencyMap <
171- EntityPath ,
172- object ,
173- any
174- > ;
175- if ( ! wem ) {
176- wem = new WeakDependencyMap < EntityPath , object , any > ( ) ;
177- entityCachePk . set ( entityInstance , wem ) ;
178- }
179-
180- return wem ;
181- } ;
182- } ;
0 commit comments