Skip to content

Commit de90c9d

Browse files
committed
Fix expo issues, fix addEventListener and removeEventListener when event type is not registered.
1 parent cd41d06 commit de90c9d

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type EventSourceListener<E extends string = never> = (
5757
) => void;
5858

5959
declare class EventSource<E extends string = never> {
60-
constructor(url: URL | string, options?: EventSourceOptions);
60+
constructor(url: URL | string, options?: EventSourceOptions = {});
6161
open(): void;
6262
close(): void;
6363
addEventListener(type: E | EventType, listener: EventSourceListener<E>): void;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-sse",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "EventSource implementation for React Native. Server-Sent Events (SSE) for iOS and Android.",
55
"main": "index.js",
66
"scripts": {

src/EventSource.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class EventSource {
44
OPEN = 1;
55
CLOSED = 2;
66

7-
constructor(url, options) {
7+
constructor(url, options = {}) {
88
this.interval = options.pollingInterval || 5000;
99
this.lastEventId = null;
1010
this.lastIndexProcessed = 0;
@@ -209,11 +209,17 @@ class EventSource {
209209
}
210210

211211
addEventListener(type, listener) {
212+
if (this.eventHandlers[type] === undefined) {
213+
this.eventHandlers[type] = [];
214+
}
215+
212216
this.eventHandlers[type].push(listener);
213217
}
214218

215219
removeEventListener(type, listener) {
216-
this.eventHandlers[type] = this.eventHandlers[type].filter((handler) => handler !== listener);
220+
if (this.eventHandlers[type] !== undefined) {
221+
this.eventHandlers[type] = this.eventHandlers[type].filter((handler) => handler !== listener);
222+
}
217223
}
218224

219225
removeAllEventListeners(type) {

0 commit comments

Comments
 (0)