diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..3ed238b --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,36 @@ +type callModule = typeof import("libkmodule").callModule; +type connectModule = typeof import("libkmodule").connectModule; +type logErr = typeof import("libkmodule").logErr; +type log = typeof import("libkmodule").log; +import type { ErrTuple, DataFn } from "@siaweb/libweb"; +type callModuleBound = (method: string, data?: any) => Promise; +type connectModuleBound = (method: string, data: any, receiveUpdate: DataFn) => [sendUpdate: DataFn, response: Promise]; +declare let callModule: callModule; +declare let connectModule: connectModule; +declare let log: log; +declare let logErr: logErr; +export interface ModuleBag { + callModule: callModule; + connectModule: connectModule; + log: log; + logErr: logErr; +} +export interface ModuleBagBound extends ModuleBag { + callModule: callModuleBound; + connectModule: connectModuleBound; +} +export declare abstract class Client { + private _callModule?; + get callModule(): callModuleBound; + private _connectModule?; + get connectModule(): connectModuleBound; + loadLibs(module: string): Promise; + loadBound(module: string): Promise; + protected handleError(ret: ErrTuple): void; + protected callModuleReturn(method: string, data?: any): Promise; +} +export declare function load(module?: string): Promise; +type ClientConstructor = new (...args: any[]) => U; +export declare const factory: (type: ClientConstructor, module: string) => (...args: any) => T; +export {}; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map new file mode 100644 index 0000000..e6be371 --- /dev/null +++ b/dist/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG,cAAc,YAAY,EAAE,UAAU,CAAC;AACzD,KAAK,aAAa,GAAG,cAAc,YAAY,EAAE,aAAa,CAAC;AAC/D,KAAK,MAAM,GAAG,cAAc,YAAY,EAAE,MAAM,CAAC;AACjD,KAAK,GAAG,GAAG,cAAc,YAAY,EAAE,GAAG,CAAC;AAE3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAA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MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,SAAS,GAAG,cAAc,CAAC,CA8BrC;AAED,KAAK,iBAAiB,CAAC,CAAC,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAEtD,eAAO,MAAM,OAAO,kEAEV,MAAM,eAEY,GAAG,MAW9B,CAAC"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..3a1345b --- /dev/null +++ b/dist/index.js @@ -0,0 +1,77 @@ +let callModule; +let connectModule; +let log; +let logErr; +export class Client { + _callModule; + get callModule() { + return this._callModule; + } + _connectModule; + get connectModule() { + return this._connectModule; + } + async loadLibs(module) { + if (this._callModule && this._connectModule) { + return; + } + const moduleBag = await this.loadBound(module); + this._callModule = async (...args) => { + const ret = await moduleBag.callModule(...args); + this.handleError(ret); + return ret; + }; + this._connectModule = moduleBag.connectModule; + } + async loadBound(module) { + return (await load(module)); + } + handleError(ret) { + if (ret[1]) { + throw new Error(ret[1]); + } + } + async callModuleReturn(method, data) { + const ret = await this.callModule(method, data); + return ret[0]; + } +} +export async function load(module) { + if (callModule !== null && connectModule !== null) { + if (module) { + return { + callModule: callModule.bind(undefined, module), + connectModule: connectModule.bind(undefined, module), + log, + logErr, + }; + } + return { + callModule, + connectModule, + log, + logErr, + }; + } + const pkgName = typeof window !== "undefined" && window?.document + ? "libkernel" + : "libkmodule"; + const pkg = await import(pkgName); + callModule = pkg.callModule; + connectModule = pkg.connectModule; + log = pkg.log; + logErr = pkg.logErr; + return load(module); +} +export const factory = function (type, module) { + return function (...args) { + return new Proxy(new type(...args), { + get(target, property) { + return async () => { + await target.loadLibs(module); + return target[property]; + }; + }, + }); + }; +};