Skip to content

Commit

Permalink
chore: --
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <[email protected]>
  • Loading branch information
sfroment committed Feb 4, 2025
1 parent 672e0f5 commit ce1453c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 120 deletions.
33 changes: 10 additions & 23 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";

import { Message } from "./proto/drp/network/v1/messages_pb.js";
import { uint8ArrayToStream } from "./stream.js";
import { waitForEvent } from "./utils/waiter.js";

export * from "./stream.js";

Expand Down Expand Up @@ -242,32 +241,20 @@ export class DRPNetworkNode {
await this.start();
}

async isDialable(timeout = 5000) {
return waitForEvent(async (resolve, reject) => {
if (!this._node) {
resolve(false);
return;
}
async isDialable(callback: () => void | Promise<void>) {
if (await this._node?.isDialable(this._node.getMultiaddrs())) {
await callback();
return;
}

const checkDialable = async () => {
if (await this._node?.isDialable(this._node.getMultiaddrs())) {
resolve(true);
return;
this._node?.removeEventListener("transport:listening", checkDialable);
await callback();
}
};

const checkDialable = async () => {
try {
if (await this._node?.isDialable(this._node.getMultiaddrs())) {
resolve(true);
}
} catch (error) {
reject(error);
} finally {
this._node?.removeEventListener("transport:listening", checkDialable);
}
};

this._node.addEventListener("transport:listening", checkDialable);
}, timeout);
this._node?.addEventListener("transport:listening", checkDialable);
}

private _sortAddresses(a: Address, b: Address) {
Expand Down
29 changes: 0 additions & 29 deletions packages/network/src/utils/waiter.ts

This file was deleted.

24 changes: 22 additions & 2 deletions packages/network/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,33 @@ describe("isDialable", () => {
await btNode.start();
});

const isDialable = async (node: DRPNetworkNode, timeout = false) => {
let resolver: (value: boolean) => void;
const promise = new Promise<boolean>((resolve) => {
resolver = resolve;
});

if (timeout) {
setTimeout(() => {
resolver(false);
}, 10);
}

const callback = () => {
resolver(true);
};

await node.isDialable(callback);
return await promise;
};

test("should return true if the node is dialable", async () => {
const node = new DRPNetworkNode({
bootstrap_peers: btNode.getMultiaddrs()?.map((addr) => addr.toString()) || [],
private_key_seed: "is_dialable_node_1",
});
await node.start();
expect(await node.isDialable(100)).toBe(true);
expect(await isDialable(node)).toBe(true);
});

test("should return false if the node is not dialable", async () => {
Expand All @@ -30,6 +50,6 @@ describe("isDialable", () => {
listen_addresses: [],
});
await node.start();
expect(await node.isDialable(100)).toBe(false);
expect(await isDialable(node, true)).toBe(false);
});
});
66 changes: 0 additions & 66 deletions packages/network/tests/utils.test.ts

This file was deleted.

0 comments on commit ce1453c

Please sign in to comment.