-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: track node connection state #1719
Conversation
size-limit report 📦
|
a8d198f
to
4c0a743
Compare
})(); | ||
}, | ||
"peer:disconnect": () => { | ||
return (evt: CustomEvent<PeerId>): void => { | ||
"peer:disconnect": (evt: CustomEvent<PeerId>): void => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I tested locally, this event handler was not being called at all until I changed it to use the same pattern as the two above
4c0a743
to
1bebcbc
Compare
1bebcbc
to
2e34b07
Compare
2e34b07
to
b1e7a63
Compare
b1e7a63
to
73f4126
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!
private toggleOnline(): void { | ||
if (!this.online) { | ||
this.online = true; | ||
this.dispatchEvent( | ||
new CustomEvent<boolean>(EConnectionStateEvents.CONNECTION_STATUS, { | ||
detail: this.online | ||
}) | ||
); | ||
} | ||
} | ||
|
||
private toggleOffline(): void { | ||
if (this.online && this.libp2p.getConnections().length == 0) { | ||
this.online = false; | ||
this.dispatchEvent( | ||
new CustomEvent<boolean>(EConnectionStateEvents.CONNECTION_STATUS, { | ||
detail: this.online | ||
}) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe this particular dispatch event can be written as a private function for the class: dispatchConnectionStatus()
since it's being reused twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a usual concern with these type of functions. I like current one as it is explicitly mentions when it becomes online
and when offline
.
@adklempner can follow up if thinks it is necessary
Problem
There is currently no easy way to check if a waku node is connected to any other peers.
Solution
Adds a function
isConnected
to theWaku
interface which returns a boolean indicating whether or not the node has any active connections.The node initially starts offline. Once the first
peer:connect
event is detected by theConnectionManager
, it will now be considered offline. If theConnectionManager
detects apeer:disconnect
event, and there are no timers for either relay or ping in theKeepAliveManager
, then the node will be considered offline.Users can either call
isConnected
on the Waku node instance or add an event listener to the connection manager forwaku:online
orwaku:offline
eventsNotes