Re: [whatwg/streams] Should there be dedicated VideoFrame ReadableStream/WritableStream constructs (Issue #1187)

Transferable solves the problem of moving ownership, but it doesn't solve explicit cleanup.

The [explicit resource management proposal](https://github.com/tc39/proposal-explicit-resource-management) seems problematic when combined with transferable objects. What happens if user-land code overrides `VideoFrame.prototype[Symbol.dispose]`, then transfers a `ReadableStream` containing `VideoFrame`s, and then cancels that transferred stream? Should we call `[Symbol.dispose]` in the new realm, or should we transfer those `VideoFrame`s back to the original realm *and then call dispose on them*?

We could add a concept for closeable objects, on the same level as [transferable objects](https://html.spec.whatwg.org/multipage/structured-data.html#transferable-objects). Closeable IDL interfaces must be annotated with `[Closeable]`, and must implement "close steps". If `VideoFrame` is `[Transferable, Closeable]`, then we have all the tools we need to support proper ownership transfer and explicit cleanup.

Of course, the disadvantage is that regular JavaScript objects cannot be made closeable, so they cannot benefit from this cleanup mechanism. But perhaps we can still fix this in the future? If `[Symbol.dispose]` becomes a thing, we could do something like:
> To close a _chunk_, perform the following steps:
> 1. If _chunk_ is closable (i.e. it implements a `[Closeable]` interface), then perform its close steps.
> 1. Otherwise, if _chunk_ has a `[Symbol.dispose]` method, then call this method.
> 1. Otherwise, do nothing.

That way, platform objects like `VideoFrame` would *always* use their spec-defined close steps, so we don't have to worry about `VideoFrame.prototype[Symbol.dispose]`. But if user-land code introduces a `[Symbol.dispose]` method for certain objects, then we'll still call it.

We probably shouldn't assume that *every* stream containing closeable chunks *wants* those chunks to be closed in this way. If the chunks aren't transferable (which will be the case for user-land objects), then they may still be used *outside* the stream. So I would make this an opt-in with some sort of `closeableChunks: true` option on the underlying source/transformer/sink.

When `type: "transfer"`, then `ReadableStream` is *guaranteed* to be the exclusive owner of its chunks, so `closeableChunks: true` is implied.

-- 
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/1187#issuecomment-972733678

Received on Thursday, 18 November 2021 10:26:21 UTC