Skip to content
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

Fix for Appstate changes causing disconnect #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ const options: EventSourceOptions = {
debug: false, // Show console.debug messages for debugging purpose. Default: false
pollingInterval: 5000, // Time (ms) between reconnections. If set to 0, reconnections will be disabled. Default: 5000
lineEndingCharacter: null // Character(s) used to represent line endings in received data. Common values: '\n' for LF (Unix/Linux), '\r\n' for CRLF (Windows), '\r' for CR (older Mac). Default: null (Automatically detect from event)
reconnectOnActive: true, // Automatically reconnect when the app becomes active (e.g., after being in the background) Default: False
}
```

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface EventSourceOptions {
debug?: boolean;
pollingInterval?: number;
lineEndingCharacter?: string;
reconnectOnActive?: boolean;
}

type BuiltInEventMap = {
Expand Down
36 changes: 35 additions & 1 deletion src/EventSource.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AppState } from 'react-native';

const XMLReadyStateMap = [
'UNSENT',
'OPENED',
Expand Down Expand Up @@ -36,11 +38,16 @@ class EventSource {
this.debug = options.debug || false;
this.interval = options.pollingInterval ?? 5000;
this.lineEndingCharacter = options.lineEndingCharacter || null;
this.reconnectOnActive = options.reconnectOnActive || false;

this._xhr = null;
this._pollTimer = null;
this._lastIndexProcessed = 0;

this._appStateSubscription = null;
this._setupAppStateListener();


if (!url || (typeof url !== 'string' && typeof url.toString !== 'function')) {
throw new SyntaxError('[EventSource] Invalid URL argument.');
}
Expand All @@ -54,6 +61,28 @@ class EventSource {
this._pollAgain(this.timeoutBeforeConnection, true);
}

_setupAppStateListener() {
this._appStateSubscription = AppState.addEventListener(
'change',
this._handleAppStateChange
);
}

_handleAppStateChange = (nextAppState) => {
if (nextAppState === 'active') {
this._logDebug('[EventSource] App became active, reconnecting...');
this.close();
if (this.reconnectOnActive) {
this._pollAgain(0, true); // reconnect if the option is enabled
}
} else if (nextAppState === 'background' || nextAppState === 'inactive') {
this._logDebug(
'[EventSource] App went to background, closing connection...'
);
this.close();
}
};

_pollAgain(time, allowZero) {
if (time > 0 || allowZero) {
this._logDebug(`[EventSource] Will open new connection in ${time} ms.`);
Expand Down Expand Up @@ -313,7 +342,12 @@ class EventSource {
if (this._xhr) {
this._xhr.abort();
}

if (this._appStateSubscription) {
this._appStateSubscription.remove();
this._appStateSubscription = null;
}
}
}

export default EventSource;
export default EventSource;