Skip to content

Instantly share code, notes, and snippets.

@jasdev
Created February 11, 2021 16:45
Show Gist options
  • Save jasdev/3657ba19d3fb5617efb344f6eda6c5ff to your computer and use it in GitHub Desktop.
Save jasdev/3657ba19d3fb5617efb344f6eda6c5ff to your computer and use it in GitHub Desktop.
(1...2).publisher.share() with `print`s and a `delay` to notice race condition.
let upstream = (1...2)
.publisher
.print("\tPre-share")
.share()
.print("Post-share")
let cancellable = upstream
.first()
.delay(for: 1, scheduler: DispatchQueue.main) // ⇐
.flatMap { first in
upstream
.prepend(first, first)
}
.pairwise()
.sink(receiveValue: { _ in })
// Outputs (without the annotations):
// ```
// Post-share: receive subscription: (Multicast)
// 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
// Pre-share: receive value: (2)
// Pre-share: receive finished
// Post-share: receive subscription: (Multicast)
// Post-share: request unlimited
// Post-share: receive finished ⇒ Downstream missed the `2` and instead only gets the completion replayed.
// ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment