-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add SignalProducer+Concurrency (#64)
# Conflicts: # CHANGELOG.md
- Loading branch information
Showing
3 changed files
with
45 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
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
39 changes: 39 additions & 0 deletions
39
Sources/ACKReactiveExtensions/Core/SignalProducer+Concurrency.swift
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,39 @@ | ||
import ReactiveSwift | ||
|
||
@available(iOS 13.0, *) | ||
public extension SignalProducer { | ||
/// Run async operation as SignalProducer | ||
/// - Parameter operation: Operation to be run | ||
init(operation: @escaping () async -> Result<Value, Error>) { | ||
self.init { observer, lifetime in | ||
let task = Task { | ||
switch await operation() { | ||
case .success(let value): | ||
observer.send(value: value) | ||
observer.sendCompleted() | ||
case .failure(let error): | ||
observer.send(error: error) | ||
} | ||
} | ||
|
||
lifetime.observeEnded { | ||
task.cancel() | ||
} | ||
} | ||
} | ||
|
||
/// Run async operation as SignalProducer | ||
/// - Parameter operation: Operation to be run | ||
init(operation: @escaping () async -> Value) where Error == Never { | ||
self.init { observer, lifetime in | ||
let task = Task { | ||
observer.send(value: await operation()) | ||
observer.sendCompleted() | ||
} | ||
|
||
lifetime.observeEnded { | ||
task.cancel() | ||
} | ||
} | ||
} | ||
} |