forked from jordanbyron/react-native-event-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (31 loc) · 836 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import EventSource from './EventSource'
class RNEventSource {
constructor(url, options={}) {
this.url = url;
this.options = options;
this.eventSource = new EventSource(url, options);
this.listeners = [];
}
addEventListener (type, listener) {
this.eventSource.addEventListener(type, listener)
const remove = () => {
this.removeListener(type, listener);
}
this.listeners.push({
remove: remove, type: type, listener: listener
});
return this.listeners[this.listeners.length - 1];
}
removeAllListeners () {
this.listeners.map((listener) => {
listener.remove();
});
}
removeListener(type, listener) {
this.eventSource.removeEventListener(type, listener);
}
close () {
this.eventSource.close();
}
}
export default RNEventSource;