Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WritableStream feedback ? #755

Closed
lifaon74 opened this issue Jul 23, 2017 · 2 comments
Closed

WritableStream feedback ? #755

lifaon74 opened this issue Jul 23, 2017 · 2 comments

Comments

@lifaon74
Copy link

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

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();
/** 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 ?

@ricea
Copy link
Collaborator

ricea commented Jul 24, 2017

We have to focus on use cases that are broadly applicable in order to avoid WritableStream becoming so complex that no-one can understand it.

My personal feeling is that the use case for getContext() is not compelling enough to justify adding it.

In this case I would pass offset along with writer to foo() to achieve the desired functionality in the simplest way possible.

@lifaon74
Copy link
Author

Ok, this is what I was thinking, I was just wondering if someone already addressed this "issue".

In my example, foo could be really far in the stack (imagine a a->b->c->....->z->foo) and passing every time getContext and writer could be a pain. offset cannot be directly passed because it evolves with the call of write. So a valid solution could be to pass an object { writer, getOffset } instead but less cool than having the possibility to extend the writer ;)

I'll let open this issue 1~2 days if somebody else could be interested, and I'll close it after. Thanks for your answer.

@domenic domenic closed this as completed Sep 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants