From 7ca897f7b0826193b46809712082356873bbeb11 Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Thu, 26 Jan 2023 00:10:53 +0700 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B2=D0=B5=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BD=D0=B0=D1=81=D0=BB=D0=B5=D0=B4=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BA=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/schema-client.ts | 23 ++++++++++++++++++++--- api/sm/sm-client.ts | 30 ++++++++++++------------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/api/schema-client.ts b/api/schema-client.ts index aaef855..e48a3ce 100644 --- a/api/schema-client.ts +++ b/api/schema-client.ts @@ -19,14 +19,25 @@ export interface SchemaInterface { contentType: string | null } +export class Context { + debouncing: number | null = null +} + export class SchemaClient { - baseUrl: string + baseUrl: string | null = null + context: Context | null = null - constructor({baseUrl}: ClientOptions) { - this.baseUrl = baseUrl + debouncing(time: number) + { + let context = this.grabContext() + context.debouncing = time + this.context = context + return this } async send(schema: SchemaInterface, data: any) { + let context = this.context + this.context = null let url = `${this.baseUrl}${schema.url}` let {url: preparedUrl, data: preparedData} = this.processAttributes(url, data) @@ -46,6 +57,7 @@ export class SchemaClient { let responseData = response.headers.get('Content-Type')?.toString().includes('application/json') ? await response.json() : await response.text() + return {responseData: responseData, headers: response.headers} } @@ -60,4 +72,9 @@ export class SchemaClient { } return {url, data: preparedData } } + + private grabContext(): Context + { + return this.context || new Context() + } } \ No newline at end of file diff --git a/api/sm/sm-client.ts b/api/sm/sm-client.ts index 2d6b667..0ba4f32 100644 --- a/api/sm/sm-client.ts +++ b/api/sm/sm-client.ts @@ -17,17 +17,11 @@ import stopProcessSchema from "./schemas/stop-process"; import killProcessSchema from "./schemas/kill-process"; import commandSchema from "./schemas/command"; -export class SMClient { - schemaClient: SchemaClient - - constructor() { - this.schemaClient = new SchemaClient({ - baseUrl: 'http://fmw.sipachev.sv' - }) - } +export class SMClient extends SchemaClient { + baseUrl = 'http://fmw.sipachev.sv' async getCommands(): Promise> { - let { responseData, headers } = await this.schemaClient.send(commandsSchema, {}) + let { responseData, headers } = await this.send(commandsSchema, {}) return { data: responseData, headers: headers @@ -35,7 +29,7 @@ export class SMClient { } async getCommand(name: string): Promise> { - let { responseData, headers } = await this.schemaClient.send(commandSchema, { + let { responseData, headers } = await this.send(commandSchema, { commandName: name }) return { @@ -45,7 +39,7 @@ export class SMClient { } async runCommand(data: any): Promise { - let { headers } = await this.schemaClient.send(runCommandsSchema, data) + let { headers } = await this.send(runCommandsSchema, data) return { data: null, headers: headers @@ -53,7 +47,7 @@ export class SMClient { } async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise> { - let { responseData, headers } = await this.schemaClient.send(processesSchema, data) + let { responseData, headers } = await this.send(processesSchema, data) return { data: responseData, headers: headers @@ -61,7 +55,7 @@ export class SMClient { } async repeatProcess(data: RepeatProcessRequest): Promise { - let { responseData, headers } = await this.schemaClient.send(repeatProcessSchema, data) + let { responseData, headers } = await this.send(repeatProcessSchema, data) return { data: responseData as ProcessesResponseInterface[], headers: headers @@ -69,7 +63,7 @@ export class SMClient { } async getProcessOutput(data: ProcessOutputRequest): Promise { - let { responseData, headers } = await this.schemaClient.send(processOutputSchema, data) + let { responseData, headers } = await this.send(processOutputSchema, data) return { data: responseData, headers: headers @@ -77,7 +71,7 @@ export class SMClient { } async playProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(playProcessSchema, {id}) + let { headers } = await this.send(playProcessSchema, {id}) return { data: null, headers: headers @@ -85,7 +79,7 @@ export class SMClient { } async pauseProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(pauseProcessSchema, {id}) + let { headers } = await this.send(pauseProcessSchema, {id}) return { data: null, headers: headers @@ -93,7 +87,7 @@ export class SMClient { } async stopProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(stopProcessSchema, {id}) + let { headers } = await this.send(stopProcessSchema, {id}) return { data: null, headers: headers @@ -101,7 +95,7 @@ export class SMClient { } async killProcess(id: string): Promise { - let { headers } = await this.schemaClient.send(killProcessSchema, {id}) + let { headers } = await this.send(killProcessSchema, {id}) return { data: null, headers: headers