Skip to content

Instantly share code, notes, and snippets.

@felixvisee
Created June 22, 2019 10:41
Show Gist options
  • Save felixvisee/31c26c4e7837746d8cea5bb007de9100 to your computer and use it in GitHub Desktop.
Save felixvisee/31c26c4e7837746d8cea5bb007de9100 to your computer and use it in GitHub Desktop.
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import Combine
import Dispatch
import Foundation
(1...10)
.publisher()
.handleEvents(receiveOutput: { print("publisher sent \($0)") })
.collect(2)
.handleEvents(receiveOutput: { print("downstream collected \($0)") })
.flatMap(maxPublishers: .max(1)) { values in
return Publishers.Just(values)
.delay(for: .seconds(1), scheduler: DispatchQueue.main)
.handleEvents(receiveOutput: { print("downstream delayed \($0)") })
}
.sink(receiveCompletion: { _ in PlaygroundPage.current.finishExecution() }) {
print("sink received \($0)")
}
@felixvisee
Copy link
Author

Output:

publisher sent 1
publisher sent 2
downstream collected [1, 2]
downstream delayed [1, 2]
sink received [1, 2]
publisher sent 3
publisher sent 4
downstream collected [3, 4]
downstream delayed [3, 4]
sink received [3, 4]
publisher sent 5
publisher sent 6
downstream collected [5, 6]
downstream delayed [5, 6]
sink received [5, 6]
publisher sent 7
publisher sent 8
downstream collected [7, 8]
downstream delayed [7, 8]
sink received [7, 8]
publisher sent 9
publisher sent 10
downstream collected [9, 10]
downstream delayed [9, 10]
sink received [9, 10]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment