[whatwg/streams] WritableStream feedback ? (#755)

This example is written in typescript (to show types) and purely hypothetical as a demonstration : 

```ts
const buffer: Uint8Array = new Uint8Array(1e6);
let offset: number = 0;

const writableStream: WritableStream<number> = new WritableStream<number>({
  write(byte: number) {
    buffer[offset++] = byte;
    // here we can return a promise indicating when the write is done,
    // but we cannot provide any feedback about the value of offset for example
  },
  context(...args: any[]) { // purely hypothetical
    return { offset: offset };
  }
});
const writer: WritableStreamDefaultWriter<number> = writableStream.getWriter();
```

```ts
/** in a separate context where only writer is available (ex: in a function) **/
async function foo(writer: WritableStreamDefaultWriter<number>): boolean {
  // this could be interesting to have access to some contextual data
  const offset: number = writer.getContext().offset; // of course impossible because not defined
  if(offset > 5) {
    await writer.write(2);
  } else {
    await writer.write(1); // a void promise is always returned
  }
}
```
Without the possibility to extends WritableStreamDefaultWriter (as an es6 class) or having a specific method (like `getContext()`), it's pretty hard to get an detailed answer of the write (same for read). What I noticed, is was that the write only returns *success* or *failed*  (intentional and probably the best), we don't have any idea of the contextual state of the writable stream. The queuing strategy with backpressure does a lot of but in some case we need more.

I could be for example interested to change the value of the next `.write()` according to what send me back the writer.

The two only solutions I see here are : 
- pass `getContext()` as a callback
- or extend `WritableStreamDefaultWriter`  with `Object.definyProperty`

... but none of them sound really elegant to me. 

So we could maybe extends the UnderlyingSink with `context` and add `getContext()` to the writer ? Or have you any idea how to solve this dilemma ?

-- 
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/755

Received on Sunday, 23 July 2017 09:23:09 UTC