-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpoint.js
More file actions
327 lines (258 loc) · 4.71 KB
/
Endpoint.js
File metadata and controls
327 lines (258 loc) · 4.71 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import Ws from 'isomorphic-ws'
/* import { version } from './package.json' with { type: 'json' } */
var version = '0.16'
import { CLOSED } from './status.js'
import Events from './_/Events.js'
import Websocket from './transport/Websocket.js'
/* * */
function noop () {}
var defaults =
{
should_reconnect: void 0,
reconnect_interval: 1e3,
should_cleanup: void 0,
emit_on_open_success: true,
}
export default function Endpoint (transport, options) /* eslint-disable-line max-statements, complexity */
{
var internal = (arguments[2] ?? {})
var { ws, dispatch, events } = internal
options = { ...defaults, ...options }
events ??= Events()
var $buffer = null
if (! dispatch)
{
$buffer = []
}
var $ws
if (dispatch)
{
$ws = ws
ws = null
}
var endp =
{
on,
send,
status,
open,
close,
aux: {},
}
function status ()
{
return $ws?.readyState ?? CLOSED
}
function on (...args)
{
if (dispatch)
{
return noop
}
else if (events)
{
return events.on(...args)
}
else
{
return noop
}
}
function send (key, data)
{
if ($buffer)
{
$buffer.push([ key, data ])
}
else if ($ws)
{
if (typeof key === 'string')
{
$ws.send(`@${ key }:${ String(data ?? '') }`)
}
else
{
send_binary(key)
}
}
}
function send_binary (msg)
{
if ($ws instanceof Ws)
{
$ws.send(msg)
return
}
if ($ws.capabilities?.binary)
{
$ws.send(msg)
return
}
/* TODO: impl binary emulation */
throw new TypeError('binary_not_supported')
}
function recv (event)
{
var msg = event.data
events.emit('@recv', msg, { endp })
if (typeof msg !== 'string') return recv_binary(msg)
if (msg.charAt(0) !== '@') return
if (msg.charAt(1) === '@') return
var colon = msg.indexOf(':')
if (colon === -1) return
var key = msg.slice(1, colon)
var data = msg.slice(colon + 1)
var meta = { key, msg, data, endp }
events.emit(key, data, meta)
}
function recv_binary (msg)
{
var key = '@binary'
var data = msg
var meta = { key, msg, data, endp }
events.emit(key, data, meta)
}
function open () /* eslint-disable-line max-statements, complexity */
{
if (! endp)
{
throw new ReferenceError('has_been_cleaned')
}
// TODO: TBD: pre-close (async involved)
// TBD: check status
if (! dispatch)
{
if (typeof transport === 'function')
{
$ws = transport()
}
else if (typeof transport === 'string')
{
$ws = Websocket(transport)
}
else
{
throw new TypeError('unknown_transport')
}
}
/* #1 */
if (! dispatch)
{
ev('open', on_open_endp)
}
/* #2 */
ev('close', () => events.emit('@close', void 0, { endp }))
ev('error', (e) => events.emit('@error', e, { endp }))
ev('message', recv)
/* #3 */
if (! dispatch)
{
ev('close', on_close_endp)
}
else
{
ev('close', cleanup)
}
function ev (name, handler)
{
$ws.addEventListener(name, handler)
}
if (dispatch)
{
/* instantly opened when under Dispatch, we're in a callback already */
events.emit('@open', void 0, { endp })
events.emit('@connect', void 0, { endp })
dispatch.rooms.join_if_any('@all', endp)
$ws.send(`@@booth:${ version }`)
}
}
function on_open_endp ()
{
events.emit('@open', void 0, { endp })
$ws.send(`@@booth:${ version }:endp`)
if (options.emit_on_open_success)
{
on_open_success()
}
}
function on_open_success ()
{
flush()
on_connect_or_reconnect()
}
if (internal.events && (! options.on_open_success))
{
events.on('@@open/success', on_open_success)
}
function flush ()
{
if (! $buffer) return
var to_flush = $buffer
$buffer = null
for (var pair of to_flush)
{
send(...pair)
}
}
var been_connected = false
function on_connect_or_reconnect ()
{
if (! been_connected)
{
been_connected = true
events.emit('@connect', void 0, { endp })
}
else
{
events.emit('@reconnect', void 0, { endp })
}
}
function on_close_endp ()
{
if (! $ws)
{
cleanup()
return
}
if (! should_reconnect())
{
cleanup()
return
}
$buffer = []
$ws = null
setTimeout(open, options.reconnect_interval)
}
function should_reconnect ()
{
if (options.should_reconnect !== void 0)
{
return options.should_reconnect
}
if ($ws instanceof Ws)
{
return true
}
return ($ws?.capabilities?.reconnect ?? true)
}
function close ()
{
if (! $ws) return
// TODO: TBD: await close (async involved)
// TBD: check status
$ws.close()
$ws = null
}
function cleanup ()
{
if (! endp) return
if (options.should_cleanup === false) return
dispatch?.rooms.leave_every(endp)
$buffer = null
$ws = null
dispatch = null
events = null
endp = null
}
return (open(), endp)
}