* Reject any in-flight sends and mark the connection failed. Idempotent so * that close() and the underlying 'close'/'error' events do not double-reject. * @param {Error} error * @private
(error)
| 147 | * @private |
| 148 | */ |
| 149 | _failPending(error) { |
| 150 | if (this._closed) { |
| 151 | return |
| 152 | } |
| 153 | this._closed = true |
| 154 | this.connected = false |
| 155 | for (const { reject, timeoutId } of this._pending.values()) { |
| 156 | clearTimeout(timeoutId) |
| 157 | reject(error) |
| 158 | } |
| 159 | this._pending.clear() |
| 160 | // Reject any callers parked in waitForConnection() so close() (or an |
| 161 | // unexpected disconnect) cannot leave them hanging forever. |
| 162 | for (const { reject } of this._connectWaiters) { |
| 163 | reject(error) |
| 164 | } |
| 165 | this._connectWaiters.clear() |
| 166 | // Detach every addCallback() listener too. Once closed, removeCallback() |
| 167 | // can no longer reach the remote end (send() would just throw), so nothing |
| 168 | // else will ever detach these listeners. Drop them here instead of leaving |
| 169 | // them attached to an EventEmitter nothing will ever emit on again. |
| 170 | for (const { method, handler } of this._callbacks.values()) { |
| 171 | this.off(method, handler) |
| 172 | } |
| 173 | this._callbacks.clear() |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Emits `err` as an 'error' event if anything is listening for one, |
no test coverage detected