forked from pull-stream/pull-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathready.js
More file actions
31 lines (25 loc) · 729 Bytes
/
ready.js
File metadata and controls
31 lines (25 loc) · 729 Bytes
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
module.exports = function(socket, callback) {
var remove = socket && (socket.removeEventListener || socket.removeListener);
function cleanup () {
if (typeof remove == 'function') {
remove.call(socket, 'open', handleOpen);
remove.call(socket, 'error', handleErr);
}
}
function handleOpen(evt) {
cleanup(); callback();
}
function handleErr (evt) {
cleanup(); callback(evt);
}
// if the socket is closing or closed, return end
if (socket.readyState >= 2) {
return callback(true);
}
// if open, trigger the callback
if (socket.readyState === 1) {
return callback();
}
socket.addEventListener('open', handleOpen);
socket.addEventListener('error', handleErr);
};