Skip to content

Commit

Permalink
Add support for disposing communicator with "await using" (#3526)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Feb 10, 2025
1 parent c7a5a9b commit f24e5f0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions js/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ for (const name of tests) {
let bundle = await rollup({
input: input,
plugins: [IceResolver(), NodeMockupResolver()],
onwarn: (warning, next) => {
// Ignore the "this is undefined" warning, let rollup silently rewrite it.
// This avoids warnings from the TypeScript polyfills for async disposable resources.
if (warning.code === "THIS_IS_UNDEFINED") return;
next(warning);
},
});
await bundle.write({
file: path.join("dist", name, "index.js"),
Expand Down
5 changes: 5 additions & 0 deletions js/src/Ice/Communicator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ declare module "ice" {
*/
destroy(): Promise<void>;

/**
* Asynchronously disposes this communicator. It's an alias for {@link Communicator#destroy}.
*/
[Symbol.asyncDispose](): Promise<void>;

/**
* Shuts down this communicator. In JavaScript, shutdown resolves a promise and doesn't do anything else.
*
Expand Down
4 changes: 4 additions & 0 deletions js/src/Ice/Communicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class Communicator {
return this._instance.destroy();
}

[Symbol.asyncDispose]() {
return this.destroy();
}

shutdown() {
if (!this._isShutdown) {
this._isShutdown = true;
Expand Down
2 changes: 1 addition & 1 deletion js/test/Ice/properties/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Client extends TestHelper {
}

{
const communicator = Ice.initialize(args);
await using communicator = Ice.initialize(args);
const properties = communicator.getProperties();

out.write("testing that creating an object adapter with unknown properties throws an exception...");
Expand Down

0 comments on commit f24e5f0

Please sign in to comment.