Type cache

This commit is contained in:
Adria Navarro 2024-12-30 13:21:06 +01:00
parent 901e3eaba4
commit 350c91012f
2 changed files with 23 additions and 7 deletions

View File

@ -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<string>
getTable: (tableId: string) => Promise<FindTableResponse>
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<string, Promise<FindTableResponse>> = {}
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

View File

@ -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