forked from hyperdevs-team/mini-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
488b802
commit 9300e78
Showing
8 changed files
with
147 additions
and
3 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
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,54 @@ | ||
import Combine | ||
|
||
public extension Publisher { | ||
func removeExpired() -> Publishers.RemoveExpired<Self> | ||
where Output: Taskable { | ||
Publishers.RemoveExpired(upstream: self) | ||
} | ||
} | ||
|
||
public extension Publishers { | ||
/// Create a `Publisher` that connect an Upstream (Another publisher) that type erases `Task`s to `EmptyTask` | ||
/// The Output of this `Publisher` always is a combined `EmptyTask` | ||
struct RemoveExpired<Upstream: Publisher>: Publisher where Upstream.Output: Taskable { | ||
public typealias Output = Upstream.Output | ||
public typealias Failure = Upstream.Failure | ||
|
||
public let upstream: Upstream | ||
|
||
public init(upstream: Upstream) { | ||
self.upstream = upstream | ||
} | ||
|
||
public func receive<S: Subscriber>(subscriber: S) where Upstream.Failure == S.Failure, Output == S.Input { | ||
upstream.subscribe(Inner(downstream: subscriber)) | ||
} | ||
} | ||
} | ||
|
||
extension Publishers.RemoveExpired { | ||
private struct Inner<Downstream: Subscriber>: Subscriber | ||
where Downstream.Input == Output, Downstream.Failure == Upstream.Failure, Output: Taskable { | ||
let combineIdentifier = CombineIdentifier() | ||
private let downstream: Downstream | ||
|
||
fileprivate init(downstream: Downstream) { | ||
self.downstream = downstream | ||
} | ||
|
||
func receive(subscription: Subscription) { | ||
downstream.receive(subscription: subscription) | ||
} | ||
|
||
func receive(_ input: Upstream.Output) -> Subscribers.Demand { | ||
if input.isExpired { | ||
return .none | ||
} | ||
return downstream.receive(input) | ||
} | ||
|
||
func receive(completion: Subscribers.Completion<Upstream.Failure>) { | ||
downstream.receive(completion: completion) | ||
} | ||
} | ||
} |
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
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
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