(returnValue: R = StreamClosed)
| 102 | const StreamClosed = undefined as never |
| 103 | |
| 104 | function feed<T, R = never>(returnValue: R = StreamClosed) { |
| 105 | const list: T[] = [] |
| 106 | let done = false |
| 107 | let wake: (() => void) | undefined |
| 108 | |
| 109 | const wrapped = (async function* (): AsyncGenerator<T, R, unknown> { |
| 110 | while (!done || list.length > 0) { |
| 111 | if (list.length === 0) { |
| 112 | await new Promise<void>((resolve) => { |
| 113 | wake = resolve |
| 114 | }) |
| 115 | continue |
| 116 | } |
| 117 | |
| 118 | const next = list.shift() |
| 119 | if (!next) { |
| 120 | continue |
| 121 | } |
| 122 | |
| 123 | yield next |
| 124 | } |
| 125 | return returnValue as R |
| 126 | })() |
| 127 | |
| 128 | return { |
| 129 | stream: wrapped, |
| 130 | push(value: T) { |
| 131 | list.push(value) |
| 132 | wake?.() |
| 133 | wake = undefined |
| 134 | }, |
| 135 | close() { |
| 136 | done = true |
| 137 | wake?.() |
| 138 | wake = undefined |
| 139 | }, |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | function eventFeed() { |
| 144 | return feed<SdkEvent>() |
no outgoing calls
no test coverage detected