MCPcopy Create free account
hub / github.com/anomalyco/opencode / feed

Function feed

packages/opencode/test/cli/run/stream.transport.test.ts:104–141  ·  view source on GitHub ↗
(returnValue: R = StreamClosed)

Source from the content-addressed store, hash-verified

102const StreamClosed = undefined as never
103
104function 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
143function eventFeed() {
144 return feed<SdkEvent>()

Callers 2

eventFeedFunction · 0.70
globalFeedFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected