mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-05 23:33:08 +00:00
feat: StoreConnect events (#2601)
* store connect evt: use enum instead of free strings for Waku event types * store connect evt: more accurate enum name * store connect evt: add store connect event on peer manager * store connect evt: simplify logic statements * store connect evt: test store connect * store connect evt: export event types * test: use enum * Shorter name for waku events
This commit is contained in:
parent
78c856d079
commit
0dfbcf6b6b
@ -391,7 +391,7 @@ test.describe("Waku Server API", () => {
|
|||||||
});
|
});
|
||||||
await axios.post(`${API_URL}/admin/v1/start-node`);
|
await axios.post(`${API_URL}/admin/v1/start-node`);
|
||||||
|
|
||||||
// Connect to peers
|
// FilterConnect to peers
|
||||||
const dialResponse = await axios.post(`${API_URL}/admin/v1/peers`, {
|
const dialResponse = await axios.post(`${API_URL}/admin/v1/peers`, {
|
||||||
peerMultiaddrs: PEERS
|
peerMultiaddrs: PEERS
|
||||||
});
|
});
|
||||||
@ -425,7 +425,7 @@ test.describe("Waku Server API", () => {
|
|||||||
});
|
});
|
||||||
await axios.post(`${API_URL}/admin/v1/start-node`);
|
await axios.post(`${API_URL}/admin/v1/start-node`);
|
||||||
|
|
||||||
// Connect to peers
|
// FilterConnect to peers
|
||||||
await axios.post(`${API_URL}/admin/v1/peers`, {
|
await axios.post(`${API_URL}/admin/v1/peers`, {
|
||||||
peerMultiaddrs: PEERS
|
peerMultiaddrs: PEERS
|
||||||
});
|
});
|
||||||
@ -465,7 +465,7 @@ test.describe("Waku Server API", () => {
|
|||||||
});
|
});
|
||||||
await axios.post(`${API_URL}/admin/v1/start-node`);
|
await axios.post(`${API_URL}/admin/v1/start-node`);
|
||||||
|
|
||||||
// Connect to peers
|
// FilterConnect to peers
|
||||||
await axios.post(`${API_URL}/admin/v1/peers`, {
|
await axios.post(`${API_URL}/admin/v1/peers`, {
|
||||||
peerMultiaddrs: PEERS
|
peerMultiaddrs: PEERS
|
||||||
});
|
});
|
||||||
@ -577,7 +577,7 @@ test.describe("Waku Server API", () => {
|
|||||||
// Start node
|
// Start node
|
||||||
await axios.post(`${API_URL}/admin/v1/start-node`);
|
await axios.post(`${API_URL}/admin/v1/start-node`);
|
||||||
|
|
||||||
// Connect to peers
|
// FilterConnect to peers
|
||||||
await axios.post(`${API_URL}/admin/v1/peers`, {
|
await axios.post(`${API_URL}/admin/v1/peers`, {
|
||||||
peerMultiaddrs: PEERS
|
peerMultiaddrs: PEERS
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,7 +3,8 @@ import { multiaddr } from "@multiformats/multiaddr";
|
|||||||
import {
|
import {
|
||||||
CONNECTION_LOCKED_TAG,
|
CONNECTION_LOCKED_TAG,
|
||||||
IWakuEventEmitter,
|
IWakuEventEmitter,
|
||||||
Tags
|
Tags,
|
||||||
|
WakuEvent
|
||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
@ -143,7 +144,7 @@ describe("ConnectionLimiter", () => {
|
|||||||
.true;
|
.true;
|
||||||
expect(
|
expect(
|
||||||
(events.addEventListener as sinon.SinonStub).calledWith(
|
(events.addEventListener as sinon.SinonStub).calledWith(
|
||||||
"waku:connection",
|
WakuEvent.Connection,
|
||||||
sinon.match.func
|
sinon.match.func
|
||||||
)
|
)
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
@ -178,7 +179,7 @@ describe("ConnectionLimiter", () => {
|
|||||||
.true;
|
.true;
|
||||||
expect(
|
expect(
|
||||||
(events.removeEventListener as sinon.SinonStub).calledWith(
|
(events.removeEventListener as sinon.SinonStub).calledWith(
|
||||||
"waku:connection",
|
WakuEvent.Connection,
|
||||||
sinon.match.func
|
sinon.match.func
|
||||||
)
|
)
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
|
|||||||
@ -5,7 +5,8 @@ import {
|
|||||||
IWakuEventEmitter,
|
IWakuEventEmitter,
|
||||||
Libp2p,
|
Libp2p,
|
||||||
Libp2pEventHandler,
|
Libp2pEventHandler,
|
||||||
Tags
|
Tags,
|
||||||
|
WakuEvent
|
||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import { Logger } from "@waku/utils";
|
import { Logger } from "@waku/utils";
|
||||||
|
|
||||||
@ -69,7 +70,10 @@ export class ConnectionLimiter implements IConnectionLimiter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.events.addEventListener("waku:connection", this.onWakuConnectionEvent);
|
this.events.addEventListener(
|
||||||
|
WakuEvent.Connection,
|
||||||
|
this.onWakuConnectionEvent
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE: Event is not being emitted on closing nor losing a connection.
|
* NOTE: Event is not being emitted on closing nor losing a connection.
|
||||||
@ -90,7 +94,7 @@ export class ConnectionLimiter implements IConnectionLimiter {
|
|||||||
|
|
||||||
public stop(): void {
|
public stop(): void {
|
||||||
this.events.removeEventListener(
|
this.events.removeEventListener(
|
||||||
"waku:connection",
|
WakuEvent.Connection,
|
||||||
this.onWakuConnectionEvent
|
this.onWakuConnectionEvent
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -274,11 +278,9 @@ export class ConnectionLimiter implements IConnectionLimiter {
|
|||||||
.map((id) => this.getPeer(id))
|
.map((id) => this.getPeer(id))
|
||||||
);
|
);
|
||||||
|
|
||||||
const bootstrapPeers = peers.filter(
|
return peers.filter(
|
||||||
(peer) => peer && peer.tags.has(Tags.BOOTSTRAP)
|
(peer) => peer && peer.tags.has(Tags.BOOTSTRAP)
|
||||||
) as Peer[];
|
) as Peer[];
|
||||||
|
|
||||||
return bootstrapPeers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getPeer(peerId: PeerId): Promise<Peer | null> {
|
private async getPeer(peerId: PeerId): Promise<Peer | null> {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { IWakuEventEmitter, Libp2p } from "@waku/interfaces";
|
import { IWakuEventEmitter, Libp2p, WakuEvent } from "@waku/interfaces";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
|
|
||||||
@ -341,7 +341,7 @@ describe("NetworkMonitor", () => {
|
|||||||
const dispatchedEvent = dispatchEventStub.getCall(0)
|
const dispatchedEvent = dispatchEventStub.getCall(0)
|
||||||
.args[0] as CustomEvent<boolean>;
|
.args[0] as CustomEvent<boolean>;
|
||||||
expect(dispatchedEvent).to.be.instanceOf(CustomEvent);
|
expect(dispatchedEvent).to.be.instanceOf(CustomEvent);
|
||||||
expect(dispatchedEvent.type).to.equal("waku:connection");
|
expect(dispatchedEvent.type).to.equal(WakuEvent.Connection);
|
||||||
expect(dispatchedEvent.detail).to.be.true;
|
expect(dispatchedEvent.detail).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { IWakuEventEmitter, Libp2p } from "@waku/interfaces";
|
import { IWakuEventEmitter, Libp2p, WakuEvent } from "@waku/interfaces";
|
||||||
|
|
||||||
type NetworkMonitorConstructorOptions = {
|
type NetworkMonitorConstructorOptions = {
|
||||||
libp2p: Libp2p;
|
libp2p: Libp2p;
|
||||||
@ -104,7 +104,7 @@ export class NetworkMonitor implements INetworkMonitor {
|
|||||||
|
|
||||||
private dispatchNetworkEvent(): void {
|
private dispatchNetworkEvent(): void {
|
||||||
this.events.dispatchEvent(
|
this.events.dispatchEvent(
|
||||||
new CustomEvent<boolean>("waku:connection", {
|
new CustomEvent<boolean>(WakuEvent.Connection, {
|
||||||
detail: this.isConnected()
|
detail: this.isConnected()
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
@ -25,28 +25,33 @@ export type CreateEncoderParams = CreateDecoderParams & {
|
|||||||
ephemeral?: boolean;
|
ephemeral?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export enum WakuEvent {
|
||||||
|
Connection = "waku:connection",
|
||||||
|
Health = "waku:health"
|
||||||
|
}
|
||||||
|
|
||||||
export interface IWakuEvents {
|
export interface IWakuEvents {
|
||||||
/**
|
/**
|
||||||
* Emitted when a connection is established or lost.
|
* Emitted when a connection is established or lost.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* waku.addEventListener("waku:connection", (event) => {
|
* waku.addEventListener(WakuEvent.Connection, (event) => {
|
||||||
* console.log(event.detail); // true if connected, false if disconnected
|
* console.log(event.detail); // true if connected, false if disconnected
|
||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
"waku:connection": CustomEvent<boolean>;
|
[WakuEvent.Connection]: CustomEvent<boolean>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emitted when the health status changes.
|
* Emitted when the health status changes.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* waku.addEventListener("waku:health", (event) => {
|
* waku.addEventListener(WakuEvent.Health, (event) => {
|
||||||
* console.log(event.detail); // 'Unhealthy', 'MinimallyHealthy', or 'SufficientlyHealthy'
|
* console.log(event.detail); // 'Unhealthy', 'MinimallyHealthy', or 'SufficientlyHealthy'
|
||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
"waku:health": CustomEvent<HealthStatus>;
|
[WakuEvent.Health]: CustomEvent<HealthStatus>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type IWakuEventEmitter = TypedEventEmitter<IWakuEvents>;
|
export type IWakuEventEmitter = TypedEventEmitter<IWakuEvents>;
|
||||||
@ -61,12 +66,12 @@ export interface IWaku {
|
|||||||
/**
|
/**
|
||||||
* Emits events related to the Waku node.
|
* Emits events related to the Waku node.
|
||||||
* Those are:
|
* Those are:
|
||||||
* - "waku:connection"
|
* - WakuEvent.Connection
|
||||||
* - "waku:health"
|
* - WakuEvent.Health
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* waku.events.addEventListener("waku:connection", (event) => {
|
* waku.events.addEventListener(WakuEvent.Connection, (event) => {
|
||||||
* console.log(event.detail); // true if connected, false if disconnected
|
* console.log(event.detail); // true if connected, false if disconnected
|
||||||
* });
|
* });
|
||||||
* ```
|
* ```
|
||||||
|
|||||||
@ -363,11 +363,11 @@ export class Subscription {
|
|||||||
|
|
||||||
private setupEventListeners(): void {
|
private setupEventListeners(): void {
|
||||||
this.peerManager.events.addEventListener(
|
this.peerManager.events.addEventListener(
|
||||||
PeerManagerEventNames.Connect,
|
PeerManagerEventNames.FilterConnect,
|
||||||
this.onPeerConnected as Libp2pEventHandler
|
this.onPeerConnected as Libp2pEventHandler
|
||||||
);
|
);
|
||||||
this.peerManager.events.addEventListener(
|
this.peerManager.events.addEventListener(
|
||||||
PeerManagerEventNames.Disconnect,
|
PeerManagerEventNames.FilterDisconnect,
|
||||||
this.onPeerDisconnected as Libp2pEventHandler
|
this.onPeerDisconnected as Libp2pEventHandler
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -398,11 +398,11 @@ export class Subscription {
|
|||||||
|
|
||||||
private disposeEventListeners(): void {
|
private disposeEventListeners(): void {
|
||||||
this.peerManager.events.removeEventListener(
|
this.peerManager.events.removeEventListener(
|
||||||
PeerManagerEventNames.Connect,
|
PeerManagerEventNames.FilterConnect,
|
||||||
this.onPeerConnected as Libp2pEventHandler
|
this.onPeerConnected as Libp2pEventHandler
|
||||||
);
|
);
|
||||||
this.peerManager.events.removeEventListener(
|
this.peerManager.events.removeEventListener(
|
||||||
PeerManagerEventNames.Disconnect,
|
PeerManagerEventNames.FilterDisconnect,
|
||||||
this.onPeerDisconnected as Libp2pEventHandler
|
this.onPeerDisconnected as Libp2pEventHandler
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,11 @@
|
|||||||
import { Connection, Peer } from "@libp2p/interface";
|
import { Connection, Peer } from "@libp2p/interface";
|
||||||
import { FilterCodecs, LightPushCodec } from "@waku/core";
|
import { FilterCodecs, LightPushCodec } from "@waku/core";
|
||||||
import { HealthStatus, IWakuEventEmitter, Libp2p } from "@waku/interfaces";
|
import {
|
||||||
|
HealthStatus,
|
||||||
|
IWakuEventEmitter,
|
||||||
|
Libp2p,
|
||||||
|
WakuEvent
|
||||||
|
} from "@waku/interfaces";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
|
|
||||||
@ -34,8 +39,9 @@ describe("HealthIndicator", () => {
|
|||||||
|
|
||||||
// Start monitoring
|
// Start monitoring
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -53,8 +59,9 @@ describe("HealthIndicator", () => {
|
|||||||
healthIndicator.start();
|
healthIndicator.start();
|
||||||
|
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -76,8 +83,9 @@ describe("HealthIndicator", () => {
|
|||||||
healthIndicator.start();
|
healthIndicator.start();
|
||||||
|
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -131,8 +139,9 @@ describe("HealthIndicator", () => {
|
|||||||
peerStoreStub.withArgs(connection2.remotePeer).resolves(peer2);
|
peerStoreStub.withArgs(connection2.remotePeer).resolves(peer2);
|
||||||
|
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -144,8 +153,9 @@ describe("HealthIndicator", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const statusChangePromise2 = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise2 = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -166,8 +176,9 @@ describe("HealthIndicator", () => {
|
|||||||
sinon.stub(libp2p.peerStore, "get").resolves(peer);
|
sinon.stub(libp2p.peerStore, "get").resolves(peer);
|
||||||
|
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -189,8 +200,9 @@ describe("HealthIndicator", () => {
|
|||||||
sinon.stub(libp2p.peerStore, "get").rejects(new Error("Peer not found"));
|
sinon.stub(libp2p.peerStore, "get").rejects(new Error("Peer not found"));
|
||||||
|
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -217,8 +229,9 @@ describe("HealthIndicator", () => {
|
|||||||
peerStoreStub.withArgs(connection2.remotePeer).resolves(peer2);
|
peerStoreStub.withArgs(connection2.remotePeer).resolves(peer2);
|
||||||
|
|
||||||
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
const statusChangePromise = new Promise<HealthStatus>((resolve) => {
|
||||||
events.addEventListener("waku:health", (e: CustomEvent<HealthStatus>) =>
|
events.addEventListener(
|
||||||
resolve(e.detail)
|
WakuEvent.Health,
|
||||||
|
(e: CustomEvent<HealthStatus>) => resolve(e.detail)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,11 @@
|
|||||||
import type { IdentifyResult, PeerId } from "@libp2p/interface";
|
import type { IdentifyResult, PeerId } from "@libp2p/interface";
|
||||||
import { FilterCodecs, LightPushCodec } from "@waku/core";
|
import { FilterCodecs, LightPushCodec } from "@waku/core";
|
||||||
import { HealthStatus, IWakuEventEmitter, Libp2p } from "@waku/interfaces";
|
import {
|
||||||
|
HealthStatus,
|
||||||
|
IWakuEventEmitter,
|
||||||
|
Libp2p,
|
||||||
|
WakuEvent
|
||||||
|
} from "@waku/interfaces";
|
||||||
import { Logger } from "@waku/utils";
|
import { Logger } from "@waku/utils";
|
||||||
|
|
||||||
type PeerEvent<T> = (_event: CustomEvent<T>) => void;
|
type PeerEvent<T> = (_event: CustomEvent<T>) => void;
|
||||||
@ -130,7 +135,7 @@ export class HealthIndicator implements IHealthIndicator {
|
|||||||
if (this.value !== newValue) {
|
if (this.value !== newValue) {
|
||||||
this.value = newValue;
|
this.value = newValue;
|
||||||
this.events.dispatchEvent(
|
this.events.dispatchEvent(
|
||||||
new CustomEvent<HealthStatus>("waku:health", {
|
new CustomEvent<HealthStatus>(WakuEvent.Health, {
|
||||||
detail: this.value
|
detail: this.value
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
@ -43,10 +43,7 @@ describe("PeerManager", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const skipIfNoPeers = (result: PeerId[] | null): boolean => {
|
const skipIfNoPeers = (result: PeerId[] | null): boolean => {
|
||||||
if (!result || result.length === 0) {
|
return !result || result.length === 0;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -151,20 +148,27 @@ describe("PeerManager", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should dispatch connect and disconnect events", () => {
|
it("should dispatch connect and disconnect events", () => {
|
||||||
const connectSpy = sinon.spy();
|
const filterConnectSpy = sinon.spy();
|
||||||
const disconnectSpy = sinon.spy();
|
const storeConnectSpy = sinon.spy();
|
||||||
|
const filterDisconnectSpy = sinon.spy();
|
||||||
peerManager.events.addEventListener(
|
peerManager.events.addEventListener(
|
||||||
PeerManagerEventNames.Connect,
|
PeerManagerEventNames.FilterConnect,
|
||||||
connectSpy
|
filterConnectSpy
|
||||||
);
|
);
|
||||||
peerManager.events.addEventListener(
|
peerManager.events.addEventListener(
|
||||||
PeerManagerEventNames.Disconnect,
|
PeerManagerEventNames.StoreConnect,
|
||||||
disconnectSpy
|
storeConnectSpy
|
||||||
|
);
|
||||||
|
peerManager.events.addEventListener(
|
||||||
|
PeerManagerEventNames.FilterDisconnect,
|
||||||
|
filterDisconnectSpy
|
||||||
);
|
);
|
||||||
peerManager["dispatchFilterPeerConnect"](peers[0].id);
|
peerManager["dispatchFilterPeerConnect"](peers[0].id);
|
||||||
|
peerManager["dispatchStorePeerConnect"](peers[0].id);
|
||||||
peerManager["dispatchFilterPeerDisconnect"](peers[0].id);
|
peerManager["dispatchFilterPeerDisconnect"](peers[0].id);
|
||||||
expect(connectSpy.calledOnce).to.be.true;
|
expect(filterConnectSpy.calledOnce).to.be.true;
|
||||||
expect(disconnectSpy.calledOnce).to.be.true;
|
expect(storeConnectSpy.calledOnce).to.be.true;
|
||||||
|
expect(filterDisconnectSpy.calledOnce).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle onConnected and onDisconnected", async () => {
|
it("should handle onConnected and onDisconnected", async () => {
|
||||||
|
|||||||
@ -34,20 +34,26 @@ type GetPeersParams = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export enum PeerManagerEventNames {
|
export enum PeerManagerEventNames {
|
||||||
Connect = "filter:connect",
|
FilterConnect = "filter:connect",
|
||||||
Disconnect = "filter:disconnect"
|
FilterDisconnect = "filter:disconnect",
|
||||||
|
StoreConnect = "store:connect"
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IPeerManagerEvents {
|
export interface IPeerManagerEvents {
|
||||||
/**
|
/**
|
||||||
* Notifies about Filter peer being connected.
|
* Notifies about Filter peer being connected.
|
||||||
*/
|
*/
|
||||||
[PeerManagerEventNames.Connect]: CustomEvent<PeerId>;
|
[PeerManagerEventNames.FilterConnect]: CustomEvent<PeerId>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notifies about Filter peer being disconnected.
|
* Notifies about Filter peer being disconnected.
|
||||||
*/
|
*/
|
||||||
[PeerManagerEventNames.Disconnect]: CustomEvent<PeerId>;
|
[PeerManagerEventNames.FilterDisconnect]: CustomEvent<PeerId>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notifies about a Store peer being connected.
|
||||||
|
*/
|
||||||
|
[PeerManagerEventNames.StoreConnect]: CustomEvent<PeerId>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,13 +204,14 @@ export class PeerManager {
|
|||||||
|
|
||||||
private async onConnected(event: CustomEvent<IdentifyResult>): Promise<void> {
|
private async onConnected(event: CustomEvent<IdentifyResult>): Promise<void> {
|
||||||
const result = event.detail;
|
const result = event.detail;
|
||||||
const isFilterPeer = result.protocols.includes(
|
if (
|
||||||
this.matchProtocolToCodec(Protocols.Filter)
|
result.protocols.includes(this.matchProtocolToCodec(Protocols.Filter))
|
||||||
);
|
) {
|
||||||
|
|
||||||
if (isFilterPeer) {
|
|
||||||
this.dispatchFilterPeerConnect(result.peerId);
|
this.dispatchFilterPeerConnect(result.peerId);
|
||||||
}
|
}
|
||||||
|
if (result.protocols.includes(this.matchProtocolToCodec(Protocols.Store))) {
|
||||||
|
this.dispatchStorePeerConnect(result.peerId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async onDisconnected(event: CustomEvent<PeerId>): Promise<void> {
|
private async onDisconnected(event: CustomEvent<PeerId>): Promise<void> {
|
||||||
@ -261,18 +268,24 @@ export class PeerManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const wasUnlocked = new Date(value).getTime();
|
const wasUnlocked = new Date(value).getTime();
|
||||||
return Date.now() - wasUnlocked >= 10_000 ? true : false;
|
return Date.now() - wasUnlocked >= 10_000;
|
||||||
}
|
}
|
||||||
|
|
||||||
private dispatchFilterPeerConnect(id: PeerId): void {
|
private dispatchFilterPeerConnect(id: PeerId): void {
|
||||||
this.events.dispatchEvent(
|
this.events.dispatchEvent(
|
||||||
new CustomEvent(PeerManagerEventNames.Connect, { detail: id })
|
new CustomEvent(PeerManagerEventNames.FilterConnect, { detail: id })
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private dispatchStorePeerConnect(id: PeerId): void {
|
||||||
|
this.events.dispatchEvent(
|
||||||
|
new CustomEvent(PeerManagerEventNames.StoreConnect, { detail: id })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private dispatchFilterPeerDisconnect(id: PeerId): void {
|
private dispatchFilterPeerDisconnect(id: PeerId): void {
|
||||||
this.events.dispatchEvent(
|
this.events.dispatchEvent(
|
||||||
new CustomEvent(PeerManagerEventNames.Disconnect, { detail: id })
|
new CustomEvent(PeerManagerEventNames.FilterDisconnect, { detail: id })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import type { PeerId } from "@libp2p/interface";
|
|||||||
import { TypedEventEmitter } from "@libp2p/interface";
|
import { TypedEventEmitter } from "@libp2p/interface";
|
||||||
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
|
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
|
||||||
import { Multiaddr } from "@multiformats/multiaddr";
|
import { Multiaddr } from "@multiformats/multiaddr";
|
||||||
import { LightNode, Protocols, Tags } from "@waku/interfaces";
|
import { LightNode, Protocols, Tags, WakuEvent } from "@waku/interfaces";
|
||||||
import { createRelayNode } from "@waku/relay";
|
import { createRelayNode } from "@waku/relay";
|
||||||
import { createLightNode } from "@waku/sdk";
|
import { createLightNode } from "@waku/sdk";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
@ -65,10 +65,13 @@ describe("Connection state", function () {
|
|||||||
it("should emit `waku:online` event only when first peer is connected", async function () {
|
it("should emit `waku:online` event only when first peer is connected", async function () {
|
||||||
let eventCount = 0;
|
let eventCount = 0;
|
||||||
const connectionStatus = new Promise<boolean>((resolve) => {
|
const connectionStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
eventCount++;
|
WakuEvent.Connection,
|
||||||
resolve(status);
|
({ detail: status }) => {
|
||||||
});
|
eventCount++;
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
await waku.dial(nwaku1PeerId, [Protocols.Filter]);
|
await waku.dial(nwaku1PeerId, [Protocols.Filter]);
|
||||||
@ -87,10 +90,13 @@ describe("Connection state", function () {
|
|||||||
|
|
||||||
let eventCount = 0;
|
let eventCount = 0;
|
||||||
const connectionStatus = new Promise<boolean>((resolve) => {
|
const connectionStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
eventCount++;
|
WakuEvent.Connection,
|
||||||
resolve(status);
|
({ detail: status }) => {
|
||||||
});
|
eventCount++;
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
await nwaku1.stop();
|
await nwaku1.stop();
|
||||||
@ -116,18 +122,24 @@ describe("Connection state", function () {
|
|||||||
|
|
||||||
let eventCount1 = 0;
|
let eventCount1 = 0;
|
||||||
const connectionStatus1 = new Promise<boolean>((resolve) => {
|
const connectionStatus1 = new Promise<boolean>((resolve) => {
|
||||||
waku1.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku1.events.addEventListener(
|
||||||
eventCount1++;
|
WakuEvent.Connection,
|
||||||
resolve(status);
|
({ detail: status }) => {
|
||||||
});
|
eventCount1++;
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
let eventCount2 = 0;
|
let eventCount2 = 0;
|
||||||
const connectionStatus2 = new Promise<boolean>((resolve) => {
|
const connectionStatus2 = new Promise<boolean>((resolve) => {
|
||||||
waku2.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku2.events.addEventListener(
|
||||||
eventCount2++;
|
WakuEvent.Connection,
|
||||||
resolve(status);
|
({ detail: status }) => {
|
||||||
});
|
eventCount2++;
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
await waku1.libp2p.peerStore.merge(waku2.peerId, {
|
await waku1.libp2p.peerStore.merge(waku2.peerId, {
|
||||||
@ -191,7 +203,7 @@ describe("Connection state", function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("waku:connection", function () {
|
describe(WakuEvent.Connection, function () {
|
||||||
let navigatorMock: any;
|
let navigatorMock: any;
|
||||||
let originalNavigator: any;
|
let originalNavigator: any;
|
||||||
|
|
||||||
@ -259,10 +271,13 @@ describe("waku:connection", function () {
|
|||||||
|
|
||||||
let eventCount = 0;
|
let eventCount = 0;
|
||||||
const connectedStatus = new Promise<boolean>((resolve) => {
|
const connectedStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
eventCount++;
|
WakuEvent.Connection,
|
||||||
resolve(status);
|
({ detail: status }) => {
|
||||||
});
|
eventCount++;
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
waku.libp2p.dispatchEvent(
|
waku.libp2p.dispatchEvent(
|
||||||
@ -279,9 +294,12 @@ describe("waku:connection", function () {
|
|||||||
expect(eventCount).to.be.eq(1);
|
expect(eventCount).to.be.eq(1);
|
||||||
|
|
||||||
const disconnectedStatus = new Promise<boolean>((resolve) => {
|
const disconnectedStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
resolve(status);
|
WakuEvent.Connection,
|
||||||
});
|
({ detail: status }) => {
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
waku.libp2p.dispatchEvent(
|
waku.libp2p.dispatchEvent(
|
||||||
@ -314,10 +332,13 @@ describe("waku:connection", function () {
|
|||||||
|
|
||||||
let eventCount = 0;
|
let eventCount = 0;
|
||||||
const connectedStatus = new Promise<boolean>((resolve) => {
|
const connectedStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
eventCount++;
|
WakuEvent.Connection,
|
||||||
resolve(status);
|
({ detail: status }) => {
|
||||||
});
|
eventCount++;
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
waku.libp2p.dispatchEvent(
|
waku.libp2p.dispatchEvent(
|
||||||
@ -331,9 +352,12 @@ describe("waku:connection", function () {
|
|||||||
expect(eventCount).to.be.eq(1);
|
expect(eventCount).to.be.eq(1);
|
||||||
|
|
||||||
const disconnectedStatus = new Promise<boolean>((resolve) => {
|
const disconnectedStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
resolve(status);
|
WakuEvent.Connection,
|
||||||
});
|
({ detail: status }) => {
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
navigatorMock.onLine = false;
|
navigatorMock.onLine = false;
|
||||||
@ -346,9 +370,12 @@ describe("waku:connection", function () {
|
|||||||
expect(eventCount).to.be.eq(2);
|
expect(eventCount).to.be.eq(2);
|
||||||
|
|
||||||
const connectionRecoveredStatus = new Promise<boolean>((resolve) => {
|
const connectionRecoveredStatus = new Promise<boolean>((resolve) => {
|
||||||
waku.events.addEventListener("waku:connection", ({ detail: status }) => {
|
waku.events.addEventListener(
|
||||||
resolve(status);
|
WakuEvent.Connection,
|
||||||
});
|
({ detail: status }) => {
|
||||||
|
resolve(status);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
navigatorMock.onLine = true;
|
navigatorMock.onLine = true;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user