feat: add getNetworkModuleStatus to fetch a network module status, either one time or continuous

This commit is contained in:
Derrick Hammer 2023-07-23 13:27:05 -04:00
parent b55782db8f
commit 19167acec2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 43 additions and 2 deletions

View File

@ -10,6 +10,8 @@ import {
log as logModule,
logErr as logErrModule,
} from "#module/index.js";
import defer, { DeferredPromise } from "p-defer";
import { connectModuleBound } from "#module/client.js";
// @ts-ignore
const kernelEnv = typeof window !== "undefined" && window?.document;
@ -20,3 +22,42 @@ export const connectModule = kernelEnv
: connectModuleModule;
export const log = kernelEnv ? logKernel : logModule;
export const logErr = kernelEnv ? logErrKernel : logErrModule;
export async function getNetworkModuleStatus(
callback?: any,
module?: string,
// @ts-ignore
CM: connectModuleBound = connectModule.bind(null, module),
): Promise<void | (() => Promise<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 async () => {
if (closed) {
return;
}
closed = true;
close();
};
}

View File

@ -2,8 +2,8 @@ import Emittery from "emittery";
import { callModule, connectModule, log, logErr } from "../api.js";
import { DataFn, ErrTuple } from "@lumeweb/libweb";
type callModuleBound = (method: string, data?: any) => Promise<ErrTuple>;
type connectModuleBound = (
export type callModuleBound = (method: string, data?: any) => Promise<ErrTuple>;
export type connectModuleBound = (
method: string,
data: any,
receiveUpdate: DataFn,