console icon indicating copy to clipboard operation
console copied to clipboard

Proposal: Integration with Symbol.dispose

Open RReverser opened this issue 5 months ago • 4 comments

What problem are you trying to solve?

I propose the following methods should integrate with the explicit resource management proposal that has now shipped in several browsers:

  • group
  • groupCollapsed
  • profile
  • time

These methods should return a disposable object so that instead of e.g.

console.group("my group");
console.time("my label");
...
console.timeEnd("my label");
console.groupEnd();

user could write

using group = console.group("my group");
using time = console.time("my label");
...
// groups and times are automatically ended at the end of the scope

What solutions exist today?

Explicit console.*End() methods.

How would you solve it?

The start methods should return an object that has a single Symbol.dispose property. This property is mapped to the explicit methods with the same label as the starting method.

Anything else?

There is a web-compat risk in changing the return type from current undefined to an object, as some code out there might be relying on it being undefined / false-y at the moment.

If so, we might have to either

  • Introduce an option object, so one writes console.group({ label: "my group", disposable: true }) if they want to get a disposable object.
  • Introduce new dedicated methods for the disposable counterparts of all existing methods in the list above.

RReverser avatar Aug 04 '25 10:08 RReverser

This has bugged me a bit from the beginning, having been added in their current form as a result of them already existing in multiple browsers. Thanks for your proposal.

My preference would be a new dedicated function that return an object, even though I don't have a good name for them at the moment. I have concerns that some users would already be providing an object to these methods and relying on toString behavior to create the label.

How would nesting work? Would you do (e.g.) using myGroup.groupDisp("new nested group")?

terinjokes avatar Aug 04 '25 11:08 terinjokes

I have concerns that some users would already be providing an object to these methods and relying on toString behavior to create the label.

That should be fine and backwards compatible; as long as the object doesn't have disposable: true, the behaviour would be just like before.

How would nesting work? Would you do (e.g.) using myGroup.groupDisp("new nested group")?

I don't think that's necessary - or, at least, I assumed that it would work just like today:

using _group1 = console.group("parent");
using _group2 = console.group("child");
...

I guess adding helper methods to the returned object could be nice too, but it's out of scope of this proposal.

RReverser avatar Aug 04 '25 11:08 RReverser

I'm reminded of some of the discussion around nesting in #193.

terinjokes avatar Aug 04 '25 11:08 terinjokes

Yeah I looked through it as well, and thought about reusing its return object, but it only helps for grouping, not other mentioned APIs.

RReverser avatar Aug 04 '25 12:08 RReverser