diff --git a/packages/frontend-core/src/components/grid/stores/cache.js b/packages/frontend-core/src/components/grid/stores/cache.ts similarity index 63% rename from packages/frontend-core/src/components/grid/stores/cache.js rename to packages/frontend-core/src/components/grid/stores/cache.ts index cf4690f15b..999a3ea11d 100644 --- a/packages/frontend-core/src/components/grid/stores/cache.js +++ b/packages/frontend-core/src/components/grid/stores/cache.ts @@ -1,16 +1,31 @@ -export const createActions = context => { +import { FindTableResponse } from "@budibase/types" +import { Store as StoreContext } from "." + +interface CacheActionStore { + cache: { + actions: { + getPrimaryDisplayForTableId: (tableId: string) => Promise + getTable: (tableId: string) => Promise + resetCache: () => any + } + } +} + +export type Store = CacheActionStore + +export const createActions = (context: StoreContext): CacheActionStore => { const { API } = context // Cache for the primary display columns of different tables. // If we ever need to cache table definitions for other purposes then we can // expand this to be a more generic cache. - let tableCache = {} + let tableCache: Record> = {} const resetCache = () => { tableCache = {} } - const fetchTable = async tableId => { + const fetchTable = async (tableId: string) => { // If we've never encountered this tableId before then store a promise that // resolves to the primary display so that subsequent invocations before the // promise completes can reuse this promise @@ -21,13 +36,13 @@ export const createActions = context => { return await tableCache[tableId] } - const getPrimaryDisplayForTableId = async tableId => { + const getPrimaryDisplayForTableId = async (tableId: string) => { const table = await fetchTable(tableId) const display = table?.primaryDisplay || table?.schema?.[0]?.name return display } - const getTable = async tableId => { + const getTable = async (tableId: string) => { const table = await fetchTable(tableId) return table } @@ -43,7 +58,7 @@ export const createActions = context => { } } -export const initialise = context => { +export const initialise = (context: StoreContext) => { const { datasource, cache } = context // Wipe the caches whenever the datasource changes to ensure we aren't diff --git a/packages/frontend-core/src/components/grid/stores/index.ts b/packages/frontend-core/src/components/grid/stores/index.ts index 9581f9ff7c..755e69a0fa 100644 --- a/packages/frontend-core/src/components/grid/stores/index.ts +++ b/packages/frontend-core/src/components/grid/stores/index.ts @@ -106,7 +106,8 @@ export type Store = BaseStore & Reorder.Store & Resize.Store & Config.Store & - Conditions.Store + Conditions.Store & + Cache.Store export const attachStores = (context: Store): Store => { // Atomic store creation