Skip to content

Commit

Permalink
fix: stop reconnecting if the eventsource is closed
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Reining <[email protected]>
  • Loading branch information
lukas-reining committed Apr 3, 2024
1 parent f07ee9b commit c294aae
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 22 deletions.
File renamed without changes.
21 changes: 4 additions & 17 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"dependencies": {
"express": "^4.18.2",
"extended-eventsource": "^1.3.2",
"extended-eventsource": "^1.4.9",
"serve-static": "^1.15.0",
"ssestream": "^1.1.0"
}
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extended-eventsource",
"version": "1.4.8",
"version": "1.4.9",
"author": "Lukas Reining",
"readme": "README.md",
"description": "Spec compliant EventSource implementation for browsers and Node.JS",
Expand Down
11 changes: 10 additions & 1 deletion src/eventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class CustomEventSource extends EventTarget implements EventSource {

public readonly options: EventSourceInit & EventSourceOptions;
private abortController?: AbortController;
private timeoutId: ReturnType<typeof setTimeout> | undefined = undefined;
private retry: number;
private currentLastEventId?: string;

Expand Down Expand Up @@ -65,6 +66,13 @@ export class CustomEventSource extends EventTarget implements EventSource {
}

private async connect(lastEventId?: string) {
if (this.readyState === this.CLOSED) {
this.logger?.warn(
'Canceled reconnecting due to state already being closed',
);
return;
}

try {
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource
this.abortController = new AbortController();
Expand Down Expand Up @@ -164,7 +172,7 @@ export class CustomEventSource extends EventTarget implements EventSource {
this.logger?.warn(msg, error ?? '');
}

setTimeout(async () => {
this.timeoutId = setTimeout(async () => {
await this.connect(this.currentLastEventId);
}, this.retry);
}
Expand Down Expand Up @@ -211,6 +219,7 @@ export class CustomEventSource extends EventTarget implements EventSource {
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-close
public close() {
this.readyState = this.CLOSED;
clearTimeout(this.timeoutId);
this.abortController?.abort();
}

Expand Down

0 comments on commit c294aae

Please sign in to comment.