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

Add asDisposable convenience property to Task #205

Merged
merged 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MobiusCore/Source/Disposables/Task+Disposable.swift
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, *)
louisdebaere marked this conversation as resolved.
Show resolved Hide resolved
public extension Task {

var asDisposable: any Disposable {
zvonicek marked this conversation as resolved.
Show resolved Hide resolved
louisdebaere marked this conversation as resolved.
Show resolved Hide resolved
AnonymousDisposable { cancel() }
}
}
29 changes: 29 additions & 0 deletions MobiusCore/Test/Task+DisposableTests.swift
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())
}
}
}
}
Loading