hyperledger / fabric-gateway

Go, Node and Java client API for Hyperledger Fabric v2.4+
https://hyperledger.github.io/fabric-gateway/
Apache License 2.0
150 stars 87 forks source link

Support ECMAScript explicit resource management in Node API #639

Closed bestbeforetoday closed 11 months ago

bestbeforetoday commented 11 months ago

[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