From 021fcf424eff85b11c9c1eda908ccd6abebf1aad Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 28 Jun 2023 00:26:22 -0400 Subject: [PATCH] refactor: change how module calling works --- src/module/client.ts | 50 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/module/client.ts b/src/module/client.ts index 3b8b006..f58ca40 100644 --- a/src/module/client.ts +++ b/src/module/client.ts @@ -1,13 +1,47 @@ import EventEmitter from "eventemitter2"; -import { callModule } from "../api.js"; -import { ErrTuple } from "@lumeweb/libweb"; +import { callModule, connectModule, log, logErr } from "../api.js"; +import { DataFn, ErrTuple } from "@lumeweb/libweb"; + +type callModuleBound = (method: string, data?: any) => Promise; +type connectModuleBound = ( + method: string, + data: any, + receiveUpdate: DataFn, +) => [sendUpdate: DataFn, response: Promise]; + +interface ModuleBag { + callModule: typeof callModule; + connectModule: typeof connectModule; + log: typeof log; + logErr: typeof logErr; +} + +interface ModuleBagBound extends ModuleBag { + callModule: callModuleBound; + connectModule: connectModuleBound; +} export abstract class Client extends EventEmitter { - private async _callModule(...args) { - // @ts-ignore - const ret = await callModule(...args); - this.handleError(ret); - return ret; + private _module: string; + + constructor(module: string) { + super(); + this._module = module; + } + + get callModule(): callModuleBound { + return this.getBound(this._module).callModule; + } + + get connectModule(): connectModuleBound { + return this.getBound(this._module).connectModule; + } + + public getBound(module: string): ModuleBagBound { + return { + callModule: callModule.bind(undefined, module), + connectModule: connectModule.bind(undefined, module), + } as ModuleBagBound; } protected handleError(ret: ErrTuple): void { @@ -22,7 +56,7 @@ export abstract class Client extends EventEmitter { } protected async callModuleReturn(method: string, data?: any): Promise { - const ret = await callModule(method, data); + const ret = await this.callModule(method, data); return ret[0]; }