From c294aae4bb7562db50ff7ee5e8e71a079f08c5ae Mon Sep 17 00:00:00 2001 From: Lukas Reining Date: Wed, 3 Apr 2024 22:40:06 +0200 Subject: [PATCH] fix: stop reconnecting if the eventsource is closed Signed-off-by: Lukas Reining --- .eslintrc.js => .eslintrc.cjs | 0 example/package-lock.json | 21 ++++----------------- example/package.json | 2 +- jest.config.js => jest.config.cjs | 0 package-lock.json | 4 ++-- package.json | 2 +- src/eventsource.ts | 11 ++++++++++- 7 files changed, 18 insertions(+), 22 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) rename jest.config.js => jest.config.cjs (100%) diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/example/package-lock.json b/example/package-lock.json index d6bc534..3a3170c 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "express": "^4.18.2", - "extended-eventsource": "^1.3.2", + "extended-eventsource": "^1.4.9", "serve-static": "^1.15.0", "ssestream": "^1.1.0" }, @@ -280,16 +280,6 @@ "version": "1.0.6", "license": "MIT" }, - "node_modules/core-js": { - "version": "3.33.2", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.33.2.tgz", - "integrity": "sha512-XeBzWI6QL3nJQiHmdzbAOiMYqjrb7hwU7A39Qhvd/POSa/t9E1AeZyEZx3fNvp/vtM8zXwhoL0FsiS0hD0pruQ==", - "hasInstallScript": true, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/core-js" - } - }, "node_modules/create-require": { "version": "1.1.1", "dev": true, @@ -400,12 +390,9 @@ } }, "node_modules/extended-eventsource": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/extended-eventsource/-/extended-eventsource-1.3.2.tgz", - "integrity": "sha512-aztqQg5ykVAWCxGvI7TzkUMhm0BfTUOEXRn0Y/KxioFKO5be0+JXXAxLYurYBBRRi+TpHbg+7zgkqdnSPuccsw==", - "dependencies": { - "core-js": "^3.33.2" - } + "version": "1.4.9", + "resolved": "https://registry.npmjs.org/extended-eventsource/-/extended-eventsource-1.4.9.tgz", + "integrity": "sha512-x0SMAw57jEaiBKLQWTatVBZkT/PRuL5hdiEP6s8GKqeQOapBRwnohI6e8zGM0XStolQrQe+L6Wvin8G0UthRbw==" }, "node_modules/finalhandler": { "version": "1.2.0", diff --git a/example/package.json b/example/package.json index f80ea8a..89ac938 100644 --- a/example/package.json +++ b/example/package.json @@ -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" } diff --git a/jest.config.js b/jest.config.cjs similarity index 100% rename from jest.config.js rename to jest.config.cjs diff --git a/package-lock.json b/package-lock.json index 2acb2ee..a572c33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "extended-eventsource", - "version": "1.4.7", + "version": "1.4.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "extended-eventsource", - "version": "1.4.7", + "version": "1.4.8", "license": "MIT", "devDependencies": { "@babel/cli": "^7.23.0", diff --git a/package.json b/package.json index 02838ec..094b78a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/eventsource.ts b/src/eventsource.ts index cdb0c7a..f8086e9 100644 --- a/src/eventsource.ts +++ b/src/eventsource.ts @@ -34,6 +34,7 @@ export class CustomEventSource extends EventTarget implements EventSource { public readonly options: EventSourceInit & EventSourceOptions; private abortController?: AbortController; + private timeoutId: ReturnType | undefined = undefined; private retry: number; private currentLastEventId?: string; @@ -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(); @@ -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); } @@ -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(); }