Skip to content
FutureBlitz

    Install

    Import std.concurrent.channels

    Selector

    std.concurrent.channels

    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

    • Public New() constructor #

      A new Selector, with no cases and no timeout.

    • Public Method On<T: Sendable>(channel: Channel<T>, handler: Func(T)): Selector method #

      Waits on channel too, whose values go to handler.

    • Public Method Timeout(time: Duration, handler: Func()): Selector method #

      Runs handler when time passes in a Wait with no value. A Selector has one timeout, which this replaces.

    • Public Method Wait(): Bool method #

      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.

    • Public Method Wait(cancel: Cancellation): Bool Throws CancelledError method #

      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.