From 72f1ae89be33f29ff240ea4536fc9d69544bfa3b Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Fri, 27 Jan 2023 17:18:33 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D1=8B=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/schema-client.ts | 40 ++++++++++++++++++++++---- components/elements/commands/index.tsx | 2 +- 2 files changed, 36 insertions(+), 6 deletions(-) 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) }