Skip to content

Instantly share code, notes, and snippets.

@alexShepard84
Created March 29, 2023 07:26
Show Gist options
  • Save alexShepard84/4a7ed0c93b03ed775f0287ebc9e78da3 to your computer and use it in GitHub Desktop.
Save alexShepard84/4a7ed0c93b03ed775f0287ebc9e78da3 to your computer and use it in GitHub Desktop.
`SingleSubscriberSubject` is a publisher that only supports a single subscriber at a time
// Copyright (c) 2023 Alex Schäfer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Combine
/// When a new subscriber is added, the previous subscriber is cancelled and the last value is set to nil.
/// When sending a new value, only the current subscriber will receive the value.
final class SingleSubscriberSubject<Output, Failure>: Publisher where Failure: Error {
typealias Output = Output
typealias Failure = Failure
private var lastValue: Output?
private var lastSubscriber: AnySubscriber<Output, Failure>?
func receive<S>(subscriber: S) where S: Subscriber, Failure == S.Failure, Output == S.Input {
if lastSubscriber != nil {
lastValue = nil
}
lastSubscriber = AnySubscriber(subscriber)
let subscription = Subscription(
subscriber: subscriber,
lastValue: lastValue
) { [weak self] in
self?.lastSubscriber = nil
self?.lastValue = nil
}
subscriber.receive(subscription: subscription)
}
func send(_ value: Output?) {
lastValue = value
guard
let lastSubscriber = lastSubscriber,
let value = value
else {
return
}
_ = lastSubscriber.receive(value)
}
}
private extension SingleSubscriberSubject {
final class Subscription<S: Subscriber> {
private var subscriber: S?
private var lastValue: S.Input?
private let onCancel: () -> Void
init(subscriber: S, lastValue: S.Input?, onCancel: @escaping () -> Void) {
self.subscriber = subscriber
self.lastValue = lastValue
self.onCancel = onCancel
}
}
}
extension SingleSubscriberSubject.Subscription: Cancellable {
func cancel() {
subscriber = nil
onCancel()
}
}
extension SingleSubscriberSubject.Subscription: Subscription {
func request(_ demand: Subscribers.Demand) {
guard let subscriber = subscriber, demand > 0 else { return }
if let lastValue = lastValue {
_ = subscriber.receive(lastValue)
self.lastValue = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment