diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-09-11 02:54:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-11 02:54:37 +0200 |
commit | ba8bbe6f1c11d4cf19ad0b916ba41cd919dbb042 (patch) | |
tree | c12b2df8d9e36b64f4653c4a38f928b748b755cf /ext/websocket | |
parent | bd4ca721eb2aa7b772a3834add2c37f606d4568b (diff) |
refactor: use Deno.core.tryClose (#11980)
Diffstat (limited to 'ext/websocket')
-rw-r--r-- | ext/websocket/01_websocket.js | 21 | ||||
-rw-r--r-- | ext/websocket/02_websocketstream.js | 25 |
2 files changed, 10 insertions, 36 deletions
diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js index df8063d21..79e4d923c 100644 --- a/ext/websocket/01_websocket.js +++ b/ext/websocket/01_websocket.js @@ -65,19 +65,6 @@ const CLOSING = 2; const CLOSED = 3; - /** - * Tries to close the resource (and ignores BadResource errors). - * @param {number} rid - */ - function tryClose(rid) { - try { - core.close(rid); - } catch (err) { - // Ignore error if the socket has already been closed. - if (!(err instanceof Deno.errors.BadResource)) throw err; - } - } - const handlerSymbol = Symbol("eventHandlers"); function makeWrappedHandler(handler) { function wrappedHandler(...args) { @@ -292,7 +279,7 @@ const event = new CloseEvent("close"); this.dispatchEvent(event); - tryClose(this[_rid]); + core.tryClose(this[_rid]); }, ); } else { @@ -430,7 +417,7 @@ reason, }); this.dispatchEvent(event); - tryClose(this[_rid]); + core.tryClose(this[_rid]); }, ); } @@ -484,7 +471,7 @@ reason: value.reason, }); this.dispatchEvent(event); - tryClose(this[_rid]); + core.tryClose(this[_rid]); break; } case "error": { @@ -497,7 +484,7 @@ const closeEv = new CloseEvent("close"); this.dispatchEvent(closeEv); - tryClose(this[_rid]); + core.tryClose(this[_rid]); break; } } diff --git a/ext/websocket/02_websocketstream.js b/ext/websocket/02_websocketstream.js index 4e901f53a..f7c4d4d0f 100644 --- a/ext/websocket/02_websocketstream.js +++ b/ext/websocket/02_websocketstream.js @@ -56,19 +56,6 @@ ], ); - /** - * Tries to close the resource (and ignores BadResource errors). - * @param {number} rid - */ - function tryClose(rid) { - try { - core.close(rid); - } catch (err) { - // Ignore error if the socket has already been closed. - if (!(err instanceof Deno.errors.BadResource)) throw err; - } - } - const _rid = Symbol("[[rid]]"); const _url = Symbol("[[url]]"); const _connection = Symbol("[[connection]]"); @@ -279,7 +266,7 @@ case "close": { if (this[_closing]) { this[_closed].resolve(value); - tryClose(this[_rid]); + core.tryClose(this[_rid]); } else { PromisePrototypeThen( core.opAsync("op_ws_close", { @@ -288,12 +275,12 @@ }), () => { this[_closed].resolve(value); - tryClose(this[_rid]); + core.tryClose(this[_rid]); }, (err) => { this[_closed].reject(err); controller.error(err); - tryClose(this[_rid]); + core.tryClose(this[_rid]); }, ); } @@ -303,7 +290,7 @@ const err = new Error(value); this[_closed].reject(err); controller.error(err); - tryClose(this[_rid]); + core.tryClose(this[_rid]); break; } } @@ -327,7 +314,7 @@ } }, (err) => { - tryClose(cancelRid); + core.tryClose(cancelRid); this[_connection].reject(err); this[_closed].reject(err); }, @@ -393,7 +380,7 @@ reason: closeInfo.reason, }), (err) => { - this[_rid] && tryClose(this[_rid]); + this[_rid] && core.tryClose(this[_rid]); this[_closed].reject(err); }, ); |