Documentation

Std.Sync.Broadcast

The Std.Sync.Broadcast module implements a broadcasting primitive for sending values to multiple consumers. It maintains a queue of values and supports both synchronous and asynchronous waiting.

This module is heavily inspired by Std.Sync.Channel as well as tokio’s broadcast implementation.

Errors that may be thrown while interacting with the broadcast channel API.

  • closed : Error

    Tried to send to a closed broadcast channel.

  • alreadyClosed : Error

    Tried to close an already closed broadcast channel.

  • notSubscribed : Error

    Tried to unsubscribe a channel that already is not part of it.

Instances For
    @[implicit_reducible]
    @[implicit_reducible]
    structure Std.Broadcast (α : Type) :

    A multi-subscriber broadcast that delivers each message to all current subscribers. Supports only bounded buffering and an asynchronous API; to switch into synchronous mode use Broadcast.sync.

    Unlike Std.Channel, each message is received by every subscriber instead of just one. Subscribers only receive messages sent after they have subscribed (unless otherwise specified).

    Instances For
      structure Std.Broadcast.Receiver (α : Type) :

      A receiver for a Broadcast channel that can asynchronously receive messages. Each receiver gets a copy of every message sent to the broadcast channel after the receiver was created. Multiple receivers can exist for the same broadcast, and each will receive all messages independently.

      Instances For
        @[inline]
        def Std.Broadcast.new {α : Type} (capacity : Nat := 16) (h : capacity > 0 := by decide) :

        Creates a new broadcast channel.

        Instances For
          @[inline]
          def Std.Broadcast.trySend {α : Type} (ch : Broadcast α) (v : α) :

          Try to send a value to the broadcast channel, if this can be completed right away without blocking return true, otherwise don't send the value and return false.

          Instances For
            @[inline]
            def Std.Broadcast.subscribe {α : Type} (ch : Broadcast α) :

            Subscribes a new Receiver from the Broadcast channel.

            Instances For
              @[inline]
              def Std.Broadcast.close {α : Type} (ch : Broadcast α) :

              Closes a Broadcast channel.

              Instances For
                @[inline]
                def Std.Broadcast.send {α : Type} (ch : Broadcast α) (v : α) :

                Send a value through the broadcast channel, returning a task that will resolve once the transmission could be completed.

                Instances For
                  @[inline]

                  Try to receive a value from the broadcast receiver, if a message is available right away return some value, otherwise return none without blocking.

                  Instances For
                    @[inline]

                    Receive a value from the broadcast receiver, returning a task that will resolve with the next available message. This will block until a message is available.

                    Instances For
                      @[inline]

                      Creates a Selector that resolves once the broadcast channel ch has data available and provides that data.

                      Instances For
                        @[inline]

                        Unsubscribes a Receiver from the Broadcast channel.

                        Instances For

                          ch.forAsync f calls f for every message received on ch.

                          Note that if this function is called twice, each message will only arrive at exactly one invocation.

                          Instances For

                            A multi-subscriber broadcast that delivers each message to all current subscribers. Supports only bounded buffering and an asynchronous API.

                            It's the sync version of Broadcast.

                            Instances For

                              A receiver for a Broadcast channel that can asynchronously receive messages. Each receiver gets a copy of every message sent to the broadcast channel after the receiver was created. Multiple receivers can exist for the same broadcast, and each will receive all messages independently.

                              It's the sync version of Broadcast.Receiver.

                              Instances For
                                @[inline]
                                def Std.Broadcast.Sync.new {α : Type} (capacity : Nat := 16) (h : capacity > 0 := by decide) :

                                Creates a new broadcast channel.

                                Instances For
                                  @[inline]
                                  def Std.Broadcast.Sync.trySend {α : Type} (ch : Sync α) (v : α) :

                                  Try to send a value to the broadcast channel, if this can be completed right away without blocking return true, otherwise don't send the value and return false.

                                  Instances For
                                    @[inline]
                                    def Std.Broadcast.Sync.send {α : Type} (ch : Sync α) (v : α) :

                                    Send a value through the channel, blocking until the transmission could be completed.

                                    Instances For
                                      @[inline]

                                      Try to receive a value from the broadcast receiver, if a message is available right away return some value, otherwise return none without blocking.

                                      Instances For

                                        Receive a value from the channel, blocking until the transmission could be completed.

                                        Instances For
                                          partial def Std.Broadcast.Sync.Receiver.forIn {α : Type} {m : TypeType u_1} {β : Type} [Inhabited α] [Monad m] [MonadLiftT BaseIO m] (ch : Receiver α) (f : αβm (ForInStep β)) :
                                          βm β
                                          @[implicit_reducible]

                                          for msg in ch.sync do ... receives all messages in the channel until it is closed.