| 63 | * @category constructors |
| 64 | */ |
| 65 | export const makeChannel = <IE, Done>( |
| 66 | options?: DecodeOptions & { |
| 67 | readonly bufferSize?: number |
| 68 | } |
| 69 | ): Channel.Channel< |
| 70 | Chunk.Chunk<Event>, |
| 71 | Chunk.Chunk<string>, |
| 72 | IE, |
| 73 | IE, |
| 74 | void, |
| 75 | Done |
| 76 | > => { |
| 77 | const events = Mailbox.make<Event, IE | Retry>(options?.bufferSize ?? 16).pipe( |
| 78 | Effect.map((mailbox) => { |
| 79 | let events: Array<Event> = [] |
| 80 | let retry: Retry | undefined |
| 81 | const parser = makeParser((event) => { |
| 82 | switch (event._tag) { |
| 83 | case "Retry": |
| 84 | return (retry = event) |
| 85 | case "Event": |
| 86 | return events.push(event) |
| 87 | } |
| 88 | }, options) |
| 89 | const input: AsyncInput.AsyncInputProducer< |
| 90 | IE, |
| 91 | Chunk.Chunk<string>, |
| 92 | Done |
| 93 | > = { |
| 94 | awaitRead() { |
| 95 | return Effect.void |
| 96 | }, |
| 97 | emit(chunks) { |
| 98 | let error: SseError | undefined |
| 99 | Chunk.forEach(chunks, (chunk) => { |
| 100 | if (error === undefined) { |
| 101 | error = parser.feed(chunk) |
| 102 | } |
| 103 | }) |
| 104 | const toEmit = events |
| 105 | events = [] |
| 106 | return error |
| 107 | ? Effect.zipRight(mailbox.offerAll(toEmit), mailbox.failCause(Cause.die(error))) |
| 108 | : retry |
| 109 | ? Effect.zipRight(mailbox.offerAll(toEmit), mailbox.fail(retry)) |
| 110 | : mailbox.offerAll(toEmit) |
| 111 | }, |
| 112 | error(cause) { |
| 113 | return mailbox.failCause(cause) |
| 114 | }, |
| 115 | done(_) { |
| 116 | return mailbox.end |
| 117 | } |
| 118 | } |
| 119 | return Channel.embedInput(Mailbox.toChannel(mailbox), input) |
| 120 | }), |
| 121 | Channel.unwrap |
| 122 | ) |