Skip to content

Instantly share code, notes, and snippets.

@brow
Last active August 29, 2015 14:21
Show Gist options
  • Save brow/9e9e3d8f4db0028312ee to your computer and use it in GitHub Desktop.
Save brow/9e9e3d8f4db0028312ee to your computer and use it in GitHub Desktop.
Non-blocking queue in RAC 3
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
}
}
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