Skip to content

Support ECMAScript explicit resource management in Node API#639

Merged
bestbeforetoday merged 1 commit intohyperledger:mainfrom
bestbeforetoday:node-using
Oct 2, 2023
Merged

Support ECMAScript explicit resource management in Node API#639
bestbeforetoday merged 1 commit intohyperledger:mainfrom
bestbeforetoday:node-using

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 <Mark.S.Lewis@outlook.com>
@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