Skip to content

Add done event #63

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

Merged
merged 4 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type BuiltInEventType = 'open' | 'message' | 'error' | 'close';
export type BuiltInEventType = 'open' | 'message' | 'error' | 'done' | 'close';
export type EventType<E extends string = never> = E | BuiltInEventType;

export interface MessageEvent {
Expand All @@ -12,6 +12,10 @@ export interface OpenEvent {
type: 'open';
}

export interface DoneEvent {
type: 'done';
}

export interface CloseEvent {
type: 'close';
}
Expand Down Expand Up @@ -55,6 +59,7 @@ export interface EventSourceOptions {
type BuiltInEventMap = {
'message': MessageEvent,
'open': OpenEvent,
'done': DoneEvent,
'close': CloseEvent,
'error': ErrorEvent | TimeoutEvent | ExceptionEvent,
};
Expand Down
2 changes: 2 additions & 0 deletions src/EventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class EventSource {
open: [],
message: [],
error: [],
done: [],
close: [],
};

Expand Down Expand Up @@ -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
Contributor 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._logDebug('[EventSource][onreadystatechange][DONE] Operation done.');
this._pollAgain(this.interval, false);
}
Expand Down