From 6dc8682b9acabea56fd69a25c28b6a8f95c2ce26 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 14 Sep 2023 14:08:59 -0400 Subject: feat: explicit resource management in TypeScript (#20506) This adds support for `using` and `await using` declarations in TypeScript only. We need to wait for v8 to support it for this to work in JS. --- .../run/explicit_resource_management/main.out | 5 +++++ .../run/explicit_resource_management/main.ts | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 cli/tests/testdata/run/explicit_resource_management/main.out create mode 100644 cli/tests/testdata/run/explicit_resource_management/main.ts (limited to 'cli/tests/testdata') diff --git a/cli/tests/testdata/run/explicit_resource_management/main.out b/cli/tests/testdata/run/explicit_resource_management/main.out new file mode 100644 index 000000000..ff5ac4b59 --- /dev/null +++ b/cli/tests/testdata/run/explicit_resource_management/main.out @@ -0,0 +1,5 @@ +A +Disposed +B +Async disposed +C diff --git a/cli/tests/testdata/run/explicit_resource_management/main.ts b/cli/tests/testdata/run/explicit_resource_management/main.ts new file mode 100644 index 000000000..0201a51f9 --- /dev/null +++ b/cli/tests/testdata/run/explicit_resource_management/main.ts @@ -0,0 +1,21 @@ +class Resource { + [Symbol.dispose]() { + console.log("Disposed"); + } +} +class AsyncResource { + async [Symbol.asyncDispose]() { + await new Promise((resolve) => setTimeout(resolve, 10)); + console.log("Async disposed"); + } +} + +{ + using resource = new Resource(); + console.log("A"); +} +{ + await using resource = new AsyncResource(); + console.log("B"); +} +console.log("C"); -- cgit v1.2.3