2023-02-05 15:26:29 +00:00
|
|
|
import EventEmitter from "eventemitter3";
|
2023-01-31 10:02:52 +00:00
|
|
|
let callModule;
|
|
|
|
let connectModule;
|
|
|
|
let log;
|
|
|
|
let logErr;
|
2023-02-01 12:42:32 +00:00
|
|
|
export class Client extends EventEmitter {
|
2023-01-31 10:02:52 +00:00
|
|
|
_callModule;
|
|
|
|
get callModule() {
|
|
|
|
return this._callModule;
|
|
|
|
}
|
2023-02-17 02:06:32 +00:00
|
|
|
_log;
|
|
|
|
get log() {
|
|
|
|
return this._log;
|
|
|
|
}
|
|
|
|
_logErr;
|
|
|
|
get logErr() {
|
|
|
|
return this._logErr;
|
|
|
|
}
|
2023-01-31 10:02:52 +00:00
|
|
|
_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);
|
2023-02-01 17:28:30 +00:00
|
|
|
this.handleError(ret);
|
|
|
|
return ret;
|
2023-01-31 10:02:52 +00:00
|
|
|
};
|
|
|
|
this._connectModule = moduleBag.connectModule;
|
2023-02-17 02:06:32 +00:00
|
|
|
this._log = log;
|
|
|
|
this._logErr = logErr;
|
2023-01-31 10:02:52 +00:00
|
|
|
}
|
|
|
|
async loadBound(module) {
|
|
|
|
return (await load(module));
|
|
|
|
}
|
|
|
|
handleError(ret) {
|
|
|
|
if (ret[1]) {
|
|
|
|
throw new Error(ret[1]);
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 12:27:20 +00:00
|
|
|
handleErrorOrReturn(ret) {
|
|
|
|
this.handleError(ret);
|
|
|
|
return ret[0];
|
|
|
|
}
|
2023-01-31 10:02:52 +00:00
|
|
|
async callModuleReturn(method, data) {
|
|
|
|
const ret = await this.callModule(method, data);
|
|
|
|
return ret[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export async function load(module) {
|
2023-01-31 12:43:04 +00:00
|
|
|
if (callModule && connectModule) {
|
2023-01-31 10:02:52 +00:00
|
|
|
if (module) {
|
|
|
|
return {
|
|
|
|
callModule: callModule.bind(undefined, module),
|
|
|
|
connectModule: connectModule.bind(undefined, module),
|
|
|
|
log,
|
|
|
|
logErr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
callModule,
|
|
|
|
connectModule,
|
|
|
|
log,
|
|
|
|
logErr,
|
|
|
|
};
|
|
|
|
}
|
2023-01-31 12:49:03 +00:00
|
|
|
const pkg = typeof window !== "undefined" && window?.document
|
|
|
|
? await import("libkernel")
|
|
|
|
: await import("libkmodule");
|
2023-01-31 10:02:52 +00:00
|
|
|
callModule = pkg.callModule;
|
|
|
|
connectModule = pkg.connectModule;
|
2023-01-31 12:49:03 +00:00
|
|
|
// @ts-ignore
|
2023-01-31 10:02:52 +00:00
|
|
|
log = pkg.log;
|
2023-01-31 12:49:03 +00:00
|
|
|
// @ts-ignore
|
2023-01-31 10:02:52 +00:00
|
|
|
logErr = pkg.logErr;
|
|
|
|
return load(module);
|
|
|
|
}
|
|
|
|
export const factory = function (type, module) {
|
|
|
|
return function (...args) {
|
|
|
|
return new Proxy(new type(...args), {
|
|
|
|
get(target, property) {
|
2023-01-31 13:05:20 +00:00
|
|
|
const prop = target[property];
|
|
|
|
if (typeof prop !== "function") {
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
return async (...args) => {
|
2023-01-31 10:02:52 +00:00
|
|
|
await target.loadLibs(module);
|
2023-01-31 13:05:20 +00:00
|
|
|
return target[property](...args);
|
2023-01-31 10:02:52 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|