Skip to content

Commit ba8867c

Browse files
committed
add network listener
1 parent 069925b commit ba8867c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/core/src/lib/connection_manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class ConnectionManager implements IConnectionManager {
3434
private currentActiveParallelDialCount = 0;
3535
private pendingPeerDialQueue: Array<PeerId> = [];
3636

37-
private isConnectedToNetwork: boolean = navigator.onLine;
37+
private isConnectedToNetwork: boolean = window.navigator.onLine;
3838
private isConnectedToWakuNetwork: boolean = false;
3939

4040
private constructor(

packages/sdk/src/protocols/filter.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type ContentTopic,
66
CoreProtocolResult,
77
CreateSubscriptionResult,
8+
EConnectionStateEvents,
89
type IAsyncIterator,
910
type IDecodedMessage,
1011
type IDecoder,
@@ -48,6 +49,8 @@ export class SubscriptionManager implements ISubscriptionSDK {
4849
readonly peers: Peer[];
4950
readonly receivedMessagesHashStr: string[] = [];
5051

52+
private subscribeOptions: SubscribeOptions = DEFAULT_SUBSCRIBE_OPTIONS;
53+
5154
private keepAliveTimer: number | null = null;
5255

5356
private subscriptionCallbacks: Map<
@@ -70,6 +73,8 @@ export class SubscriptionManager implements ISubscriptionSDK {
7073
callback: Callback<T>,
7174
options: SubscribeOptions = DEFAULT_SUBSCRIBE_OPTIONS
7275
): Promise<SDKProtocolResult> {
76+
this.subscribeOptions = options;
77+
7378
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
7479

7580
// check that all decoders are configured for the same pubsub topic as this subscription
@@ -236,11 +241,40 @@ export class SubscriptionManager implements ISubscriptionSDK {
236241
}
237242

238243
private startNetworkMonitoring(): void {
239-
// this.protocol.addLibp2pEventListener("waku:connection", (evt) => console.log(evt));
244+
// @ts-expect-error: tmp change while PR in draft
245+
this.protocol.addLibp2pEventListener(
246+
EConnectionStateEvents.CONNECTION_STATUS,
247+
this.networkStateListener as () => void
248+
);
240249
}
241250

242251
private stopNetworkMonitoring(): void {
243-
// this.protocol.removeLibp2pEventListener("waku:connection", (evt) => console.log(evt));
252+
// @ts-expect-error: tmp change while PR in draft
253+
this.protocol.removeLibp2pEventListener(
254+
EConnectionStateEvents.CONNECTION_STATUS,
255+
this.networkStateListener as () => void
256+
);
257+
}
258+
259+
private async networkStateListener(isConnected: boolean): Promise<void> {
260+
if (!isConnected) {
261+
this.stopKeepAlivePings();
262+
return;
263+
}
264+
265+
const result = await this.ping();
266+
const renewPeerPromises = result.failures.map((v) => {
267+
if (v.peerId) {
268+
// @ts-expect-error: tmp change while PR in draft
269+
return this.protocol.renewPeer(v.peerId);
270+
}
271+
});
272+
273+
await Promise.all(renewPeerPromises);
274+
275+
this.startKeepAlivePings(
276+
this.subscribeOptions?.keepAlive || DEFAULT_SUBSCRIBE_OPTIONS.keepAlive
277+
);
244278
}
245279

246280
private startKeepAlivePings(interval: number): void {

0 commit comments

Comments
 (0)