Skip to content

Instantly share code, notes, and snippets.

@jasdev
Created February 7, 2021 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/8c3f8e80cfc8dfa73121b2da65a96643 to your computer and use it in GitHub Desktop.
Save jasdev/8c3f8e80cfc8dfa73121b2da65a96643 to your computer and use it in GitHub Desktop.
(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