@@ -53,14 +53,17 @@ export const useCoreStore = defineStore('core', () => {
5353 const resourceColumnsError : Ref < string > = ref ( '' ) ;
5454 const resourceColumnsId : Ref < string | null > = ref ( null ) ;
5555 const adminUser : Ref < null | AdminUser > = ref ( null ) ;
56+ // /get_resource responses, valid until config is refetched or user logs out
57+ const fullResourceCache = new Map < string , AdminForthResourceFrontend > ( ) ;
58+
5659
57-
5860 async function resetAdminUser ( ) {
5961 adminUser . value = null ;
6062 }
6163
6264 async function resetResource ( ) {
6365 resource . value = null ;
66+ fullResourceCache . clear ( ) ;
6467 }
6568
6669 async function toggleTheme ( ) {
@@ -99,6 +102,7 @@ export const useCoreStore = defineStore('core', () => {
99102 }
100103
101104 menu . value = resp . menu ;
105+ fullResourceCache . clear ( ) ;
102106 resourceById . value = resp . resources . reduce ( ( acc : Record < string , ResourceVeryShort > , resource : ResourceVeryShort ) => {
103107 acc [ resource . resourceId ] = resource ;
104108 return acc ;
@@ -237,33 +241,47 @@ export const useCoreStore = defineStore('core', () => {
237241
238242 }
239243
244+ function applyResource ( fullResource : AdminForthResourceFrontend ) {
245+ resource . value = fullResource ;
246+ resourceOptions . value = fullResource . options ;
247+ }
248+
240249 async function fetchResourceFull ( { resourceId, forceFetch } : { resourceId : string , forceFetch ?: boolean } ) {
241- if ( resourceColumnsId . value === resourceId && resource . value && ! forceFetch ) {
242- // already fetched
250+ resourceColumnsId . value = resourceId ;
251+ resourceColumnsError . value = '' ;
252+ const cached = fullResourceCache . get ( resourceId ) ;
253+ if ( cached && ! forceFetch ) {
254+ // applied synchronously so the page renders real columns on the first frame
255+ // flag is reset because a request for previously opened resource may still be in flight
256+ isResourceFetching . value = false ;
257+ applyResource ( cached ) ;
243258 return ;
244259 }
245260 isResourceFetching . value = true ;
246- resourceColumnsId . value = resourceId ;
247- resourceColumnsError . value = '' ;
248261 const res = await callAdminForthApi ( {
249262 path : '/get_resource' ,
250263 method : 'POST' ,
251264 body : {
252265 resourceId,
253266 }
254267 } ) ;
268+ if ( res && ! res . error ) {
269+ fullResourceCache . set ( resourceId , res . resource ) ;
270+ resourceById . value [ resourceId ] = res . resource ;
271+ }
272+ if ( resourceColumnsId . value !== resourceId ) {
273+ // user switched to another resource while this request was in flight, that fetch owns the state now
274+ return ;
275+ }
276+ isResourceFetching . value = false ;
255277 if ( ! res ) {
256- isResourceFetching . value = false ;
257278 return ;
258279 }
259280 if ( res . error ) {
260281 resourceColumnsError . value = res . error ;
261282 } else {
262- resourceById . value [ resourceId ] = res . resource ;
263- resource . value = res . resource ;
264- resourceOptions . value = res . resource . options ;
283+ applyResource ( res . resource ) ;
265284 }
266- isResourceFetching . value = false ;
267285 }
268286
269287 async function getLoginFormConfig ( ) {
0 commit comments