2023-08-16 20:18:13 +05:30
|
|
|
import type { Libp2p } from "@libp2p/interface";
|
2024-10-11 03:17:12 +05:30
|
|
|
import type { Peer, Stream } from "@libp2p/interface";
|
2023-11-28 15:57:18 +05:30
|
|
|
import type {
|
2024-03-11 18:50:34 +05:30
|
|
|
IBaseProtocolCore,
|
2023-11-28 15:57:18 +05:30
|
|
|
Libp2pComponents,
|
2024-01-09 23:34:30 -08:00
|
|
|
PubsubTopic
|
2023-11-28 15:57:18 +05:30
|
|
|
} from "@waku/interfaces";
|
2023-09-04 10:27:25 +05:30
|
|
|
|
2024-07-16 18:35:24 +02:00
|
|
|
import { StreamManager } from "./stream_manager/index.js";
|
2023-02-23 14:04:27 +11:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A class with predefined helpers, to be used as a base to implement Waku
|
|
|
|
|
* Protocols.
|
|
|
|
|
*/
|
2024-03-11 18:50:34 +05:30
|
|
|
export class BaseProtocol implements IBaseProtocolCore {
|
2023-07-25 02:17:52 +02:00
|
|
|
public readonly addLibp2pEventListener: Libp2p["addEventListener"];
|
|
|
|
|
public readonly removeLibp2pEventListener: Libp2p["removeEventListener"];
|
2023-09-04 10:27:25 +05:30
|
|
|
protected streamManager: StreamManager;
|
2023-07-25 02:17:52 +02:00
|
|
|
|
2024-07-19 15:58:17 +05:30
|
|
|
protected constructor(
|
2023-08-11 15:14:02 +02:00
|
|
|
public multicodec: string,
|
2024-10-11 03:17:12 +05:30
|
|
|
protected components: Libp2pComponents,
|
2024-08-13 05:23:20 +05:30
|
|
|
public readonly pubsubTopics: PubsubTopic[]
|
2023-08-11 15:14:02 +02:00
|
|
|
) {
|
2023-07-25 02:17:52 +02:00
|
|
|
this.addLibp2pEventListener = components.events.addEventListener.bind(
|
2023-08-16 20:18:13 +05:30
|
|
|
components.events
|
2023-07-25 02:17:52 +02:00
|
|
|
);
|
|
|
|
|
this.removeLibp2pEventListener = components.events.removeEventListener.bind(
|
2023-08-16 20:18:13 +05:30
|
|
|
components.events
|
2023-07-25 02:17:52 +02:00
|
|
|
);
|
2023-09-04 10:27:25 +05:30
|
|
|
|
|
|
|
|
this.streamManager = new StreamManager(
|
|
|
|
|
multicodec,
|
|
|
|
|
components.connectionManager.getConnections.bind(
|
|
|
|
|
components.connectionManager
|
|
|
|
|
),
|
|
|
|
|
this.addLibp2pEventListener
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-07-16 18:35:24 +02:00
|
|
|
|
2023-09-04 10:27:25 +05:30
|
|
|
protected async getStream(peer: Peer): Promise<Stream> {
|
|
|
|
|
return this.streamManager.getStream(peer);
|
2023-07-25 02:17:52 +02:00
|
|
|
}
|
2023-02-23 14:04:27 +11:00
|
|
|
}
|