Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/core/src/lib/filter/filter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { PeerId } from "@libp2p/interface";
import type { IncomingStreamData } from "@libp2p/interface-internal";
import type { PeerId, StreamHandler } from "@libp2p/interface";
import {
type ContentTopic,
type CoreProtocolResult,
Expand Down Expand Up @@ -52,7 +51,7 @@ export class FilterCore {

public async start(): Promise<void> {
try {
await this.libp2p.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
await this.libp2p.handle(FilterCodecs.PUSH, this.onRequest, {
Copy link

Copilot AI Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing this.onRequest directly will lose the this context when the method is called. Since onRequest is now an arrow function property, this change is correct, but the arrow function should be defined before this line to ensure proper initialization order.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What it says. I doubt you can remove the bind here without breaking things. this.onRequest uses this.handleIncomingMessage which I believe will not be available if not binded

maxInboundStreams: 100
});
} catch (e) {
Expand Down Expand Up @@ -304,7 +303,7 @@ export class FilterCore {
};
}

private onRequest(streamData: IncomingStreamData): void {
private onRequest: StreamHandler = (streamData) => {
const { connection, stream } = streamData;
const { remotePeer } = connection;
log.info(`Received message from ${remotePeer.toString()}`);
Expand Down Expand Up @@ -345,5 +344,5 @@ export class FilterCore {
} catch (e) {
log.error("Error decoding message", e);
}
}
};
}
11 changes: 5 additions & 6 deletions packages/core/src/lib/stream_manager/stream_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("StreamManager", () => {
createMockStream({ id: "1", protocol: MULTICODEC, writeStatus })
];

streamManager["libp2p"]["connectionManager"]["getConnections"] = (
(streamManager["libp2p"]["connectionManager"] as any).getConnections = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid the as any in test? it just makes refactoring harder as your bypass the type system. Just leave it as it was.

_peerId: PeerId | undefined
) => [con1];

Expand All @@ -46,7 +46,7 @@ describe("StreamManager", () => {
});

it("should return undefined if no connection provided", async () => {
streamManager["libp2p"]["connectionManager"]["getConnections"] = (
(streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined
) => [];

Expand All @@ -70,7 +70,7 @@ describe("StreamManager", () => {
);

con1.newStream = newStreamSpy;
streamManager["libp2p"]["connectionManager"]["getConnections"] = (
(streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined
) => [con1];

Expand All @@ -97,7 +97,7 @@ describe("StreamManager", () => {
);

con1.newStream = newStreamSpy;
streamManager["libp2p"]["connectionManager"]["getConnections"] = (
(streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_peerId: PeerId | undefined
) => [con1];

Expand Down Expand Up @@ -148,7 +148,7 @@ describe("StreamManager", () => {
writeStatus: "writable"
})
];
streamManager["libp2p"]["connectionManager"]["getConnections"] = (
(streamManager["libp2p"]["connectionManager"] as any).getConnections = (
_id: PeerId | undefined
) => [con1];

Expand Down Expand Up @@ -178,7 +178,6 @@ function createMockConnection(options: MockConnectionOptions = {}): Connection {
}
} as Connection;
}

type MockStreamOptions = {
id?: string;
protocol?: string;
Expand Down
24 changes: 13 additions & 11 deletions packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,35 @@ export type CreateEncoderParams = CreateDecoderParams & {
ephemeral?: boolean;
};

export enum WakuEvent {
Connection = "waku:connection",
Health = "waku:health"
}
export const WakuEvent = {
Connection: "waku:connection",
Health: "waku:health"
} as const;
Comment on lines +28 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you wanted to remove the enum? In any case, add a comment for next implementer to say to leave it as is to expose string for event types to developer.


export type WakuEvent = (typeof WakuEvent)[keyof typeof WakuEvent];

export interface IWakuEvents {
/**
* Emitted when a connection is established or lost.
*
* @example
* ```typescript
* waku.addEventListener(WakuEvent.Connection, (event) => {
* waku.events.addEventListener("waku:connection", (event) => {
* console.log(event.detail); // true if connected, false if disconnected
* });
*/
[WakuEvent.Connection]: CustomEvent<boolean>;
"waku:connection": CustomEvent<boolean>;

/**
* Emitted when the health status changes.
*
* @example
* ```typescript
* waku.addEventListener(WakuEvent.Health, (event) => {
* waku.events.addEventListener("waku:health", (event) => {
* console.log(event.detail); // 'Unhealthy', 'MinimallyHealthy', or 'SufficientlyHealthy'
* });
*/
[WakuEvent.Health]: CustomEvent<HealthStatus>;
"waku:health": CustomEvent<HealthStatus>;
}

export type IWakuEventEmitter = TypedEventEmitter<IWakuEvents>;
Expand All @@ -66,12 +68,12 @@ export interface IWaku {
/**
* Emits events related to the Waku node.
* Those are:
* - WakuEvent.Connection
* - WakuEvent.Health
* - "waku:connection"
* - "waku:health"
*
* @example
* ```typescript
* waku.events.addEventListener(WakuEvent.Connection, (event) => {
* waku.events.addEventListener("waku:connection", (event) => {
* console.log(event.detail); // true if connected, false if disconnected
* });
* ```
Expand Down
Loading