Re: [w3ctag/design-reviews] How should web standards use the new JavaScript Symbol.dispose protocol? (Discussion #800)

I could imagine several approaches:

1. `beginLayer` returns a disposable that calls `endLayer()` for you (assuming you can validate the nesting level, or leave it to the user to ensure they don't call `.endLayer()`):
  ```js
  const ctx = canvas.getContext('2d');
  {
    using _ = ctx.beginLayer(...); // binding doesn't matter here
    ctx.fillStyle = 'rgba(225, 0, 0, 1)';
    ctx.fillRect(50, 50, 75, 50);
  } // '_[Symbol.dispose]()' calls 'ctx.endLayer()'
  ```
2. Same as above, except you remove `endLayer()` and depend on the return value of `beginLayer()`: 
  ```js
  const ctx = canvas.getContext('2d');
  {
    using layer = ctx.beginLayer(...); // binding doesn't matter here
    ctx.fillStyle = 'rgba(225, 0, 0, 1)';
    ctx.fillRect(50, 50, 75, 50);
  } // 'layer[Symbol.dispose]()' performs compositing against 'ctx'
  ```
3. Or, instead have a `createLayer()` that returns the layer that you manipulate instead:
  ```js
  const ctx = canvas.getContext('2d');
  {
    using layer = ctx.createLayer(...);
    layer.fillStyle = 'rgba(225, 0, 0, 1)';
    layer.fillRect(50, 50, 75, 50);
  } // 'layer[Symbol.dispose]()' performs compositing against 'ctx'
  ```

With option (2), you could still support imperative nesting by using the return value, even without immediately supporting `Symbol.dispose`:

```js
const ctx = canvas.getContext('2d');
const layer = ctx.beginLayer(...);
ctx.fillStyle = 'rgba(225, 0, 0, 1)';
ctx.fillRect(50, 50, 75, 50);
layer.end(); // which would eventually be aliased by '[Symbol.dispose]()`
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3ctag/design-reviews/discussions/800#discussioncomment-4739751
You are receiving this because you are subscribed to this thread.

Message ID: <w3ctag/design-reviews/repo-discussions/800/comments/4739751@github.com>

Received on Friday, 20 January 2023 17:35:04 UTC