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

Track current Sarus state in state machine #404

Merged
merged 5 commits into from
Jan 29, 2024

Conversation

justuswilhelm
Copy link
Contributor

This pull request adds a new .state property to see whether Sarus is connecting right now, or if a connection has been closed or disconnected by the user. This way, we can easily keep track of failed connection attempts. This is useful for #403

Add a new class property `state` that stores the current state of a
Sarus object. This allows us to cleanly determine whether a Sarus object
has ever connected, whether a connection is failing, whether a user has
disconnected a Sarus object, and so on.
This doesn't do anything for now, it's always "created"
Sarus will try to connect automatically in the constructor, so it is not
necessary to distinguish between "created" and "connecting".
sarus.connect();
expect(sarus.state).toBe("connecting");
await server.connected;
// XXX for some reason the test will fail without waiting 10 ms here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a function in Hub's repo called delayUntil which will delay until a condition is met:

https://github.com/anephenix/hub/blob/master/helpers/delay.js

We could potentially import that function into the helpers/delay.ts file and use this code instead of delaying for 10 miliseconds:

delayUntil(() => sarus.state === "connected")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it would be better to not have to hardcode a duration here. Unfortunately I do not understand the websocket mock well enough. It was my understanding that awaiting server.connected should already cover the required waiting. I don't see how awaiting any more than that is changing the sarus.state (e.g. onopen being called).

Some leaky abstraction perhaps? If you have any ideas, please let me know.

@paulbjensen paulbjensen merged commit 4ec3c78 into anephenix:master Jan 29, 2024
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.

2 participants