refactor: change how module calling works

This commit is contained in:
Derrick Hammer 2023-06-28 00:26:22 -04:00
parent caae937352
commit 021fcf424e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 42 additions and 8 deletions

View File

@ -1,13 +1,47 @@
import EventEmitter from "eventemitter2"; import EventEmitter from "eventemitter2";
import { callModule } from "../api.js"; import { callModule, connectModule, log, logErr } from "../api.js";
import { ErrTuple } from "@lumeweb/libweb"; import { DataFn, ErrTuple } from "@lumeweb/libweb";
type callModuleBound = (method: string, data?: any) => Promise<ErrTuple>;
type connectModuleBound = (
method: string,
data: any,
receiveUpdate: DataFn,
) => [sendUpdate: DataFn, response: Promise<ErrTuple>];
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 { export abstract class Client extends EventEmitter {
private async _callModule(...args) { private _module: string;
// @ts-ignore
const ret = await callModule(...args); constructor(module: string) {
this.handleError(ret); super();
return ret; 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 { protected handleError(ret: ErrTuple): void {
@ -22,7 +56,7 @@ export abstract class Client extends EventEmitter {
} }
protected async callModuleReturn(method: string, data?: any): Promise<any> { protected async callModuleReturn(method: string, data?: any): Promise<any> {
const ret = await callModule(method, data); const ret = await this.callModule(method, data);
return ret[0]; return ret[0];
} }