Last active
August 29, 2015 14:21
-
-
Save brow/9e9e3d8f4db0028312ee to your computer and use it in GitHub Desktop.
Non-blocking queue in RAC 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ReactiveCocoa | |
public func enqueue<T>(elements: SignalProducer<T, NoError>) -> SignalProducer<T, NoError> { | |
let (poppers, poppersSink) = Signal<SinkOf<Event<T, NoError>>, NoError>.pipe() | |
elements | |
|> zipWith(poppers) | |
|> start(next: { element, popper in | |
sendNext(popper, element) | |
sendCompleted(popper) | |
}) | |
let poppersDisposable = ScopedDisposable(ActionDisposable { | |
sendCompleted(poppersSink) | |
}) | |
return SignalProducer { observer, _ in | |
sendNext(poppersSink, observer) | |
poppersDisposable | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let queue = SignalProducer(values: [1, 2]) |> enqueue | |
// Pop | |
queue.start(next: println) // => 1 | |
// Pop again | |
queue.start(next: println) // => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment