Re: [whatwg/streams] Reset for TransformStream (and WritableStream?) (#1026)

> I would imagine that a Transformer that does not implement reset would instead flush, whatever that means.

With the current semantics, we call `flush()` when the writable end is closed. This "completes" the transform stream: after that call, we'll never call `transform()` or `flush()` again (we'll even [clear those algorithms](https://streams.spec.whatwg.org/#transform-stream-default-controller-clear-algorithms) so that we *can never* call them again).

A developer can thus use `flush()` to clean up resources. For example, imagine a transform stream that offloads processing to a worker:
```javascript
new TransformStream({
  start(controller) {
    this.worker = new Worker("my-worker.js");
    this.worker.onmessage = (event) => {
      controller.enqueue(event.data);
    };
  },
  transform(chunk) {
    this.worker.postMessage(chunk);
  },
  flush() {
    this.worker.terminate();
  }
});
```
This transform stream uses `flush()` to destroy the worker created in `start()`. This works, because `transform()` can never be called again after `flush()`.

With the proposed semantics of `reset()` "falling back" to `flush()`, this pattern would no longer work.

---

Now, `flush()` might not be the right method to clean up such resources. `flush()` is only called when the writable end is closed, not when the writable end is aborted or the readable end is cancelled. In such cases, the transform stream does not call *any method* on its transformer. Perhaps we need a new transformer method to fill this gap?

-- 
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/1026#issuecomment-581022283

Received on Saturday, 1 February 2020 11:44:42 UTC