-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocketHandlerPool.js
56 lines (50 loc) · 2.15 KB
/
websocketHandlerPool.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
//refactor this module to be like ./apiHandlerPool and export the pool itself, move config to ./config.
/**@type {import('./pool')<WebsocketAPIInterface>}*/
const websocketHandlersPool = new (require("./pool"))();
const { waitATick } = require('./commonUtils');
const Matcher = require("./matcher");
class WebsocketAPIInterface extends Matcher {
/**
* @param {string|RegExp} matcher
* @param {WebSocketConnectionRegisterCallback} registerCallback
*/
constructor(matcher, registerCallback) {
super(matcher);
if (typeof registerCallback !== "function" || registerCallback.length === 0) {
throw "Connection registering callback must be a function with at least one valid parameter.";
}
this.registerConnection = registerCallback;
}
}
/**
* @param {import('./matcher').MatcherTypes} matcher
* @param {WebSocketConnectionRegisterCallback} registerCallback
*/
const addHandler = (matcher, registerCallback) => {
const newWSApiInterface = new WebsocketAPIInterface(matcher, registerCallback);
return websocketHandlersPool.add(newWSApiInterface);
};
/**@param {Symbol} symbol */
const removeHandler = symbol => {
websocketHandlersPool.remove(symbol);
};
module.exports.interface = {
findFirstEligibleHandler: path => websocketHandlersPool.find(ws => waitATick().then(() => ws.testOn(path))),
add: addHandler,
remove: removeHandler
};
let useNagleAlgorithm = false;
module.exports.config = {
get shouldServe() { return websocketHandlersPool.pool.size > 0; },
get useNagle() { return useNagleAlgorithm; },
set useNagle(value) { useNagleAlgorithm = !!value; }
};
/**
* @callback WebSocketConnectionRegisterCallback
* @param {import('websocket').connection} wsConnection - The accepted WebSocket connection. Consumer of this api MUST set their own `close` and `message` event handlers.
* @returns {void}
*/
/**
* @typedef {Matcher} WebsocketAPIInterface
* @prop {WebSocketConnectionRegisterCallback} registerConnection - This function will be used when the request is accepted. One may use this to add a connection to their Set.
*/