11import { fetch } from "@design-sdk/figma-remote" ;
2- import { FileResponse } from "@design-sdk/figma-remote-types " ;
3- import { FigmaFileStore , FileResponseRecord } from "@editor/figma-file-store " ;
2+ import { FigmaFileStore , FigmaFileMetaStore } from "./stores " ;
3+ import type { FileResponseRecord } from "./stores " ;
44
55export type TFetchFileForApp = (
66 | fetch . FetchFileGeneratorReturnType
@@ -20,23 +20,29 @@ export class FigmaDesignRepository {
2020 ) { }
2121
2222 static async fetchCachedFile ( fileId : string ) {
23+ const metastore = new FigmaFileMetaStore ( ) ;
2324 const store = new FigmaFileStore ( fileId ) ;
2425 const existing = await store . get ( ) ;
2526 if ( existing ) {
27+ // everytime the file is consumed consider it as used, we upsert the file so that the lastUsed can be updated.
28+ metastore . upsert ( existing . key , existing ) ;
2629 return { ...existing , __initial : false } as TFetchFileForApp ;
2730 } else {
2831 throw new Error ( "file not found" ) ;
2932 }
3033 }
3134
32- async * fetchFile ( fileId : string ) {
33- const store = new FigmaFileStore ( fileId ) ;
35+ async * fetchFile ( filekey : string ) {
36+ const metastore = new FigmaFileMetaStore ( ) ;
37+ const store = new FigmaFileStore ( filekey ) ;
3438 const existing = await store . get ( ) ;
3539 if ( existing ) {
40+ // everytime the file is consumed consider it as used, we upsert the file so that the lastUsed can be updated.
41+ metastore . upsert ( existing . key , existing ) ;
3642 yield { ...existing , __initial : false } as TFetchFileForApp ;
3743 }
3844
39- const _iter = fetch . fetchFile ( { file : fileId , auth : this . auth } ) ;
45+ const _iter = fetch . fetchFile ( { file : filekey , auth : this . auth } ) ;
4046 let next : IteratorResult < fetch . FetchFileGeneratorReturnType > ;
4147 while ( ( next = await _iter . next ( ) ) . done === false ) {
4248 switch ( next . value . __response_type ) {
@@ -48,6 +54,7 @@ export class FigmaDesignRepository {
4854 __type : "file-fetched-for-app" ,
4955 } as TFetchFileForApp ;
5056 store . upsert ( next . value ) ;
57+ metastore . upsert ( filekey , next . value ) ;
5158 }
5259 break ;
5360 case "roots" :
@@ -58,6 +65,7 @@ export class FigmaDesignRepository {
5865 __type : "file-fetched-for-app" ,
5966 } as TFetchFileForApp ;
6067 store . upsert ( next . value ) ;
68+ metastore . upsert ( filekey , next . value ) ;
6169 }
6270 break ;
6371 case "whole" :
@@ -67,6 +75,7 @@ export class FigmaDesignRepository {
6775 __type : "file-fetched-for-app" ,
6876 } as TFetchFileForApp ;
6977 store . upsert ( next . value ) ;
78+ metastore . upsert ( filekey , next . value ) ;
7079 break ;
7180 }
7281 }
0 commit comments