Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ECMAScript explicit resource management in Node API #639

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

bestbeforetoday
Copy link
Member

@bestbeforetoday bestbeforetoday commented Sep 30, 2023

[Symbol.dispose]() implementation added to Gateway and CloseableAsyncIterable. For environments that support ECMAScript resource management (such as TypeScript 5.2 and later), this allows the using keyword to be used to automatically close chaincode and block event iterables when they go out of scope instead of explcitly calling close with a try/finally block.

For example, this eventing code:

const events = await network.getChaincodeEvents(chaincodeName, { startBlock: BigInt(101) });
try {
    for async (const event of events) {
        // Process event
    }
} finally {
    events.close();
}

Can be replaced with the following, where the clean-up carried out by calling events.close() is done automatically when the events variable goes out of scope:

using events = await network.getChaincodeEvents(chaincodeName, { startBlock: BigInt(101) });
for async (const event of events) {
    // Process event
}

Closes #636

@bestbeforetoday bestbeforetoday changed the title Support ECMAScript resource management in Node API Support ECMAScript explicit resource management in Node API Sep 30, 2023
[Symbol.dispose]() implementation added to Gateway and
CloseableAsyncIterable, which allows the `using` keyword to be used to
automatically close chaincode and block event iterables when they go out
of scope instead of explcitly calling close with a try/finally block.

Signed-off-by: Mark S. Lewis <[email protected]>
@bestbeforetoday bestbeforetoday marked this pull request as ready for review September 30, 2023 14:34
@bestbeforetoday bestbeforetoday requested a review from a team as a code owner September 30, 2023 14:34
@bestbeforetoday bestbeforetoday enabled auto-merge (squash) September 30, 2023 14:34
@bestbeforetoday bestbeforetoday merged commit 3fcc067 into hyperledger:main Oct 2, 2023
@bestbeforetoday bestbeforetoday deleted the node-using branch October 2, 2023 09:01
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.

Explicit resource management (using keyword) for Node API
2 participants