@@ -35,6 +35,8 @@ import {
3535} from './actions/index.js' ;
3636import ensurePojo from './ensurePojo.js' ;
3737import type { EndpointUpdateFunction } from './types.js' ;
38+ import type { GCInterface } from '../state/GCPolicy.js' ;
39+ import { ImmortalGCPolicy } from '../state/GCPolicy.js' ;
3840import { initialState } from '../state/reducer/createReducer.js' ;
3941import selectMeta from '../state/selectMeta.js' ;
4042import type { ActionTypes , State } from '../types.js' ;
@@ -46,6 +48,7 @@ interface ConstructorProps<D extends GenericDispatch = DataClientDispatch> {
4648 dispatch ?: D ;
4749 getState ?: ( ) => State < unknown > ;
4850 memo ?: Pick < MemoCache , 'denormalize' | 'query' | 'buildQueryKey' > ;
51+ gcPolicy ?: GCInterface ;
4952}
5053
5154const unsetDispatch = ( action : unknown ) : Promise < void > => {
@@ -89,14 +92,21 @@ export default class Controller<
8992 'denormalize' | 'query' | 'buildQueryKey'
9093 > ;
9194
95+ /**
96+ * Handles garbage collection
97+ */
98+ declare readonly gcPolicy : GCInterface ;
99+
92100 constructor ( {
93101 dispatch = unsetDispatch as any ,
94102 getState = unsetState ,
95103 memo = new MemoCache ( ) ,
104+ gcPolicy = new ImmortalGCPolicy ( ) ,
96105 } : ConstructorProps < D > = { } ) {
97106 this . dispatch = dispatch ;
98107 this . getState = getState ;
99108 this . memo = memo ;
109+ this . gcPolicy = gcPolicy ;
100110 }
101111
102112 /*************** Action Dispatchers ***************/
@@ -389,6 +399,7 @@ export default class Controller<
389399 data : DenormalizeNullable < E [ 'schema' ] > ;
390400 expiryStatus : ExpiryStatus ;
391401 expiresAt : number ;
402+ countRef : ( ) => ( ) => void ;
392403 } ;
393404
394405 getResponse <
@@ -403,6 +414,7 @@ export default class Controller<
403414 data : DenormalizeNullable < E [ 'schema' ] > ;
404415 expiryStatus : ExpiryStatus ;
405416 expiresAt : number ;
417+ countRef : ( ) => ( ) => void ;
406418 } ;
407419
408420 getResponse (
@@ -412,6 +424,7 @@ export default class Controller<
412424 data : unknown ;
413425 expiryStatus : ExpiryStatus ;
414426 expiresAt : number ;
427+ countRef : ( ) => ( ) => void ;
415428 } {
416429 const state = rest [ rest . length - 1 ] as State < unknown > ;
417430 // this is typescript generics breaking
@@ -446,12 +459,14 @@ export default class Controller<
446459 data : input as any ,
447460 expiryStatus : ExpiryStatus . Valid ,
448461 expiresAt : Infinity ,
462+ countRef : ( ) => ( ) => undefined ,
449463 } ;
450464 }
451465
452466 let isInvalid = false ;
453467 if ( shouldQuery ) {
454468 isInvalid = ! validateQueryKey ( input ) ;
469+ // endpoint without entities
455470 } else if ( ! schema || ! schemaHasEntity ( schema ) ) {
456471 return {
457472 data : cacheEndpoints ,
@@ -460,6 +475,7 @@ export default class Controller<
460475 : cacheEndpoints && ! endpoint . invalidIfStale ? ExpiryStatus . Valid
461476 : ExpiryStatus . InvalidIfStale ,
462477 expiresAt : expiresAt || 0 ,
478+ countRef : this . gcPolicy . createCountRef ( { key } ) ,
463479 } ;
464480 }
465481
@@ -477,6 +493,7 @@ export default class Controller<
477493
478494 return this . getSchemaResponse (
479495 data ,
496+ key ,
480497 paths ,
481498 state . entityMeta ,
482499 expiresAt ,
@@ -507,6 +524,7 @@ export default class Controller<
507524
508525 private getSchemaResponse < T > (
509526 data : T ,
527+ key : string ,
510528 paths : EntityPath [ ] ,
511529 entityMeta : State < unknown > [ 'entityMeta' ] ,
512530 expiresAt : number ,
@@ -516,6 +534,7 @@ export default class Controller<
516534 data : T ;
517535 expiryStatus : ExpiryStatus ;
518536 expiresAt : number ;
537+ countRef : ( ) => ( ) => void ;
519538 } {
520539 const invalidDenormalize = typeof data === 'symbol' ;
521540
@@ -533,7 +552,12 @@ export default class Controller<
533552 : invalidDenormalize || invalidIfStale ? ExpiryStatus . InvalidIfStale
534553 : ExpiryStatus . Valid ;
535554
536- return { data, expiryStatus, expiresAt } ;
555+ return {
556+ data,
557+ expiryStatus,
558+ expiresAt,
559+ countRef : this . gcPolicy . createCountRef ( { key, paths } ) ,
560+ } ;
537561 }
538562}
539563
0 commit comments