@@ -12,6 +12,7 @@ import { AxiosInstance, AxiosError } from 'axios'
1212import ResourceInterface from './interfaces/ResourceInterface'
1313import StoreData , { Link , SerializablePromise } from './interfaces/StoreData'
1414import ApiActions from './interfaces/ApiActions'
15+ import { isVirtualResource } from './halHelpers'
1516
1617/**
1718 * Defines the API store methods available in all Vue components. The methods can be called as follows:
@@ -132,19 +133,23 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
132133 * in the Vuex store.
133134 */
134135 async function reload ( uriOrEntity : string | ResourceInterface ) : Promise < ResourceInterface > {
135- if ( uriOrEntity instanceof Resource && 'virtual' in uriOrEntity . _storeData . _meta && uriOrEntity . _storeData . _meta . virtual ) {
136+ let resource : ResourceInterface
137+
138+ if ( typeof uriOrEntity === 'string' ) {
139+ resource = get ( uriOrEntity )
140+ } else {
141+ resource = uriOrEntity
142+ }
143+
144+ if ( isVirtualResource ( resource ) ) {
136145 // For embedded collections which had to reload the parent entity, unwrap the embedded collection after loading has finished
137- const { owningResource, owningRelation } = uriOrEntity . _storeData . _meta
146+ const { owningResource, owningRelation } = resource . _storeData . _meta
138147 return reload ( owningResource ) . then ( owner => owner [ owningRelation ] ( ) )
139148 }
140149
141- const uri = normalizeEntityUri ( uriOrEntity , axios . defaults . baseURL )
150+ const uri = normalizeEntityUri ( resource , axios . defaults . baseURL )
142151
143152 if ( uri === null ) {
144- if ( uriOrEntity instanceof LoadingResource ) {
145- // A LoadingResource is safe to return without breaking the UI.
146- return uriOrEntity
147- }
148153 // We don't know anything about the requested object, something is wrong.
149154 throw new Error ( `Could not perform reload, "${ uriOrEntity } " is not an entity or URI` )
150155 }
@@ -159,16 +164,6 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
159164 return loadPromise . then ( storeData => resourceCreator . wrap ( storeData ) )
160165 }
161166
162- async function reloadResourceFromStoreData ( storeData : StoreData ) {
163- if ( 'virtual' in storeData . _meta && storeData . _meta . virtual ) {
164- const { owningResource, owningRelation } = storeData . _meta
165-
166- return reload ( owningResource ) . then ( owner => owner [ owningRelation ] ( ) )
167- }
168-
169- return reload ( storeData . _meta . self )
170- }
171-
172167 /**
173168 * Returns true if uri doesn't exist in store (never loaded before)
174169 * @param uri
@@ -367,7 +362,7 @@ function HalJsonVuex (store: Store<Record<string, State>>, axios: AxiosInstance,
367362 . filter ( outdatedEntity => ! outdatedEntity . _meta . deleting )
368363
369364 // reload outdated entities...
370- . map ( outdatedEntity => reloadResourceFromStoreData ( outdatedEntity ) . catch ( ( ) => {
365+ . map ( outdatedEntity => reload ( outdatedEntity . _meta . self ) . catch ( ( ) => {
371366 // ...but ignore any errors (such as 404 errors during reloading)
372367 // handleAxiosError will take care of recursively deleting cascade-deleted entities
373368 } ) )
0 commit comments