MCPcopy Create free account
hub / github.com/BrainJS/brain.js / clearBuffer

Function clearBuffer

browser.js:25365–25424  ·  view source on GitHub ↗
(stream, state)

Source from the content-addressed store, hash-verified

25363
25364// if there's something in the buffer waiting, then process it
25365function clearBuffer(stream, state) {
25366 state.bufferProcessing = true;
25367 var entry = state.bufferedRequest;
25368
25369 if (stream._writev && entry && entry.next) {
25370 // Fast case, write everything using _writev()
25371 var l = state.bufferedRequestCount;
25372 var buffer = new Array(l);
25373 var holder = state.corkedRequestsFree;
25374 holder.entry = entry;
25375
25376 var count = 0;
25377 var allBuffers = true;
25378 while (entry) {
25379 buffer[count] = entry;
25380 if (!entry.isBuf) allBuffers = false;
25381 entry = entry.next;
25382 count += 1;
25383 }
25384 buffer.allBuffers = allBuffers;
25385
25386 doWrite(stream, state, true, state.length, buffer, '', holder.finish);
25387
25388 // doWrite is almost always async, defer these to save a bit of time
25389 // as the hot path ends with doWrite
25390 state.pendingcb++;
25391 state.lastBufferedRequest = null;
25392 if (holder.next) {
25393 state.corkedRequestsFree = holder.next;
25394 holder.next = null;
25395 } else {
25396 state.corkedRequestsFree = new CorkedRequest(state);
25397 }
25398 state.bufferedRequestCount = 0;
25399 } else {
25400 // Slow case, write chunks one-by-one
25401 while (entry) {
25402 var chunk = entry.chunk;
25403 var encoding = entry.encoding;
25404 var cb = entry.callback;
25405 var len = state.objectMode ? 1 : chunk.length;
25406
25407 doWrite(stream, state, false, len, chunk, encoding, cb);
25408 entry = entry.next;
25409 state.bufferedRequestCount--;
25410 // if we didn't call the onwrite immediately, then
25411 // it means that we need to wait until it does.
25412 // also, that means that the chunk and cb are currently
25413 // being processed, so move the buffer counter past them.
25414 if (state.writing) {
25415 break;
25416 }
25417 }
25418
25419 if (entry === null) state.lastBufferedRequest = null;
25420 }
25421
25422 state.bufferedRequest = entry;

Callers 2

browser.jsFile · 0.70
onwriteFunction · 0.70

Calls 1

doWriteFunction · 0.70

Tested by

no test coverage detected