@@ -114,8 +114,11 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
114114 * @param id the ID of the relationship to delete
115115 * @param copyVirtualMetadata whether to copy this relationship's virtual metadata to the related Items
116116 * accepted values: none, all, left, right, configured
117+ * @param shouldRefresh refresh the cache for the items in the relationship after creating
118+ * it. Disable this if you want to add relationships in bulk, and
119+ * want to refresh the cachemanually at the end
117120 */
118- deleteRelationship ( id : string , copyVirtualMetadata : string ) : Observable < RemoteData < NoContent > > {
121+ deleteRelationship ( id : string , copyVirtualMetadata : string , shouldRefresh = true ) : Observable < RemoteData < NoContent > > {
119122 return this . getRelationshipEndpoint ( id ) . pipe (
120123 isNotEmptyOperator ( ) ,
121124 take ( 1 ) ,
@@ -126,7 +129,11 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
126129 sendRequest ( this . requestService ) ,
127130 switchMap ( ( restRequest : RestRequest ) => this . rdbService . buildFromRequestUUID ( restRequest . uuid ) ) ,
128131 getFirstCompletedRemoteData ( ) ,
129- tap ( ( ) => this . refreshRelationshipItemsInCacheByRelationship ( id ) ) ,
132+ tap ( ( ) => {
133+ if ( shouldRefresh ) {
134+ this . refreshRelationshipItemsInCacheByRelationship ( id ) ;
135+ }
136+ } ) ,
130137 ) ;
131138 }
132139
@@ -137,8 +144,11 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
137144 * @param item2 The second item of the relationship
138145 * @param leftwardValue The leftward value of the relationship
139146 * @param rightwardValue The rightward value of the relationship
147+ * @param shouldRefresh refresh the cache for the items in the relationship after creating it.
148+ * Disable this if you want to add relationships in bulk, and want to refresh
149+ * the cache manually at the end
140150 */
141- addRelationship ( typeId : string , item1 : Item , item2 : Item , leftwardValue ?: string , rightwardValue ?: string ) : Observable < RemoteData < Relationship > > {
151+ addRelationship ( typeId : string , item1 : Item , item2 : Item , leftwardValue ?: string , rightwardValue ?: string , shouldRefresh = true ) : Observable < RemoteData < Relationship > > {
142152 const options : HttpOptions = Object . create ( { } ) ;
143153 let headers = new HttpHeaders ( ) ;
144154 headers = headers . append ( 'Content-Type' , 'text/uri-list' ) ;
@@ -153,8 +163,12 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
153163 sendRequest ( this . requestService ) ,
154164 switchMap ( ( restRequest : RestRequest ) => this . rdbService . buildFromRequestUUID ( restRequest . uuid ) ) ,
155165 getFirstCompletedRemoteData ( ) ,
156- tap ( ( ) => this . refreshRelationshipItemsInCache ( item1 ) ) ,
157- tap ( ( ) => this . refreshRelationshipItemsInCache ( item2 ) ) ,
166+ tap ( ( ) => {
167+ if ( shouldRefresh ) {
168+ this . refreshRelationshipItemsInCache ( item1 ) ;
169+ this . refreshRelationshipItemsInCache ( item2 ) ;
170+ }
171+ } ) ,
158172 ) as Observable < RemoteData < Relationship > > ;
159173 }
160174
@@ -182,7 +196,7 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
182196 * Method to remove an item that's part of a relationship from the cache
183197 * @param item The item to remove from the cache
184198 */
185- public refreshRelationshipItemsInCache ( item ) {
199+ public refreshRelationshipItemsInCache ( item : Item ) : void {
186200 this . objectCache . remove ( item . _links . self . href ) ;
187201 this . requestService . removeByHrefSubstring ( item . uuid ) ;
188202 observableCombineLatest ( [
@@ -295,7 +309,19 @@ export class RelationshipDataService extends IdentifiableDataService<Relationshi
295309 } else {
296310 findListOptions . searchParams = searchParams ;
297311 }
298- return this . searchBy ( 'byLabel' , findListOptions , useCachedVersionIfAvailable , reRequestOnStale , ...linksToFollow ) ;
312+
313+ // always set reRequestOnStale to false here, so it doesn't happen automatically in BaseDataService
314+ const result$ = this . searchBy ( 'byLabel' , findListOptions , useCachedVersionIfAvailable , false , ...linksToFollow ) ;
315+
316+ // add this result as a dependency of the item, meaning that if the item is invalided, this
317+ // result will be as well
318+ this . addDependency ( result$ , item . _links . self . href ) ;
319+
320+ // do the reRequestOnStale call here, to ensure any re-requests also get added as dependencies
321+ return result$ . pipe (
322+ this . reRequestStaleRemoteData ( reRequestOnStale , ( ) =>
323+ this . getItemRelationshipsByLabel ( item , label , options , useCachedVersionIfAvailable , reRequestOnStale , ...linksToFollow ) ) ,
324+ ) ;
299325 }
300326
301327 /**
0 commit comments