-
Notifications
You must be signed in to change notification settings - Fork 33
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
Emit connectionstatechange events #299
Conversation
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.
A couple minor things (no need to rerequest review) otherwise lgtm!
@@ -376,6 +381,7 @@ export class RobotClient extends EventDispatcher implements Robot { | |||
} | |||
|
|||
public async disconnect() { | |||
this.emit(MachineConnectionEvent.DISCONNECTING, {}); |
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.
(q) is this necessary, given that we've set this.on(eventType ...)
above?
Same with other this.emit
calls below.
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.
Similar (q): why not just emit the connectionstatechange
here, rather than emitting this event, then catching it and re-emitting?
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 actually emitting the source event, signaling disconnect is starting. The this.on(eventType, ...)
above re-emits the event with the name connectionstatechange
. What this means is the consumer can choose which event they want to listen to - if they care about all connection events, they can set up a robotClient.on('connectionstatechange', ...)
event handler. If they only want to listen to disconnected events, they can just set up a robotClient.on('disconnected', ...)
handler.
We don't want to just emit connectionstatechange
here because we would need to emit the connectionstatechange
anywhere a disconnecting, disconnect, connecting, or connected event is emitted. By catching and re-emitting, we don't need to remember to also emit connectionstatechange
everywhere.
@@ -376,6 +381,7 @@ export class RobotClient extends EventDispatcher implements Robot { | |||
} | |||
|
|||
public async disconnect() { | |||
this.emit(MachineConnectionEvent.DISCONNECTING, {}); |
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 actually emitting the source event, signaling disconnect is starting. The this.on(eventType, ...)
above re-emits the event with the name connectionstatechange
. What this means is the consumer can choose which event they want to listen to - if they care about all connection events, they can set up a robotClient.on('connectionstatechange', ...)
event handler. If they only want to listen to disconnected events, they can just set up a robotClient.on('disconnected', ...)
handler.
We don't want to just emit connectionstatechange
here because we would need to emit the connectionstatechange
anywhere a disconnecting, disconnect, connecting, or connected event is emitted. By catching and re-emitting, we don't need to remember to also emit connectionstatechange
everywhere.
This adds
connectionstatechange
events to theRobotClient
such that a consumer only has to listen to one thing to get all of the connection events.Example:
To best see this in action, check out the follow-up PR to add a new vanilla example for using the robot client. I used this for testing during local development. (The existing vanilla example uses the Viam client.)
Change log
MachineConnectionEvent
s:connecting | connected | disconnecting | disconnected
reconnected
. AFAICT it was just used internally, but please let me know if that assumption is false. Because of how reconnect logic is structured usingconnect()
, it would be messy to emit a different event duringreconnecting
andreconnected
.connect()
- emitconnecting
at start andconnected
if successful.disconnected
is still emitted when there is a failure.disconnect()
- emitdisconnecting
at start anddisconnected
if successful.disconnecting -> disconnect
isn't humanly perceivable under the conditions I was testing in, but I think it's good practice to emit to the consumerconnectionstatechange
event onRobotClient
s