Re: [whatwg/streams] Reconnecting a Stream after creating it - possible? (#1004)

You can pass an `AbortSignal` to `pipeTo()` to stop an ongoing pipe. You can use `preventAbort` and `preventCancel` to prevent the aborted pipe from also cancelling the input and/or aborting the output.

Note that the pipe still waits for any pending writes to complete, and both ends remain locked until everything has been written. That means you still have to wait for the `pipeTo()` promise to settle (either resolve or reject) before you can start a new pipe.

```javascript
let currentPipePromise = Promise.resolve();
let currentPipeAbortController = new AbortController();

async function startNewPipe(dest) {
  // Abort the previous pipe
  currentPipeAbortController.abort();
  currentPipeAbortController = new AbortController();
  // Wait for previous pipe to complete
  await currentPipePromise.catch(() => {});
  // Start new pipe, but make sure we can abort it later
  currentPipePromise = src.pipeTo(dest, {
    preventAbort: true,
    preventCancel: true,
    signal: currentPipeAbortController.signal
  });
}

startNewPipe(dest1);

const pipeTo1 = () => {
  console.log('Piping to 1');
  startNewPipe(dest1);
}

const pipeTo2 = async () => {
  console.log('Piping to 2');
  startNewPipe(dest2);
}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1004#issuecomment-508271679

Received on Wednesday, 3 July 2019 22:10:36 UTC