2023-07-19 14:32:10 +00:00
|
|
|
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);
|
2023-07-19 15:18:34 +00:00
|
|
|
addHandler("getNetworkTypes", handleGetNetworkTypes);
|
2023-07-19 17:14:16 +00:00
|
|
|
addHandler("getNetworksByType", handleGetNetworksByType);
|
2023-07-19 14:32:10 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-07-19 15:18:34 +00:00
|
|
|
function handleGetNetworkTypes(aq: ActiveQuery) {
|
2023-07-19 14:32:10 +00:00
|
|
|
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(),
|
|
|
|
]);
|
|
|
|
}
|
2023-07-19 17:14:16 +00:00
|
|
|
|
|
|
|
function handleGetNetworksByType(aq: ActiveQuery) {
|
|
|
|
if (!("type" in aq.callerInput)) {
|
|
|
|
aq.reject("type missing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!types.has(aq.callerInput)) {
|
|
|
|
aq.reject("type not registered");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aq.respond(
|
|
|
|
[...networks.entries()]
|
|
|
|
.filter((item) => {
|
|
|
|
return item[1].has(aq.callerInput);
|
|
|
|
})
|
|
|
|
.map((item) => item[0]),
|
|
|
|
);
|
|
|
|
}
|