Re: [whatwg/webidl] An undefined dictionary member in IDL->JS conversion: exist or not? (Issue #1094)

The Streams standard aims to align the return value of `reader.read()` with the return value of `iterator.next()`.
```javascript
let array = ["a", "b"];
let it = array[Symbol.iterator]();
it.next(); // { done: false, value: "a" }
it.next(); // { done: false, value: "b" }
it.next(); // { done: true, value: undefined }

let readable = new ReadableStream({
  start(c) {
    c.enqueue("a");
    c.enqueue("b");
    c.close();
  }
});
let reader = readable.getReader();
await reader.read(); // { done: false, value: "a" }
await reader.read(); // { done: false, value: "b" }
await reader.read(); // { done: true, value: undefined }
```

Before we switched to Web IDL in https://github.com/whatwg/streams/commit/caf3cea28134d8be2ec69b6a1105748a7c8d10b5, we tried to match this by having [ReadableStreamCreateReadResult](https://streams.spec.whatwg.org/commit-snapshots/cfcd303a38158e4ec3837d1028158c88d505c35d/#readable-stream-create-read-result) behave the same as ECMAScript's [CreateIterResultObject](https://tc39.es/ecma262/#sec-createiterresultobject). So a read result would *always* have `done` and `value` properties, even if the latter were `undefined`.

Today in Web IDL, the same CreateIterResultObject function is used to [construct an iterator result](https://webidl.spec.whatwg.org/#iterator-result). So that *also* always has both properties, but it isn't used to construct a `read()` result.

I don't know whether it's acceptable to return `{ done: true }` instead of `{ done: true, value: undefined }`. One can argue that matching the return values of `iterator.next()` is less important now that [`ReadableStream` has its own dedicated async iterator](https://streams.spec.whatwg.org/#rs-asynciterator). But then again, we've had `{ done: true, value: undefined }` on `read()` for a while now, and it might be too late to change it.

@domenic @ricea What are your thoughts on this?

---

Alternatively, would this be allowed?
```
dictionary ReadableStreamBYOBReadResult {
  (ArrayBufferView or undefined) value;
  boolean done;
};
```
That would more closely match our current behavior.

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

Message ID: <whatwg/webidl/issues/1094/1026903486@github.com>

Received on Tuesday, 1 February 2022 14:29:28 UTC