| 145 | ) |
| 146 | |
| 147 | const write = (chunk: Uint8Array | string | Socket.CloseEvent) => |
| 148 | latch.whenOpen(Effect.suspend(() => { |
| 149 | const { conn, writer } = current! |
| 150 | if (Socket.isCloseEvent(chunk)) { |
| 151 | tearingDown = true |
| 152 | return close(conn) |
| 153 | } |
| 154 | const bytes = typeof chunk === "string" ? encoder.encode(chunk) : chunk |
| 155 | return Effect.tryPromise({ |
| 156 | try: () => writer.ready.then(() => writer.write(bytes)), |
| 157 | catch: (cause) => |
| 158 | new Socket.SocketError({ |
| 159 | reason: new Socket.SocketWriteError({ cause }) |
| 160 | }) |
| 161 | }) |
| 162 | })) |
| 163 | |
| 164 | const writer = Effect.acquireRelease( |
| 165 | Effect.sync(() => { |