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

Explicit resource management (using keyword) for Node API #636

Closed
6 of 18 tasks
bestbeforetoday opened this issue Sep 25, 2023 · 0 comments · Fixed by #639
Closed
6 of 18 tasks

Explicit resource management (using keyword) for Node API #636

bestbeforetoday opened this issue Sep 25, 2023 · 0 comments · Fixed by #639
Assignees
Labels
client Relates to Fabric Gateway client enhancement New feature or request
Milestone

Comments

@bestbeforetoday
Copy link
Member

bestbeforetoday commented Sep 25, 2023

As an Node application developer
I want to use explicit resource management (using keyword)
So that I don't need to write clean-up code using try/finally blocks to ensure resources are closed after use

TypeScript 5.2 adds support for the using keyword, defined by the upcoming Explicit Resource Management ECMAScript feature. For closeable resources that are only used within a defined scope, this avoids the need for clean-up code (typically implemented using try/finally blocks to ensure it is called in the event of an error) to close resources. It provides similar capability to Java's try-with-resources, and (to some extent) Go's defer.

This feature proposes that all objects in the Node API with close() methods to perform resource clean-up are augmented to implement the Disposable type, and have a [Symbol.dispose] method that calls the existing close().

For example, the existing application code:

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

Could be simplified to the following, with the close() automatically called 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
}

Tasks:

  • Augment CloseableAsyncIterable
    • Chaincode events
    • Block events
    • Filtered block events
    • Block and private data events
  • Augment Gateway
  • Add new code examples to API documentation:
    • Network
    • Network#getChaincodeEvents
    • Network#getBlockEvents
    • Network#getFilteredBlockEvents
    • Network#getBlockAndPrivateDataEvents
    • ChaincodeEventsRequest#getEvents
    • BlockEventsRequest#getEvents
    • FilteredBlockEventsRequest#getEvents
    • BlockAndPrivateDataEventsRequest#getEvents

The following samples should also be updated once this capability is published:

@bestbeforetoday bestbeforetoday added enhancement New feature or request client Relates to Fabric Gateway client labels Sep 25, 2023
@bestbeforetoday bestbeforetoday self-assigned this Sep 30, 2023
@bestbeforetoday bestbeforetoday added this to the v1.4 milestone Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Relates to Fabric Gateway client enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant