Import std.concurrent.channels
Selector
class
Public Class Selector
Waits on several Channels at once: Wait takes a value from the first of them, in the order On gave them, that has one, and gives it to that Channel's handler, or runs the timeout's handler once its time passes with none. It's a builder: Selector().On(orders, Take).On(quit, Stop).Timeout(1.Seconds, Idle).Wait(). What it waits for blocks its thread, as a Channel's Receive does.
Examples
Const words = Channel<String>(4)
Const numbers = Channel<Int>(4)
words.Send("hello")
Local seen: String[] = []
Local onWord = Function(word: String)
seen.Add(word)
End Function
Local onNumber = Function(number: Int)
seen.Add("{number}")
End Function
Local selector = Selector().On(words, onWord).On(numbers, onNumber)
Expect(selector.Wait())
Expect(seen == ["hello"])std/concurrent/channels.fb, line 100
-
A new Selector, with no cases and no timeout.
-
Waits on
channeltoo, whose values go tohandler. -
Runs
handlerwhen time passes in a Wait with no value. A Selector has one timeout, which this replaces. -
Waits until a Channel has a value, which goes to its handler, or the timeout passes, which runs its handler, and gives True; or until every Channel is closed and empty, as none can have a value, and gives False.
-
Waits as Wait does, unless the cancellation is cancelled first, or is already, which throws a CancelledError and takes no value.
Report an issue with this page or with FutureBlitz on GitHub.