-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Collect. Use ReduceProducer.
- Loading branch information
1 parent
777d91e
commit 22545f0
Showing
2 changed files
with
78 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Publishers.Collect.swift | ||
// | ||
// | ||
// Created by Sergej Jaskiewicz on 09.10.2019. | ||
// | ||
|
||
extension Publisher { | ||
|
||
/// Collects all received elements, and emits a single array of the collection when | ||
/// the upstream publisher finishes. | ||
/// | ||
/// If the upstream publisher fails with an error, this publisher forwards the error | ||
/// to the downstream receiver instead of sending its output. | ||
/// This publisher requests an unlimited number of elements from the upstream | ||
/// publisher. It only sends the collected array to its downstream after a request | ||
/// whose demand is greater than 0 items. | ||
/// Note: This publisher uses an unbounded amount of memory to store the received | ||
/// values. | ||
/// | ||
/// - Returns: A publisher that collects all received items and returns them as | ||
/// an array upon completion. | ||
public func collect() -> Publishers.Collect<Self> { | ||
return .init(upstream: self) | ||
} | ||
} | ||
|
||
extension Publishers { | ||
|
||
/// A publisher that buffers items. | ||
public struct Collect<Upstream: Publisher>: Publisher { | ||
|
||
public typealias Output = [Upstream.Output] | ||
|
||
public typealias Failure = Upstream.Failure | ||
|
||
/// The publisher that this publisher receives elements from. | ||
public let upstream: Upstream | ||
|
||
public init(upstream: Upstream) { | ||
self.upstream = upstream | ||
} | ||
|
||
public func receive<Downstream: Subscriber>(subscriber: Downstream) | ||
where Upstream.Failure == Downstream.Failure, | ||
Downstream.Input == [Upstream.Output] | ||
{ | ||
upstream.subscribe(Inner(downstream: subscriber)) | ||
} | ||
} | ||
} | ||
|
||
extension Publishers.Collect { | ||
private class Inner<Downstream: Subscriber> | ||
: ReduceProducer<Downstream, | ||
Upstream.Output, | ||
[Upstream.Output], | ||
Upstream.Failure, | ||
Void> | ||
where Downstream.Input == [Upstream.Output], | ||
Downstream.Failure == Upstream.Failure | ||
{ | ||
fileprivate init(downstream: Downstream) { | ||
super.init(downstream: downstream, initial: [], reduce: ()) | ||
} | ||
|
||
override func receive( | ||
newValue: Upstream.Output | ||
) -> PartialCompletion<Void, Downstream.Failure> { | ||
result!.append(newValue) | ||
return .continue | ||
} | ||
|
||
override var description: String { | ||
return "Collect" | ||
} | ||
} | ||
} |