MCPcopy Create free account
hub / github.com/Effect-TS/effect / drain

Function drain

packages/platform/node-shared/src/NodeSocket.ts:214–243  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

212 // The only place a waiter is resumed, so delivery order has one owner:
213 // retained bytes first, then freshly read bytes, then the error.
214 function drain() {
215 if (waiter === undefined || reading) return
216 let chunk = pending
217 pending = undefined
218 if (chunk === undefined && !closed) {
219 // A data listener can interrupt and replace the pull synchronously.
220 // The replacement must wait for this read to preserve byte order.
221 reading = true
222 try {
223 chunk = readAvailable(conn, chunkSize)
224 } finally {
225 reading = false
226 }
227 }
228 // A close during read() discards the bytes it consumed.
229 if (closed) chunk = undefined
230 if (waiter === undefined) {
231 pending = chunk
232 return
233 }
234 const result = chunk !== undefined
235 ? Effect.succeed(chunk)
236 : error !== undefined
237 ? Effect.fail(error)
238 : undefined
239 if (result === undefined) return
240 const resume = waiter
241 waiter = undefined
242 resume(result)
243 }
244 // Normal EOF: already consumed bytes are still delivered before the error.
245 function end(err: Socket.SocketError) {
246 error ??= err

Callers 3

endFunction · 0.70
closeFunction · 0.70
fromDuplexFunction · 0.70

Calls 4

readAvailableFunction · 0.85
failMethod · 0.65
resumeFunction · 0.50
succeedMethod · 0.45

Tested by

no test coverage detected