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

Add done event #63

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

daniel-bryant
Copy link

See my original issue #56 for some background.

Currently the library only supports close events, which fire when the connection is closed from the client side. An additional event for tracking server side closing of the connection would be useful. This PR adds a done event to do that. Luckily most of the plumbing is already there, so it was easy to hook up the new event. As far as naming goes, I just matched the state name like most other events seem to do. It still might be a little confusing, so I added some text to the README.

README.md Outdated
### Done vs Close

`done` events will fire when server closes the connection.
Reconnections will occur indefinitely, unless this behavior is disabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be rephrased slightly to make it more clear. Maybe something like:

By default, the client will automatically reconnect when this happens. You can disable the reconnections by setting the pollingInterval option to 0".

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good to me. Updated.

@@ -115,6 +116,7 @@ class EventSource {
this._handleEvent(xhr.responseText || '');

if (xhr.readyState === XMLHttpRequest.DONE) {
this.dispatch('done', { type: 'done' });
Copy link
Contributor

Choose a reason for hiding this comment

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

I would strongly recommend to dispatch the done event after calling _pollAgain. This would allow users to cancel the reconnection by calling .close() from the done handler.

Copy link
Author

Choose a reason for hiding this comment

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

Good call. I just updated and tested that it works as expected.

This allows 'close' called from a 'done' handler to cancel the upcoming
reconnection. Otherwise '_pollTimer' will get reset after the handler.
@EmilJunker
Copy link
Contributor

LGTM 👍

@rafeautie
Copy link

any idea when this will merge? :)

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.

4 participants