| 1213 | } |
| 1214 | }) |
| 1215 | const writeAll = ( |
| 1216 | chunks: NonEmptyReadonlyArray<Uint8Array | string> |
| 1217 | ): Effect.Effect<void, SocketError> => |
| 1218 | Effect.suspend(() => { |
| 1219 | if (terminalError !== undefined) return Effect.fail(terminalError) |
| 1220 | const ws = currentWS |
| 1221 | if (ws === undefined) return latch.whenOpen(writeAll(chunks)) |
| 1222 | try { |
| 1223 | for (let i = 0; i < chunks.length; i++) { |
| 1224 | ws.send(chunks[i] as string | Uint8Array<ArrayBuffer>) |
| 1225 | } |
| 1226 | return Effect.void |
| 1227 | } catch (cause) { |
| 1228 | return Effect.fail(new SocketError({ reason: new SocketWriteError({ cause }) })) |
| 1229 | } |
| 1230 | }) |
| 1231 | const writer: Socket["writer"] = Effect.succeed({ write, writeAll }) |
| 1232 | |
| 1233 | return Effect.succeed(make({ reader, writer })) |