Skip to content

Conversation

@ubik2
Copy link
Contributor

@ubik2 ubik2 commented Jul 17, 2025

As of Typescript 5.2, there's a using keyword that will automatically dispose the object when it comes off the stack.
This simplifies some exception handling that would otherwise be necessary to ensure these objects are cleaned up.

Switch over the CycleTracker to use this functionality.
We could probably do the same for transactions (there's an Symbol.asyncDispose for that), to either avoid a tx.commit(), or if we implemented a rollback, it could happen there instead.


Summary by cubic

Updated CycleTracker to use the new TypeScript 5.2 using keyword for automatic resource cleanup, reducing manual exception handling and making cycle detection code simpler.

  • Refactors
    • Replaced manual enter/exit calls with using for automatic disposal.
    • Removed redundant try/finally blocks and simplified cycle detection logic.

@ubik2
Copy link
Contributor Author

ubik2 commented Jul 17, 2025

Changes look bigger than they are.
I moved the logging into the CycleTracker (so I pass context to let me log more if desired). Previously, we had one log in CycleTracker (without context) and one outside (with context), which was a bit messy.
I generally return right after checking, instead of in an else clause at the end to simplify the flow.
I don't embed the code in a try/finally block.

@ubik2
Copy link
Contributor Author

ubik2 commented Jul 17, 2025

I'm including @seefeldb and @Gozala here for feedback on whether this pattern would also be useful in the transactions.

Copy link
Contributor

@ellyxir ellyxir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, maybe would be nice to have a unit test on the cycletracker itself but i think the overall tests give us enough coverage.

exit(k: K) {
this.partial.delete(k);
return {
[Symbol.dispose]: () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just realised, does Firefox support Symbol.dispose?

Copy link
Collaborator

@jsantell jsantell Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't currently have any polyfills (e.g. babel) in our main building pipeline, and would think the platform needs to support the feature translated by typescript. It looks like this resource management API is still not in release on Firefox, and unsupported on Safari https://caniuse.com/mdn-javascript_statements_using

We could 1) investigate polyfilling this feature for browsers, 2) only use these features in Deno components, or 3) avoid using using, edit: 4) or only support browsers that implement this feature

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: Looks like the TypeScript compiler tsc translates the using functionality to non-"using" JS, but we use esbuild in most cases (shell, deno-web-test, jumble->vite->esbuild) which transparently copies over using functionality, requiring runtime support. We could explore esbuild plugins to translate this, or switch to using tsc for compiling (a larger lift)

@ellyxir ellyxir self-requested a review July 17, 2025 23:00
@ubik2
Copy link
Contributor Author

ubik2 commented Jul 21, 2025

Jordan modified the esbuild parameters to handle using there (generating the disposable invocation code), and then incorporated parts of core.js to provide the polyfill for Symbol.disposable.

@ubik2 ubik2 merged commit fbbb829 into main Jul 22, 2025
7 checks passed
@ubik2 ubik2 deleted the feat/use-using-for-cycle-tracker branch July 22, 2025 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants