From f1cdc39a318074815652dc1af42cac93eb0935f7 Mon Sep 17 00:00:00 2001 From: 8e8b2c <8e8b2c@proton.me> Date: Mon, 9 Sep 2024 12:11:00 +0100 Subject: [PATCH] chore: add publisher listing to example --- examples/emissions/co2-sensor/co2-sensor.ts | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/examples/emissions/co2-sensor/co2-sensor.ts b/examples/emissions/co2-sensor/co2-sensor.ts index 99f4421..3830332 100644 --- a/examples/emissions/co2-sensor/co2-sensor.ts +++ b/examples/emissions/co2-sensor/co2-sensor.ts @@ -6,6 +6,7 @@ import { ensureAndConnectToHapp } from "@holoom/sandbox"; import { UsernameRegistryCoordinator } from "@holoom/types"; +import { AgentPubKey } from "@holochain/client"; async function main() { // Create a conductor sandbox (with holoom installed) at the specified @@ -22,9 +23,34 @@ async function main() { ); const usernameRegistryCoordinator = new UsernameRegistryCoordinator(appWs); + await ensureListedAsPublisher(appWs.myPubKey, usernameRegistryCoordinator); + setInterval(() => publishMeasurement(usernameRegistryCoordinator), 1_000); } +async function ensureListedAsPublisher( + myPubkey: AgentPubKey, + usernameRegistryCoordinator: UsernameRegistryCoordinator +) { + const publishers = await usernameRegistryCoordinator.getAllPublishers(); + for (const [agent] of publishers) { + if (arrEqual(agent, myPubkey)) { + console.log("Already listed as publisher"); + return; + } + } + console.log("Listing self as publisher"); + await usernameRegistryCoordinator.registerAsPublisher("co2-sensor"); +} + +function arrEqual(arr1: Uint8Array, arr2: Uint8Array): boolean { + if (arr1.length !== arr2.length) return false; + for (let i = 0; i < arr1.length; i++) { + if (arr1![i] !== arr2[i]) return false; + } + return true; +} + async function publishMeasurement( usernameRegistryCoordinator: UsernameRegistryCoordinator ) {