-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
asDisposable
convenience property to Task
Offers a convenient way to return Tasks as a `Disposable` instead of having to wrap it with `AnonymousDisposable`, for example in an `EffectHandler`
- Loading branch information
Louis Debaere
committed
Feb 12, 2024
1 parent
c7cee61
commit 6026c24
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
public extension Task { | ||
|
||
var asDisposable: any Disposable { | ||
AnonymousDisposable { cancel() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import MobiusCore | ||
import Nimble | ||
import Quick | ||
|
||
@available(iOS 13.0, *) | ||
class TaskDisposableTests: QuickSpec { | ||
override func spec() { | ||
describe("Task+Disposable") { | ||
var task: Task<Void, any Error>! | ||
var disposable: Disposable! | ||
|
||
beforeEach { | ||
task = Task { | ||
try? await Task.sleep(nanoseconds: 1_000_000_000) | ||
} | ||
disposable = task.asDisposable | ||
} | ||
|
||
it("starts off not cancelled") { | ||
expect(task.isCancelled).to(beFalse()) | ||
} | ||
|
||
it("disposable cancels the task that owns it") { | ||
disposable.dispose() | ||
expect(task.isCancelled).to(beTrue()) | ||
} | ||
} | ||
} | ||
} |