libkernel/src/api.ts

64 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2023-06-28 02:35:12 +00:00
import {
callModule as callModuleKernel,
connectModule as connectModuleKernel,
log as logKernel,
logErr as logErrKernel,
} from "#kernel/index.js";
import {
callModule as callModuleModule,
connectModule as connectModuleModule,
log as logModule,
logErr as logErrModule,
} from "#module/index.js";
import defer, { DeferredPromise } from "p-defer";
import { connectModuleBound } from "#module/client.js";
2023-06-28 02:35:12 +00:00
// @ts-ignore
2023-06-29 06:40:48 +00:00
const kernelEnv = typeof window !== "undefined" && window?.document;
2023-06-28 02:35:12 +00:00
export const callModule = kernelEnv ? callModuleKernel : callModuleModule;
export const connectModule = kernelEnv
? connectModuleKernel
: connectModuleModule;
export const log = kernelEnv ? logKernel : logModule;
export const logErr = kernelEnv ? logErrKernel : logErrModule;
export function getNetworkModuleStatus(
callback?: any,
module?: string,
// @ts-ignore
CM: connectModuleBound = connectModule.bind(null, module),
): Promise<void> | (() => void) {
let recvUpdate = (data) => {
callback?.(data);
};
const [close, resp] = CM("status", null, (data) => {
recvUpdate(data);
});
if (!callback) {
return new Promise(async (resolve) => {
const d = defer();
recvUpdate = (data) => {
resolve(data);
d.resolve();
};
await d.promise;
close();
});
}
let closed = false;
return () => {
if (closed) {
return;
}
closed = true;
close();
};
}