feat: initial version

This commit is contained in:
Derrick Hammer 2023-07-19 10:32:10 -04:00
parent 73360f32ea
commit 60be134659
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 71 additions and 1 deletions

View File

@ -1,2 +1,2 @@
# kernel-module-network-registry # kernel-network-registry

13
package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "@lumeweb/kernel-network-registry",
"version": "0.1.0",
"type": "module",
"readme": "ERROR: No README data found!",
"_id": "@lumeweb/kernel-module-network-registry@0.1.0",
"scripts": {
"prepare": "presetter bootstrap"
},
"dependencies": {
"@lumeweb/libkernel": "^0.1.0-develop.20"
}
}

57
src/index.ts Normal file
View File

@ -0,0 +1,57 @@
import { ActiveQuery, addHandler } from "@lumeweb/libkernel/module";
const types: Set<string> = new Set<string>();
const networks: Map<string, Set<string>> = new Map<string, Set<string>>();
addHandler("registerType", handleRegisterType);
addHandler("getTypes", handleGetTypes);
addHandler("getNetworkTypes", handleGetNetworkType);
addHandler("registerNetwork", handleRegisterNetwork);
function handleRegisterType(aq: ActiveQuery) {
types.add(aq.callerInput);
aq.respond();
}
function handleGetTypes(aq: ActiveQuery) {
aq.respond([...types.values()]);
}
function handleRegisterNetwork(aq: ActiveQuery) {
if (!("types" in aq.callerInput)) {
aq.reject("types missing");
return;
}
if (!Array.isArray(aq.callerInput.types)) {
aq.reject("types must be an array");
return;
}
let network = networks.get(aq.domain);
if (network) {
aq.callerInput.type.forEach((item) => network?.add(item));
} else {
networks.set(aq.domain, new Set([aq.callerInput.types]));
}
aq.respond();
}
function handleGetNetworkType(aq: ActiveQuery) {
if (!("module" in aq.callerInput)) {
aq.reject("module missing");
return;
}
if (!networks.has(aq.callerInput.module)) {
aq.reject("module is not registered");
return;
}
aq.respond([
...(networks.get(aq.callerInput.module) as Set<string>).values(),
]);
}