diff options
author | Diego RodrÃguez Baquero <diegorbaquero@gmail.com> | 2021-05-08 20:10:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-08 20:10:17 +0300 |
commit | 4f18203a958ad80f07708a6543aa62232f0670bc (patch) | |
tree | 7527acdafdf997e90669444cf9a35a24cc735c98 /lib | |
parent | bce5ae19a7cb5f34298f57c342107125cc9d4a3f (diff) | |
parent | ee833d0bea0bed55721e7da78e90afda972796b7 (diff) |
Merge pull request #2018 from jimmywarting/set
Made 3 arrays into a set
Diffstat (limited to 'lib')
-rw-r--r-- | lib/conn-pool.js | 7 | ||||
-rw-r--r-- | lib/server.js | 19 |
2 files changed, 12 insertions, 14 deletions
diff --git a/lib/conn-pool.js b/lib/conn-pool.js index 80aa87f..1ed49d9 100644 --- a/lib/conn-pool.js +++ b/lib/conn-pool.js @@ -1,4 +1,3 @@ -const arrayRemove = require('unordered-array-remove') const debug = require('debug')('webtorrent:conn-pool') const net = require('net') // browser exclude const utp = require('utp-native') // browser exclude @@ -23,7 +22,7 @@ class ConnPool { // Temporarily store incoming connections so they can be destroyed if the server is // closed before the connection is passed off to a Torrent. - this._pendingConns = [] + this._pendingConns = new Set() this._onTCPConnectionBound = (conn) => { this._onConnection(conn, 'tcp') @@ -129,7 +128,7 @@ class ConnPool { return } - self._pendingConns.push(conn) + self._pendingConns.add(conn) conn.once('close', cleanupPending) const peer = type === 'utp' ? Peer.createUTPIncomingPeer(conn) : Peer.createTCPIncomingPeer(conn) @@ -157,7 +156,7 @@ class ConnPool { conn.removeListener('close', cleanupPending) wire.removeListener('handshake', onHandshake) if (self._pendingConns) { - arrayRemove(self._pendingConns, self._pendingConns.indexOf(conn)) + self._pendingConns.delete(conn) } } } diff --git a/lib/server.js b/lib/server.js index 85b4573..755f700 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,3 @@ -const arrayRemove = require('unordered-array-remove') const escapeHtml = require('escape-html') const http = require('http') const mime = require('mime') @@ -10,8 +9,8 @@ function Server (torrent, opts = {}) { const server = http.createServer() if (!opts.origin) opts.origin = '*' // allow all origins by default - const sockets = [] - const pendingReady = [] + const sockets = new Set() + const pendingReady = new Set() let closed = false const _listen = server.listen const _close = server.close @@ -27,10 +26,10 @@ function Server (torrent, opts = {}) { closed = true server.removeListener('connection', onConnection) server.removeListener('request', onRequest) - while (pendingReady.length > 0) { - const onReady = pendingReady.pop() + pendingReady.forEach(onReady => { torrent.removeListener('ready', onReady) - } + }) + pendingReady.clear() _close.call(server, cb) } @@ -63,9 +62,9 @@ function Server (torrent, opts = {}) { function onConnection (socket) { socket.setTimeout(36000000) - sockets.push(socket) + sockets.add(socket) socket.once('close', () => { - arrayRemove(sockets, sockets.indexOf(socket)) + sockets.delete(socket) }) } @@ -106,7 +105,7 @@ function Server (torrent, opts = {}) { if (torrent.ready) { return handleRequest() } else { - pendingReady.push(onReady) + pendingReady.add(onReady) torrent.once('ready', onReady) return } @@ -129,7 +128,7 @@ function Server (torrent, opts = {}) { } function onReady () { - arrayRemove(pendingReady, pendingReady.indexOf(onReady)) + pendingReady.delete(onReady) handleRequest() } |