Make Disposable.Create produce idempotent disposables - #95
Conversation
|
@copilot let's mention the idempotency in remarks in xmldocs |
There was a problem hiding this comment.
Pull request overview
This PR changes Disposable.Create to return an idempotent IDisposable, ensuring the provided delegate is invoked at most once even if Dispose() is called multiple times (including from multiple threads).
Changes:
- Updated the internal
DelegateDisposableto gate invocation with anInterlockedflag so disposal happens exactly once. - Added a new unit test verifying that repeated
Dispose()calls only invoke the action once.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| PowerKit/Disposable.cs | Makes Disposable.Create-backed disposables idempotent via an Interlocked-guarded DelegateDisposable. |
| PowerKit.Tests/DisposableTests.cs | Adds a regression test asserting multiple Dispose() calls only trigger the action once. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Updated in |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## prime #95 +/- ##
=======================================
Coverage 86.51% 86.52%
=======================================
Files 78 78
Lines 1409 1410 +1
Branches 242 243 +1
=======================================
+ Hits 1219 1220 +1
Misses 137 137
Partials 53 53 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Disposable.Createpreviously returned a disposable that invoked the delegate on everyDispose()call. The produced disposable should only invoke the action once, regardless of how many times it is disposed.Changes
DelegateDisposable: usesInterlocked.Exchangeon anintflag to ensure the delegate fires exactly once, with thread-safety guaranteesDisposableTests: addsCreate_Idempotent_Testasserting that repeatedDispose()calls invoke the action only once