Skip to content

Commit 4c446d5

Browse files
committed
shield window access
1 parent 53315dd commit 4c446d5

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

packages/core/src/lib/connection_manager.ts

+27-18
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class ConnectionManager
3939
private currentActiveParallelDialCount = 0;
4040
private pendingPeerDialQueue: Array<PeerId> = [];
4141

42-
private isConnectedToNetwork: boolean = window.navigator.onLine;
42+
private isConnectedToNetwork: boolean = false;
4343
private isConnectedToWakuNetwork: boolean = false;
4444

4545
public static create(
@@ -107,21 +107,26 @@ export class ConnectionManager
107107
}
108108

109109
public stop(): void {
110-
this.keepAliveManager.stopAll();
111-
this.libp2p.removeEventListener(
112-
"peer:connect",
113-
this.onEventHandlers["peer:connect"]
114-
);
115-
this.libp2p.removeEventListener(
116-
"peer:disconnect",
117-
this.onEventHandlers["peer:disconnect"]
118-
);
119-
this.libp2p.removeEventListener(
120-
"peer:discovery",
121-
this.onEventHandlers["peer:discovery"]
122-
);
123-
window.removeEventListener("online", this.onEventHandlers["online"]);
124-
window.removeEventListener("offline", this.onEventHandlers["offline"]);
110+
try {
111+
this.keepAliveManager.stopAll();
112+
this.libp2p.removeEventListener(
113+
"peer:connect",
114+
this.onEventHandlers["peer:connect"]
115+
);
116+
this.libp2p.removeEventListener(
117+
"peer:disconnect",
118+
this.onEventHandlers["peer:disconnect"]
119+
);
120+
this.libp2p.removeEventListener(
121+
"peer:discovery",
122+
this.onEventHandlers["peer:discovery"]
123+
);
124+
125+
window.removeEventListener("online", this.onEventHandlers["online"]);
126+
window.removeEventListener("offline", this.onEventHandlers["offline"]);
127+
} catch (err) {
128+
log.error(`Failed to stop: ${err}`);
129+
}
125130
}
126131

127132
public async dropConnection(peerId: PeerId): Promise<void> {
@@ -345,8 +350,12 @@ export class ConnectionManager
345350
}
346351

347352
private startBrowserNetworkStatusListener(): void {
348-
window.addEventListener("online", this.onEventHandlers["online"]);
349-
window.addEventListener("offline", this.onEventHandlers["offline"]);
353+
try {
354+
window.addEventListener("online", this.onEventHandlers["online"]);
355+
window.addEventListener("offline", this.onEventHandlers["offline"]);
356+
} catch (err) {
357+
log.error(`Failed to add event listener to window: ${err}`);
358+
}
350359
}
351360

352361
private onEventHandlers = {

0 commit comments

Comments
 (0)