diff --git a/api/schema-client.ts b/api/schema-client.ts index e48a3ce..2886527 100644 --- a/api/schema-client.ts +++ b/api/schema-client.ts @@ -20,24 +20,45 @@ export interface SchemaInterface { } export class Context { - debouncing: number | null = null + debouncing?: number + useMemory?: boolean + memoryKey?: string } export class SchemaClient { baseUrl: string | null = null context: Context | null = null + memory: Record = {} + debouncing(time: number) { let context = this.grabContext() context.debouncing = time - this.context = context + return this + } + + useMemory(key?: string) + { + let context = this.grabContext() + context.useMemory = true + context.memoryKey = key return this } async send(schema: SchemaInterface, data: any) { let context = this.context this.context = null + + if (context?.useMemory) { + let memoryKey = context?.memoryKey || schema.url + if (typeof this.memory[memoryKey] !== 'undefined') { + return this.memory[memoryKey] + } + } + + console.log('context', this.context, context) + let url = `${this.baseUrl}${schema.url}` let {url: preparedUrl, data: preparedData} = this.processAttributes(url, data) @@ -58,7 +79,13 @@ export class SchemaClient { ? await response.json() : await response.text() - return {responseData: responseData, headers: response.headers} + let result = {responseData: responseData, headers: response.headers} + if (context?.useMemory) { + let memoryKey = context?.memoryKey || schema.url + this.memory[memoryKey] = result + } + + return result } private processAttributes(url: string, data: any) @@ -75,6 +102,9 @@ export class SchemaClient { private grabContext(): Context { - return this.context || new Context() + if (this.context) { + return this.context + } + return this.context = new Context() } -} \ No newline at end of file +} diff --git a/components/elements/commands/index.tsx b/components/elements/commands/index.tsx index ea92228..9014b1d 100644 --- a/components/elements/commands/index.tsx +++ b/components/elements/commands/index.tsx @@ -20,7 +20,7 @@ export default function Commands() { const [open, setOpen] = useState(false); let refreshCommands = async () => { - const { data: commands } = await smClient.getCommands() + const { data: commands } = await smClient.useMemory().getCommands() setCommands(commands) }