Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Showing how `PassthroughSubject` drops value if there isn’t demand.
final class ManualSubscriber: Subscriber {
var subscription: Subscription?
func receive(subscription: Subscription) { self.subscription = subscription }
func receive(_ input: Int) -> Subscribers.Demand { print(input); return .none }
func receive(completion: Subscribers.Completion<Never>) {}
}
let subscriber = ManualSubscriber()
let subject = PassthroughSubject<Int, Never>()
subject
.subscribe(subscriber)
subscriber.subscription?.request(.max(1))
subject.send(1)
subject.send(2)
subscriber.subscription?.request(.max(1))
subject.send(3)
// Outputs:
// ```
// 1
// 3
// ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment