Skip to content
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

Merged
merged 2 commits into from
May 21, 2024
Merged

Conversation

ethanlook
Copy link
Contributor

@ethanlook ethanlook commented May 17, 2024

This adds connectionstatechange events to the RobotClient such that a consumer only has to listen to one thing to get all of the connection events.

Example:

let connectionState = 'disconnected';

// Need to mark 'connecting' and 'connected' by yourself on the initial connection since we can't listen to events until createRobotClient resolves.
connectionState = 'connecting';
const machine = VIAM.createRobotClient({ ... });
connectionState = 'connected';

machine.on('connectionstatechange', (event) => {
  connectionState = (event as { eventType: MachineConnectionState }).eventType;
});

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.)

  1. ➡️ Emit connectionstatechange events #299
  2. Update vanilla example to use Vite + TS app for robot client #300

Change log

  • Update the possible MachineConnectionEvents: connecting | connected | disconnecting | disconnected
    • I got rid of reconnected. AFAICT it was just used internally, but please let me know if that assumption is false. Because of how reconnect logic is structured using connect(), it would be messy to emit a different event during reconnecting and reconnected.
  • For connect() - emit connecting at start and connected if successful. disconnected is still emitted when there is a failure.
  • For disconnect() - emit disconnecting at start and disconnected 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 consumer
  • Expose connectionstatechange event on RobotClients

@ethanlook ethanlook marked this pull request as ready for review May 17, 2024 14:03
@ethanlook ethanlook requested a review from a team as a code owner May 17, 2024 14:03
@ethanlook ethanlook requested review from njooma and stuqdog May 17, 2024 14:03
Copy link
Member

@stuqdog stuqdog left a 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, {});
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

src/robot/client.ts Outdated Show resolved Hide resolved
@@ -376,6 +381,7 @@ export class RobotClient extends EventDispatcher implements Robot {
}

public async disconnect() {
this.emit(MachineConnectionEvent.DISCONNECTING, {});
Copy link
Contributor Author

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.

src/robot/client.ts Show resolved Hide resolved
src/robot/client.ts Outdated Show resolved Hide resolved
@ethanlook ethanlook merged commit 9f86b40 into main May 21, 2024
3 checks passed
@ethanlook ethanlook deleted the ethanlook/connectionstatechange branch May 21, 2024 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants