-
Notifications
You must be signed in to change notification settings - Fork 0
Add Publishers.AgainAt #29
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
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,344 @@ | ||
| import Combine | ||
| import Foundation | ||
| import Synchronized | ||
|
|
||
| public extension Publisher { | ||
| func againAt<Context: Scheduler>( | ||
| scheduler: Context, | ||
| options: Context.SchedulerOptions? = nil | ||
| ) -> Publishers.AgainAt<Self, Context> { | ||
| Publishers.AgainAt(upstream: self, scheduler: scheduler, options: options) | ||
| } | ||
| } | ||
|
|
||
| public extension Publishers { | ||
| struct AgainAt<Upstream: Publisher, Context: Scheduler>: Publisher { | ||
| public typealias Output = (Upstream.Output, Timer) | ||
| public typealias Failure = Upstream.Failure | ||
|
|
||
| private let upstream: Upstream | ||
| private let scheduler: Context | ||
| private let options: Context.SchedulerOptions? | ||
|
|
||
| public init( | ||
| upstream: Upstream, | ||
| scheduler: Context, | ||
| options: Context.SchedulerOptions? | ||
| ) { | ||
| self.upstream = upstream | ||
| self.scheduler = scheduler | ||
| self.options = options | ||
| } | ||
|
|
||
| public func receive<S: Subscriber>( | ||
| subscriber: S | ||
| ) where S.Input == Output, S.Failure == Failure { | ||
| let subscription = AgainAtSubscription<Upstream, Context, S>( | ||
| scheduler: scheduler, | ||
| options: options, | ||
| subscriber: subscriber | ||
| ) | ||
|
|
||
| upstream | ||
| .receive(on: scheduler, options: options) | ||
| .subscribe(subscription) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public extension Publishers.AgainAt { | ||
| final class Timer: @unchecked Sendable { | ||
| public let now: Context.SchedulerTimeType | ||
|
|
||
| private let onRepublishAt: @Sendable (Context.SchedulerTimeType) -> Void | ||
|
|
||
| fileprivate init( | ||
| now: Context.SchedulerTimeType, | ||
| onRepublishAt: @escaping @Sendable (Context.SchedulerTimeType) -> Void | ||
| ) { | ||
| self.now = now | ||
| self.onRepublishAt = onRepublishAt | ||
| } | ||
|
|
||
| public func republish(at time: Context.SchedulerTimeType) { | ||
| onRepublishAt(time) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private final class AgainAtSubscription<Upstream, Context, Downstream>: | ||
| Subscription, | ||
| Subscriber, | ||
| @unchecked Sendable | ||
| where | ||
| Upstream: Publisher, | ||
| Context: Scheduler, | ||
| Downstream: Subscriber, | ||
| Downstream.Input == Publishers.AgainAt<Upstream, Context>.Output, | ||
| Downstream.Failure == Upstream.Failure | ||
| { | ||
| typealias Input = Upstream.Output | ||
| typealias Failure = Upstream.Failure | ||
|
|
||
| private final class RepublishToken {} | ||
|
|
||
| private struct State { | ||
| var downstream: Downstream? | ||
| var upstream: Subscription? | ||
| var demand: Subscribers.Demand = .none | ||
|
|
||
| var latestOutput: Upstream.Output? | ||
| var pendingOutput: Upstream.Output? | ||
|
|
||
| var activeRepublish: RepublishToken? | ||
| var pendingCompletion: Subscribers.Completion<Failure>? | ||
|
|
||
| var isDraining = false | ||
| var isDrainScheduled = false | ||
| } | ||
|
|
||
| private enum DrainAction { | ||
| case completion(Downstream, Subscribers.Completion<Failure>) | ||
| case stop | ||
| case value(Downstream, Upstream.Output) | ||
| } | ||
|
|
||
| private let scheduler: Context | ||
| private let options: Context.SchedulerOptions? | ||
| private let state: Locked<State> | ||
|
|
||
| init( | ||
| scheduler: Context, | ||
| options: Context.SchedulerOptions?, | ||
| subscriber: Downstream | ||
| ) { | ||
| self.scheduler = scheduler | ||
| self.options = options | ||
| state = Locked(State(downstream: subscriber)) | ||
| } | ||
|
|
||
| deinit { | ||
| cancel() | ||
| } | ||
|
|
||
| func receive(subscription: Subscription) { | ||
| let downstream = state.access { state -> Downstream? in | ||
| guard state.downstream != nil, | ||
| state.upstream == nil | ||
| else { return nil } | ||
| state.upstream = subscription | ||
| return state.downstream | ||
| } | ||
|
|
||
| guard let downstream else { | ||
| subscription.cancel() | ||
| return | ||
| } | ||
|
|
||
| downstream.receive(subscription: self) | ||
|
|
||
| let shouldRequest = state.access { state in | ||
| state.downstream != nil && state.upstream != nil | ||
| } | ||
|
|
||
| if shouldRequest { | ||
| subscription.request(.unlimited) | ||
| } | ||
| } | ||
|
|
||
| func request(_ demand: Subscribers.Demand) { | ||
| guard demand > .none else { return } | ||
|
|
||
| state.access { state in | ||
| guard state.downstream != nil else { return } | ||
| state.demand += demand | ||
| } | ||
|
|
||
| scheduleDrain() | ||
| } | ||
|
|
||
| func cancel() { | ||
| let upstream = state.access { state -> Subscription? in | ||
| let upstream = state.upstream | ||
|
|
||
| state.downstream = nil | ||
| state.upstream = nil | ||
| state.latestOutput = nil | ||
| state.pendingOutput = nil | ||
| state.activeRepublish = nil | ||
| state.pendingCompletion = nil | ||
| state.isDraining = false | ||
| state.isDrainScheduled = false | ||
|
|
||
| return upstream | ||
| } | ||
|
|
||
| upstream?.cancel() | ||
| } | ||
|
|
||
| func receive(_ input: Upstream.Output) -> Subscribers.Demand { | ||
| state.access { state in | ||
| guard state.downstream != nil, state.pendingCompletion == nil else { | ||
| return | ||
| } | ||
|
|
||
| state.latestOutput = input | ||
| state.pendingOutput = input | ||
| } | ||
|
|
||
| drain() | ||
| return .none | ||
| } | ||
|
|
||
| func receive(completion: Subscribers.Completion<Failure>) { | ||
| state.access { state in | ||
| guard state.downstream != nil, state.pendingCompletion == nil else { | ||
| return | ||
| } | ||
|
|
||
| state.pendingCompletion = completion | ||
| state.activeRepublish = nil | ||
| state.latestOutput = nil | ||
| } | ||
|
|
||
| drain() | ||
| } | ||
|
|
||
| private func republish(at time: Context.SchedulerTimeType) { | ||
| let token = RepublishToken() | ||
|
|
||
| let shouldSchedule = state.access { state in | ||
| guard | ||
| state.downstream != nil, | ||
| state.pendingCompletion == nil, | ||
| state.latestOutput != nil | ||
| else { | ||
| return false | ||
| } | ||
|
|
||
| state.activeRepublish = token | ||
| return true | ||
| } | ||
|
|
||
| guard shouldSchedule else { return } | ||
|
|
||
| scheduler.schedule( | ||
| after: time, | ||
| tolerance: scheduler.minimumTolerance, | ||
| options: options | ||
| ) { [weak self] in | ||
| self?.fire(token) | ||
| } | ||
| } | ||
|
|
||
| private func fire(_ token: RepublishToken) { | ||
| state.access { state in | ||
| guard state.downstream != nil, | ||
| state.pendingCompletion == nil, | ||
| state.activeRepublish === token, | ||
| let latestOutput = state.latestOutput | ||
| else { return } | ||
|
|
||
| state.activeRepublish = nil | ||
| state.pendingOutput = latestOutput | ||
| } | ||
|
|
||
| drain() | ||
| } | ||
|
|
||
| private func drain() { | ||
| let shouldDrain = state.access { state in | ||
| state.isDrainScheduled = false | ||
|
|
||
| guard state.downstream != nil, | ||
| !state.isDraining, | ||
| hasWork(state) | ||
| else { return false } | ||
|
|
||
| state.isDraining = true | ||
| return true | ||
| } | ||
|
|
||
| guard shouldDrain else { return } | ||
|
|
||
| while true { | ||
| let action = state.access { state -> DrainAction in | ||
| guard let downstream = state.downstream else { | ||
| state.isDraining = false | ||
| return .stop | ||
| } | ||
|
|
||
| if let output = state.pendingOutput, state.demand > .none { | ||
| state.pendingOutput = nil | ||
| state.demand -= .max(1) | ||
| return .value(downstream, output) | ||
| } | ||
|
|
||
| if let completion = state.pendingCompletion { | ||
| state.downstream = nil | ||
| state.upstream = nil | ||
| state.latestOutput = nil | ||
| state.pendingOutput = nil | ||
| state.activeRepublish = nil | ||
| state.isDraining = false | ||
| state.isDrainScheduled = false | ||
| return .completion(downstream, completion) | ||
| } | ||
|
|
||
| state.isDraining = false | ||
| return .stop | ||
| } | ||
|
|
||
| switch action { | ||
| case let .completion(downstream, completion): | ||
| downstream.receive(completion: completion) | ||
| return | ||
|
|
||
| case .stop: | ||
| return | ||
|
|
||
| case let .value(downstream, output): | ||
| let timer = Publishers.AgainAt<Upstream, Context>.Timer( | ||
| now: scheduler.now, | ||
| onRepublishAt: { [weak self] time in | ||
| self?.republish(at: time) | ||
| } | ||
| ) | ||
|
|
||
| let newDemand = downstream.receive((output, timer)) | ||
|
|
||
| if newDemand > .none { | ||
| state.access { state in | ||
| if state.downstream != nil { | ||
| state.demand += newDemand | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func hasWork(_ state: State) -> Bool { | ||
| (state.pendingOutput != nil && state.demand > .none) | ||
| || state.pendingCompletion != nil | ||
| } | ||
|
|
||
| private func scheduleDrain() { | ||
| let shouldSchedule = state.access { state in | ||
| guard state.downstream != nil, | ||
| !state.isDraining, | ||
| !state.isDrainScheduled, | ||
| hasWork(state) | ||
| else { return false } | ||
|
|
||
| state.isDrainScheduled = true | ||
| return true | ||
| } | ||
|
|
||
| if shouldSchedule { | ||
| scheduler.schedule(options: options) { [weak self] in | ||
| self?.drain() | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.