From dad31261c299e2a89c28ce842eccb629ef921d44 Mon Sep 17 00:00:00 2001 From: "Laid Feggaa ( Rabi3 )" Date: Sat, 19 Jul 2025 15:18:42 +0400 Subject: [PATCH] feat: Add autoConnect option and isConnected getter to EventSource --- index.d.ts | 2 ++ package.json | 2 +- src/EventSource.js | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index eb77c42..5843922 100644 --- a/index.d.ts +++ b/index.d.ts @@ -50,6 +50,7 @@ export interface EventSourceOptions { debug?: boolean; pollingInterval?: number; lineEndingCharacter?: string; + autoConnect?: boolean; } type BuiltInEventMap = { @@ -72,6 +73,7 @@ declare class EventSource { removeEventListener>(type: T, listener: EventSourceListener): void; removeAllEventListeners>(type?: T): void; dispatch>(type: T, data: EventSourceEvent): void; + isConnected : boolean; } export default EventSource; diff --git a/package.json b/package.json index 69fbe9b..f8769a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-sse", - "version": "1.2.1", + "version": "1.2.2", "description": "EventSource implementation for React Native. Server-Sent Events (SSE) for iOS and Android.", "main": "index.js", "scripts": { diff --git a/src/EventSource.js b/src/EventSource.js index 6a7b146..7569ee8 100644 --- a/src/EventSource.js +++ b/src/EventSource.js @@ -36,6 +36,7 @@ class EventSource { this.debug = options.debug || false; this.interval = options.pollingInterval ?? 5000; this.lineEndingCharacter = options.lineEndingCharacter || null; + this.autoConnect = options.autoConnect ?? true; this._xhr = null; this._pollTimer = null; @@ -51,7 +52,7 @@ class EventSource { this.url = url; } - this._pollAgain(this.timeoutBeforeConnection, true); + if(this.autoConnect) this._pollAgain(this.timeoutBeforeConnection, true); } _pollAgain(time, allowZero) { @@ -314,6 +315,9 @@ class EventSource { this._xhr.abort(); } } + get isConnected() { + return this.status === this.OPEN; + } } export default EventSource;