From e73b9b3b7b57de8f7c8dabe8118011885cf417b5 Mon Sep 17 00:00:00 2001 From: mrinc Date: Sat, 16 Dec 2023 01:48:50 +0200 Subject: [PATCH] Updated client code --- nodejs/src/client.ts | 75 ++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/nodejs/src/client.ts b/nodejs/src/client.ts index 1df8176..42af88b 100644 --- a/nodejs/src/client.ts +++ b/nodejs/src/client.ts @@ -4,38 +4,53 @@ import { BSBServiceClient, BSBServiceConstructor, } from "./base"; -import { LoggingConfig, EventsConfig, PluginDefition, PluginType, DEBUG_MODE } from './interfaces'; +import { + LoggingConfig, + EventsConfig, + PluginDefition, + PluginType, + DEBUG_MODE, +} from "./interfaces"; import { SBLogging, ServiceBase } from "./serviceBase"; -import {Plugin as DefaultConfig} from './plugins/config-default/plugin'; +import { randomUUID } from "crypto"; export class SBClient { private serviceBase: ServiceBase; public client!: Client; private useDefaultConfigPlugin: boolean; + private configSetup: boolean = false; constructor(useDefaultConfigPlugin: boolean = false) { this.useDefaultConfigPlugin = useDefaultConfigPlugin; const CWD = process.env.APP_DIR || process.cwd(); this.serviceBase = new ServiceBase(false, true, CWD); } - public async initAndRun(client: typeof BSBServiceClient, ...args: any[]) { - if (!this.useDefaultConfigPlugin) - this.serviceBase.setConfigPlugin("config-bsb-internal-client", FakeServiceConfig); - + public async addClient(client: typeof BSBServiceClient, ...args: any[]) { + if (!this.useDefaultConfigPlugin && !this.configSetup) { + this.configSetup = true; + this.serviceBase.setConfigPlugin( + "config-bsb-internal-client", + FakeServiceConfig + ); + } const service = this.serviceBase.addService( - "service-bsb-internal-client", + "service-bsb-internal-client-" + randomUUID(), FakeServiceClient, {} ); - this.client = new (client as any)(service, ...args); + return new (client as any)(service, ...args); + } + public async init() { await this.serviceBase.init(); + } + public async run() { await this.serviceBase.run(); } } -class FakeServiceClient extends BSBService { +export class FakeServiceClient extends BSBService { public initBeforePlugins?: string[] | undefined; public initAfterPlugins?: string[] | undefined; public runBeforePlugins?: string[] | undefined; @@ -53,31 +68,37 @@ class FakeServiceClient extends BSBService { } } -class FakeServiceConfig extends BSBConfig { - private config: DefaultConfig; - constructor(appId: string, mode: DEBUG_MODE, pluginName: string, cwd: string, pluginCwd: string, logging: SBLogging) { - super(appId, mode, pluginName, cwd, pluginCwd, logging) - this.config = new DefaultConfig(appId, mode, pluginName, cwd, pluginCwd, logging); +export class FakeServiceConfig extends BSBConfig { + constructor( + appId: string, + mode: DEBUG_MODE, + pluginName: string, + cwd: string, + pluginCwd: string, + logging: SBLogging + ) { + super(appId, mode, pluginName, cwd, pluginCwd, logging); } async getLoggingPlugins(): Promise> { - return this.config.getLoggingPlugins(); + return {}; } async getEventsPlugins(): Promise> { - return this.config.getEventsPlugins(); + return {}; } async getServicePlugins(): Promise> { - return this.config.getServicePlugins(); + return {}; } - async getServicePluginDefinition(pluginName: string): Promise<{ name: string; enabled: boolean; }> { - return this.config.getServicePluginDefinition(pluginName); + async getServicePluginDefinition( + pluginName: string + ): Promise<{ name: string; enabled: boolean }> { + return { name: pluginName, enabled: false }; } - async getPluginConfig(pluginType: PluginType, plugin: string): Promise { - return this.config.getPluginConfig(pluginType, plugin); - } - dispose() { - this.config.dispose(); - } - init() { - this.config.init(); + async getPluginConfig( + pluginType: PluginType, + plugin: string + ): Promise { + return null; } + dispose?(): void; + init?(): void; }