| 201 | ) |
| 202 | |
| 203 | const write = (chunk: Uint8Array | string | Socket.CloseEvent) => |
| 204 | latch.whenOpen(Effect.callback<void, Socket.SocketError>((resume) => { |
| 205 | const conn = currentSocket! |
| 206 | if (Socket.isCloseEvent(chunk)) { |
| 207 | conn.destroy(chunk.code > 1000 ? new Error(`closed with code ${chunk.code}`) : undefined) |
| 208 | return resume(Effect.void) |
| 209 | } |
| 210 | currentSocket!.write(chunk, (cause) => { |
| 211 | resume( |
| 212 | cause |
| 213 | ? Effect.fail( |
| 214 | new Socket.SocketError({ |
| 215 | reason: new Socket.SocketWriteError({ cause: cause! }) |
| 216 | }) |
| 217 | ) |
| 218 | : Effect.void |
| 219 | ) |
| 220 | }) |
| 221 | })) |
| 222 | |
| 223 | const writer = Effect.acquireRelease( |
| 224 | Effect.succeed(write), |