[dotnet] [bidi] Simplify event streaming to implicitly unsubscribe#17705
[dotnet] [bidi] Simplify event streaming to implicitly unsubscribe#17705nvborisenko wants to merge 1 commit into
Conversation
PR Summary by Qodo[dotnet][bidi] Use IAsyncEnumerable for event streams with implicit unsubscribe Description
Diagram
High-Level Assessment
Files changed (9)
|
Code Review by Qodo
Context used✅ Compliance rules (platform):
15 rules 1. IBiDi.StreamAsync return type changed
|
|
I really like this simplification too! It makes things more understandable for consumers by using only a well-known interface and reduces casting by avoiding the The main drawback I see is that if the Another small drawback is that if enumeration is attempted after the first is disposed (i.e. Thanks for exploring it @nvborisenko, I really like it! |
This is the same issue as before. All data, got from remote end, is buffered. This is "intentional". Users might forgot to call
Good call, we should throw with self-explainable exception. Thanks for your review, appreciate earlier feedback. |
| @@ -62,15 +62,17 @@ void ISubscriptionSink.Complete(Exception? error) | |||
|
|
|||
| public IAsyncEnumerator<TEventArgs> GetAsyncEnumerator(CancellationToken cancellationToken = default) | |||
There was a problem hiding this comment.
guad for already disposed
| _transport.EnqueueEvent("script.realmDestroyed", """{"realm":"r-1","foo":"extra"}"""); | ||
|
|
||
| var received = await stream.FirstAsync().AsTask().WaitAsync(TimeSpan.FromSeconds(5)); | ||
| var received = await stream.FirstAsync().AsTask().WithResponse(_transport).WaitAsync(TimeSpan.FromSeconds(5)); |
There was a problem hiding this comment.
double check flakiness
Instead of explicitly unsubscribing on the wire, we can catch the intention implicitly via ending of enumeration.
🔗 Related Issues
Implicitly resolves #17696
💥 What does this PR do?
This pull request refactors the event streaming interface in the BiDi (Bidirectional) WebDriver .NET codebase to simplify usage and align with modern .NET asynchronous patterns. The main change is replacing the custom
IEventStream<TEventArgs>interface with the standardIAsyncEnumerable<TEventArgs>, and updating all related code, implementations, and tests accordingly. Additionally, resource management and cancellation handling are improved for event streams.API and Interface Updates:
Replaced all usages of
IEventStream<TEventArgs>withIAsyncEnumerable<TEventArgs>in public interfaces such asIBiDiandIEventSource<TEventArgs>, and updated all method signatures and implementations to reflect this change. (dotnet/src/webdriver/BiDi/IBiDi.cs,dotnet/src/webdriver/BiDi/IEventSource.cs,dotnet/src/webdriver/BiDi/BiDi.cs,dotnet/src/webdriver/BiDi/ContextEventSource.cs,dotnet/src/webdriver/BiDi/EventSource.cs) [1] [2] [3] [4] [5]Removed the
IEventStream<TEventArgs>interface and migrated its responsibilities toIAsyncEnumerable<TEventArgs>, eliminating the need for a custom event stream interface. (dotnet/src/webdriver/BiDi/IEventStream.cs)Implementation Improvements:
EventStream<TEventArgs>class to implementIAsyncEnumerable<TEventArgs>and improved cancellation token handling by introducing a custom enumerator (UnsubscribingAsyncEnumerator) that ensures proper disposal of linked cancellation tokens and the event stream itself. (dotnet/src/webdriver/BiDi/EventStream.cs) [1] [2] [3]Test Adjustments:
IAsyncEnumerable<TEventArgs>instead ofIEventStream<TEventArgs>, removing unnecessaryawait usingstatements for event streams and adjusting disposal patterns as needed. (dotnet/test/webdriver/BiDi/Network/NetworkTests.cs,dotnet/test/webdriver/BiDi/Session/SessionTests.cs,dotnet/test/webdriver/BiDi/SessionUnitTests.cs) [1] [2] [3] [4] [5] [6] [7]These changes modernize the event streaming API, simplify implementation and usage, and improve resource management for consumers of the BiDi WebDriver .NET library.
🔧 Implementation Notes
Good to simplify user-facing API.
🤖 AI assistance
💡 Additional Considerations
Low level API, amazing simplification.
🔄 Types of changes