arda-org / xSuite

🛠️ Init, Build, Test, Deploy MultiversX smart contracts in seconds. The full suite for efficiently developing high-quality contracts.
https://xsuite.dev
MIT License
13 stars 4 forks source link

Use `using` for server.listen / .close #193

Closed lcswillems closed 3 weeks ago

lcswillems commented 3 months ago

In this code:

https://github.com/arda-org/xSuite/blob/ac3c108c656cebe2bd4e2cd79064b15688d4bfb2/xsuite/src/cli/cli.test.ts#L295-L301

We first do server.listen and then server.close. However if there is an error in between, the server will never be closed and interception of requests will continue. server.close should always be called, and the default way of doing is to use try / finally. However, this is going to make the code way less readable.

We could rather use using:

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management

The idea is to create an helper that returns and object that has this method containing the cleanup logic:

[Symbol.dispose]() {
    // cleanup
}
lcswillems commented 2 months ago

This doesn't work and gives errors of the kind: image