From 60be1346596c3413134d97dd195567ba2a2e5314 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 19 Jul 2023 10:32:10 -0400 Subject: [PATCH] feat: initial version --- README.md | 2 +- package.json | 13 ++++++++++++ src/index.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 package.json create mode 100644 src/index.ts diff --git a/README.md b/README.md index a831e52..fb2ee72 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# kernel-module-network-registry +# kernel-network-registry diff --git a/package.json b/package.json new file mode 100644 index 0000000..7363ab4 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..84a5639 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,57 @@ +import { ActiveQuery, addHandler } from "@lumeweb/libkernel/module"; + +const types: Set = new Set(); +const networks: Map> = new Map>(); + +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).values(), + ]); +}