Skip to content

Commit

Permalink
chore: add publisher listing to example
Browse files Browse the repository at this point in the history
  • Loading branch information
8e8b2c committed Sep 9, 2024
1 parent 0534125 commit f1cdc39
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/emissions/co2-sensor/co2-sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
) {
Expand Down

0 comments on commit f1cdc39

Please sign in to comment.