-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpoint.js
234 lines (194 loc) · 3.25 KB
/
Endpoint.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
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
import Ws from 'isomorphic-ws'
import Events from './_/Events.js'
import Websocket from './transport/Websocket.js'
export default function Endpoint (transport, { ws, dispatch, events } = {})
{
var $buffer = null
if (! dispatch)
{
events = Events()
$buffer = []
}
var $ws
if (dispatch)
{
$ws = ws
ws = null
}
var endp =
{
on,
send,
close,
aux: {},
}
function on (...args)
{
if (dispatch)
{
return () => {}
}
else if (events)
{
return events.on(...args)
}
else
{
return () => {}
}
}
function send (kind, data = '') /* eslint-disable-line complexity */
{
if ($buffer)
{
$buffer.push([ kind, (data || '') ])
}
else if ($ws)
{
if (typeof kind === 'string')
{
$ws.send(`@${ kind }:${ String(data) }`)
}
else
{
send_binary(kind)
}
}
}
async 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 handle (event)
{
var msg = event.data
if (typeof msg !== 'string') return handle_binary(msg)
if (msg.charAt(0) !== '@') return
if (msg.charAt(1) === '@') return
var colon = msg.indexOf(':')
if (colon === -1) return
var kind = msg.slice(1, colon)
var data = msg.slice(colon + 1)
events.emit(kind, data, endp)
}
function handle_binary (data)
{
events.emit('@binary', data, endp)
}
function connect () /* eslint-disable-line complexity */
{
if (! dispatch)
{
if (typeof transport === 'function')
{
$ws = transport()
}
else if (typeof transport === 'string')
{
$ws = Websocket(transport)
}
else
{
throw new TypeError('unknown_transport')
}
}
/* before user */
if (! dispatch)
{
ev('open', flush)
}
ev('open', () => events.emit('@open', void 0, endp))
ev('close', () => events.emit('@close', void 0, endp))
ev('error', (e) => events.emit('@error', e, endp))
ev('message', handle)
/* after user */
if (! dispatch)
{
ev('open', connect_or_reconnect)
ev('close', reconnect_or_cleanup)
}
else
{
ev('close', cleanup)
}
function ev (name, handler)
{
$ws.addEventListener(name, handler)
}
/* instantly opened when in dispatch */
if (dispatch)
{
events.emit('@open', void 0, endp)
events.emit('@connect', void 0, endp)
dispatch.rooms.join_if_any('@all', endp)
}
}
function flush ()
{
if (! $buffer) return
var to_flush = $buffer
$buffer = null
for (var pair of to_flush)
{
send(...pair)
}
}
function reconnect_or_cleanup ()
{
if (! $ws)
{
cleanup()
}
else
{
$buffer = []
$ws = null
setTimeout(connect, 1e3)
}
}
function connect_or_reconnect ()
{
if (! connect_or_reconnect.yes)
{
connect_or_reconnect.yes = true
events.emit('@connect', void 0, endp)
}
else
{
events.emit('@reconnect', void 0, endp)
}
}
function close ()
{
if ($ws)
{
$ws.close()
$ws = null
}
}
function cleanup ()
{
if (! endp) return
if (dispatch)
{
dispatch.rooms.leave_every(endp)
}
$buffer = null
$ws = null
dispatch = null
events = null
endp = null
}
return (connect(), endp)
}