11/* eslint-disable max-classes-per-file */
22import {
3+ AddDependentsObjectCacheAction ,
34 AddPatchObjectCacheAction ,
45 AddToObjectCacheAction ,
56 ApplyPatchObjectCacheAction ,
67 ObjectCacheAction ,
7- ObjectCacheActionTypes ,
8+ ObjectCacheActionTypes , RemoveDependentsObjectCacheAction ,
89 RemoveFromObjectCacheAction ,
9- ResetObjectCacheTimestampsAction
10+ ResetObjectCacheTimestampsAction ,
1011} from './object-cache.actions' ;
1112import { hasValue , isNotEmpty } from '../../shared/empty.util' ;
1213import { CacheEntry } from './cache-entry' ;
@@ -69,6 +70,12 @@ export class ObjectCacheEntry implements CacheEntry {
6970 */
7071 requestUUIDs : string [ ] ;
7172
73+ /**
74+ * A list of UUIDs for the requests that depend on this object.
75+ * When this object is invalidated, these requests will be invalidated as well.
76+ */
77+ dependentRequestUUIDs : string [ ] ;
78+
7279 /**
7380 * An array of patches that were made on the client side to this entry, but haven't been sent to the server yet
7481 */
@@ -134,6 +141,14 @@ export function objectCacheReducer(state = initialState, action: ObjectCacheActi
134141 return applyPatchObjectCache ( state , action as ApplyPatchObjectCacheAction ) ;
135142 }
136143
144+ case ObjectCacheActionTypes . ADD_DEPENDENTS : {
145+ return addDependentsObjectCacheState ( state , action as AddDependentsObjectCacheAction ) ;
146+ }
147+
148+ case ObjectCacheActionTypes . REMOVE_DEPENDENTS : {
149+ return removeDependentsObjectCacheState ( state , action as RemoveDependentsObjectCacheAction ) ;
150+ }
151+
137152 default : {
138153 return state ;
139154 }
@@ -159,6 +174,7 @@ function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheActio
159174 timeCompleted : action . payload . timeCompleted ,
160175 msToLive : action . payload . msToLive ,
161176 requestUUIDs : [ action . payload . requestUUID , ...( existing . requestUUIDs || [ ] ) ] ,
177+ dependentRequestUUIDs : existing . dependentRequestUUIDs || [ ] ,
162178 isDirty : isNotEmpty ( existing . patches ) ,
163179 patches : existing . patches || [ ] ,
164180 alternativeLinks : [ ...( existing . alternativeLinks || [ ] ) , ...newAltLinks ]
@@ -252,3 +268,49 @@ function applyPatchObjectCache(state: ObjectCacheState, action: ApplyPatchObject
252268 }
253269 return newState ;
254270}
271+
272+ /**
273+ * Add a list of dependent request UUIDs to a cached object, used when defining new dependencies
274+ *
275+ * @param state the current state
276+ * @param action an AddDependentsObjectCacheAction
277+ * @return the new state, with the dependent requests of the cached object updated
278+ */
279+ function addDependentsObjectCacheState ( state : ObjectCacheState , action : AddDependentsObjectCacheAction ) : ObjectCacheState {
280+ const href = action . payload . href ;
281+ const newState = Object . assign ( { } , state ) ;
282+
283+ if ( hasValue ( newState [ href ] ) ) {
284+ newState [ href ] = Object . assign ( { } , newState [ href ] , {
285+ dependentRequestUUIDs : [
286+ ...new Set ( [
287+ ...newState [ href ] ?. dependentRequestUUIDs || [ ] ,
288+ ...action . payload . dependentRequestUUIDs ,
289+ ] )
290+ ]
291+ } ) ;
292+ }
293+
294+ return newState ;
295+ }
296+
297+
298+ /**
299+ * Remove all dependent request UUIDs from a cached object, used to clear out-of-date depedencies
300+ *
301+ * @param state the current state
302+ * @param action an AddDependentsObjectCacheAction
303+ * @return the new state, with the dependent requests of the cached object updated
304+ */
305+ function removeDependentsObjectCacheState ( state : ObjectCacheState , action : RemoveDependentsObjectCacheAction ) : ObjectCacheState {
306+ const href = action . payload ;
307+ const newState = Object . assign ( { } , state ) ;
308+
309+ if ( hasValue ( newState [ href ] ) ) {
310+ newState [ href ] = Object . assign ( { } , newState [ href ] , {
311+ dependentRequestUUIDs : [ ]
312+ } ) ;
313+ }
314+
315+ return newState ;
316+ }
0 commit comments