-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added CoFuture init with promise callback
- added subscribeCoChannel() for Publisher - added publisher() to CoChannel
- Loading branch information
Alex Belozierov
committed
Jun 12, 2020
1 parent
c3da301
commit c680c3d
Showing
61 changed files
with
666 additions
and
72 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
49 changes: 49 additions & 0 deletions
49
Sources/SwiftCoroutine/CoFuture/Operators/Combine/CoChannel/CoChannel+Combine.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,49 @@ | ||
// | ||
// CoChannel+Combine.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 11.06.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
#if canImport(Combine) | ||
import Combine | ||
|
||
@available(OSX 10.15, iOS 13.0, *) | ||
extension CoChannel { | ||
|
||
// MARK: - publisher | ||
|
||
/// Returns a publisher that emits elements of this `CoChannel`. | ||
@inlinable public func publisher() -> AnyPublisher<Element, CoChannelError> { | ||
channel.publisher() | ||
} | ||
|
||
} | ||
|
||
@available(OSX 10.15, iOS 13.0, *) | ||
extension CoChannel.Receiver { | ||
|
||
// MARK: - publisher | ||
|
||
/// Returns a publisher that emits elements of this `Receiver`. | ||
public func publisher() -> AnyPublisher<Element, CoChannelError> { | ||
CoChannelPublisher(receiver: self).eraseToAnyPublisher() | ||
} | ||
|
||
} | ||
|
||
@available(OSX 10.15, iOS 13.0, *) | ||
extension Publisher { | ||
|
||
/// Attaches `CoChannel.Receiver` as a subscriber and returns it. | ||
public func subscribeCoChannel(buffer: CoChannel<Output>.BufferType = .unlimited) -> CoChannel<Output>.Receiver { | ||
let channel = CoChannel<Output>(bufferType: buffer) | ||
let cancellable = sink(receiveCompletion: { _ in channel.close() }, | ||
receiveValue: { channel.offer($0) }) | ||
channel.whenCanceled(cancellable.cancel) | ||
return channel.receiver | ||
} | ||
|
||
} | ||
#endif |
33 changes: 33 additions & 0 deletions
33
Sources/SwiftCoroutine/CoFuture/Operators/Combine/CoChannel/CoChannelPublisher.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,33 @@ | ||
// | ||
// CoChannelPublisher.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 11.06.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
#if canImport(Combine) | ||
import Combine | ||
|
||
@available(OSX 10.15, iOS 13.0, *) | ||
internal final class CoChannelPublisher<Output> { | ||
|
||
internal typealias Failure = CoChannelError | ||
internal let receiver: CoChannel<Output>.Receiver | ||
|
||
@inlinable internal init(receiver: CoChannel<Output>.Receiver) { | ||
self.receiver = receiver | ||
} | ||
|
||
} | ||
|
||
@available(OSX 10.15, iOS 13.0, *) | ||
extension CoChannelPublisher: Publisher { | ||
|
||
@inlinable internal func receive<S: Subscriber>(subscriber: S) where Failure == S.Failure, Output == S.Input { | ||
let subscription = CoChannelSubscription(subscriber: subscriber, receiver: receiver) | ||
subscriber.receive(subscription: subscription) | ||
} | ||
|
||
} | ||
#endif |
45 changes: 45 additions & 0 deletions
45
Sources/SwiftCoroutine/CoFuture/Operators/Combine/CoChannel/CoChannelSubscription.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,45 @@ | ||
// | ||
// CoChannelSubscription.swift | ||
// SwiftCoroutine | ||
// | ||
// Created by Alex Belozierov on 11.06.2020. | ||
// Copyright © 2020 Alex Belozierov. All rights reserved. | ||
// | ||
|
||
#if canImport(Combine) | ||
import Combine | ||
|
||
@available(OSX 10.15, iOS 13.0, *) | ||
internal final class CoChannelSubscription<S: Subscriber, T>: Subscription where S.Input == T, S.Failure == CoChannelError { | ||
|
||
private let receiver: CoChannel<T>.Receiver | ||
private var subscriber: S? | ||
|
||
@inlinable internal init(subscriber: S, receiver: CoChannel<T>.Receiver) { | ||
self.receiver = receiver | ||
self.subscriber = subscriber | ||
@inline(__always) func subscribe() { | ||
receiver.whenReceive { result in | ||
guard let subscriber = self.subscriber else { return } | ||
switch result { | ||
case .success(let result): | ||
_ = subscriber.receive(result) | ||
subscribe() | ||
case .failure(let error) where error == .canceled: | ||
subscriber.receive(completion: .failure(error)) | ||
case .failure: | ||
subscriber.receive(completion: .finished) | ||
} | ||
} | ||
} | ||
subscribe() | ||
} | ||
|
||
@inlinable internal func cancel() { | ||
subscriber = nil | ||
} | ||
|
||
@inlinable internal func request(_ demand: Subscribers.Demand) {} | ||
|
||
} | ||
#endif |
File renamed without changes.
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
Oops, something went wrong.