diff --git a/index.d.ts b/index.d.ts index 4820cc4..1fab20f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -54,6 +54,7 @@ export interface EventSourceOptions { debug?: boolean; pollingInterval?: number; lineEndingCharacter?: string; + autoConnect?: boolean; } type BuiltInEventMap = { @@ -77,6 +78,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 8b2991d..948b43e 100644 --- a/src/EventSource.js +++ b/src/EventSource.js @@ -30,6 +30,7 @@ class EventSource { this.debug = options.debug || false; this.interval = options.pollingInterval ?? 5000; this.lineEndingCharacter = options.lineEndingCharacter || null; + this.autoConnect = options.autoConnect ?? true; const defaultHeaders = { Accept: 'text/event-stream', @@ -56,7 +57,7 @@ class EventSource { this.url = url; } - this._pollAgain(this.timeoutBeforeConnection, true); + if(this.autoConnect) this._pollAgain(this.timeoutBeforeConnection, true); } _pollAgain(time, allowZero) { @@ -328,6 +329,9 @@ class EventSource { this._xhr.abort(); } } + get isConnected() { + return this.status === this.OPEN; + } } export default EventSource;