concurrent.js
This file
Path
lib/convert/concurrent.js
33 lines · 1.1 KiB · JavaScript
Last changed by
Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
History of this fileTake it
Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 1why, one step back1import {createDeferred} from '../utils/deferred.js';Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
line 2why, one step back2Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
lines 3–33why, one step back3// When using multiple `.readable()`/`.writable()`/`.duplex()`, `final` and `destroy` should wait for other streams4export const initializeConcurrentStreams = () => ({5 readableDestroy: new WeakMap(),6 writableFinal: new WeakMap(),7 writableDestroy: new WeakMap(),8});910// Each file descriptor + `waitName` has its own array of promises.11// Each promise is a single `.readable()`/`.writable()`/`.duplex()` call.12export const addConcurrentStream = (concurrentStreams, stream, waitName) => {13 const weakMap = concurrentStreams[waitName];14 if (!weakMap.has(stream)) {15 weakMap.set(stream, []);16 }1718 const promises = weakMap.get(stream);19 const promise = createDeferred();20 promises.push(promise);21 const resolve = promise.resolve.bind(promise);22 return {resolve, promises};23};2425// Wait for other streams, but stop waiting when subprocess ends26export const waitForConcurrentStreams = async ({resolve, promises}, subprocess) => {27 resolve();28 const [isSubprocessExit] = await Promise.race([29 Promise.allSettled([true, subprocess]),30 Promise.all([false, ...promises]),31 ]);32 return !isSubprocessExit;33};