* Detach the current socket from this handler * @param close Whether to close the socket when detaching (overrides constructor option) * @returns this handler instance
(close?: boolean)
| 121 | * @returns this handler instance |
| 122 | */ |
| 123 | public detach(close?: boolean): PGLiteSocketHandler { |
| 124 | this.log(`detach: detaching socket, close=${close ?? this.closeOnDetach}`) |
| 125 | |
| 126 | if (!this.socket) { |
| 127 | this.log(`detach: no socket attached, nothing to do`) |
| 128 | return this |
| 129 | } |
| 130 | |
| 131 | // Remove all listeners |
| 132 | this.socket.removeAllListeners('data') |
| 133 | this.socket.removeAllListeners('error') |
| 134 | this.socket.removeAllListeners('close') |
| 135 | |
| 136 | // Close the socket if requested |
| 137 | if (close ?? this.closeOnDetach) { |
| 138 | if (this.socket.writable) { |
| 139 | this.log(`detach: closing socket`) |
| 140 | this.socket.end() |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Release the lock on the PGlite instance |
| 145 | this.log(`detach: releasing exclusive lock on PGlite instance`) |
| 146 | this.resolveLock?.() |
| 147 | |
| 148 | this.socket = null |
| 149 | this.active = false |
| 150 | return this |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Check if a socket is currently attached |
no test coverage detected