Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(1...2).publisher.share() with `print`s.
let upstream = (1...2)
.publisher
.print("\tPre-share") //
.share()
.print("Post-share") //
let cancellable = upstream
.first()
.flatMap { first in
upstream
.prepend(first, first)
}
.pairwise()
.sink(receiveValue: { _ in }) // Removing the `print` call to
// focus in on the `Publisher.print`s, above.
// Outputs (without the annotations):
// ```
// Post-share: receive subscription: (Multicast) ⇒ Start of the `first` subscription.
// Post-share: request unlimited
// Pre-share: receive subscription: (1...2)
// Pre-share: request unlimited
// Pre-share: receive value: (1)
// Post-share: receive value: (1)
// Post-share: receive cancel
// Post-share: receive subscription: (Multicast) ⇒ The `flatMap`’d subscription.
// Post-share: request unlimited
// Pre-share: receive subscription: (1...2)
// Pre-share: request unlimited
// Pre-share: receive value: (1)
// Post-share: receive value: (1)
// Pre-share: receive value: (2)
// Post-share: receive value: (2)
// Pre-share: receive finished
// Post-share: receive finished ⇒ End of the `flatMap`’d subscription.
// Pre-share: receive value: (2) ⇒ The lingering `2` from the `first` subscription.
// Pre-share: receive finished ⇒ Finished event from the `first` subscription.
// ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment