Implement async stream abstractions with enhanced cancellation and error handling #77
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces a comprehensive set of async stream abstractions that build on the existing
AsyncIterableObject
infrastructure, providing easy-to-use utilities for common streaming patterns while maintaining robust cancellation and error handling support.Problem Statement
Working with async streams in VS Code extension development often requires implementing repetitive patterns for:
The existing
AsyncIterableObject
provides solid foundations but lacks higher-level abstractions for these common use cases.Solution
Created
src/util/asyncStreamUtils.ts
with:Core Utilities (
AsyncStreamUtils
class)buffer()
- Groups items into chunks of specified sizethrottle()
- Limits rate of item emission with precise timingdebounce()
- Emits items only after periods of inactivitymerge()
- Combines multiple streams with error isolationwithTimeout()
- Prevents hanging streams with configurable timeoutsretry()
- Automatic retry with configurable attempts and delaysmapWithErrorHandling()
- Enhanced transformation with sophisticated error handlingFluent API (
AsyncStreamBuilder
class)Enables chainable operations for readable, composable stream processing:
Factory Functions
fromArray()
- Create streams from arraysfromPromise()
- Create streams from promise of arrayscreateAsyncStreamBuilder()
- Wrap existing async iterablesKey Features
🔒 Robust Cancellation Support
All operations support
CancellationToken
with proper cleanup:🛡️ Sophisticated Error Handling
Multiple error handling strategies:
⚡ Performance Optimized
🔄 Real-World Patterns
Handles common scenarios like API processing:
Architecture
AsyncIterableObject
andCancelableAsyncIterableObject
Files Added
src/util/asyncStreamUtils.ts
- Main implementation (430 lines)src/util/test/asyncStreamUtils.test.ts
- Comprehensive test suite (350+ lines)src/util/README_asyncStreamUtils.md
- Documentation and usage examplessrc/util/asyncStreamUtils.examples.ts
- Practical demos and patternsTesting
Comprehensive test coverage including:
Usage Examples
See
asyncStreamUtils.examples.ts
for comprehensive demonstrations of:This implementation provides a robust, easy-to-use foundation for async stream processing while maintaining full compatibility with VS Code's existing async infrastructure.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.