-
Notifications
You must be signed in to change notification settings - Fork 37
/
sse-client.js
156 lines (126 loc) · 3.47 KB
/
sse-client.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { EventSourcePolyfill } from 'event-source-polyfill';
export const formatText = (e) => e.data;
export const formatJSON = (e) => JSON.parse(e.data);
export default class SSEClient {
constructor(config) {
this._handlers = {};
this._listeners = {};
this._source = null;
if (config.format) {
if (typeof config.format === 'string') {
if (config.format === 'plain') {
this._format = formatText;
} else if (config.format === 'json') {
this._format = formatJSON;
} else {
this._format = formatText;
}
} else if (typeof config.format === 'function') {
this._format = config.format;
} else {
this._format = formatText;
}
} else {
this._format = formatText;
}
if (config.handlers) {
for (const event in config.handlers) {
this.on(event, config.handlers[event]);
}
}
this.url = config.url;
this.withCredentials = !!config.withCredentials;
this.polyfillOptions = config.polyfillOptions || {};
this.forcePolyfill = !!config.forcePolyfill;
}
get source() {
return this._source;
}
connect() {
if (this.forcePolyfill) {
this._source = new EventSourcePolyfill(
this.url,
Object.assign({}, this.polyfillOptions, {
withCredentials: this.withCredentials,
}),
);
} else {
this._source = new window.EventSource(this.url, {
withCredentials: this.withCredentials,
});
}
return new Promise((resolve, reject) => {
this._source.onopen = () => {
// Add event listeners that were added before we connected
for (let event in this._listeners) {
this._source.addEventListener(event, this._listeners[event]);
}
this._source.onerror = null;
resolve(this);
};
this._source.onerror = reject;
});
}
disconnect() {
if (this._source !== null) {
this._source.close();
this._source = null;
}
}
on(event, handler) {
if (!event) {
// Default "event-less" event
event = 'message';
}
if (!this._listeners[event]) {
this._create(event);
}
this._handlers[event].push(handler);
return this;
}
once(event, handler) {
this.on(event, (e) => {
this.off(event, handler);
handler(e);
});
return this;
}
off(event, handler) {
if (!this._handlers[event]) {
// no handlers registered for event
return this;
}
const idx = this._handlers[event].indexOf(handler);
if (idx === -1) {
// handler not registered for event
return this;
}
// remove handler from event
this._handlers[event].splice(idx, 1);
if (this._handlers[event].length === 0) {
// remove listener since no handlers exist
this._source.removeEventListener(event, this._listeners[event]);
delete this._handlers[event];
delete this._listeners[event];
}
return this;
}
_create(event) {
this._handlers[event] = [];
this._listeners[event] = (message) => {
let data;
try {
data = this._format(message);
} catch (err) {
if (typeof this._source.onerror === 'function') {
this._source.onerror(err);
}
return;
}
this._handlers[event].forEach((handler) => handler(data, message.lastEventId));
};
if (this._source) {
this._source.addEventListener(event, this._listeners[event]);
}
}
}