3. Multicast

3.1. Overview

The multicast service allows you to easily broadcast messages to a specified set of objects. It is, in essence, a kind of observer pattern, with t he observers being given to the multicaster when it is created. Events are then sent to the observers by invoking methods on the multicaster.

3.2. Usage

The multicast service is parameterized. Just send it a list of objects (i.e., other services) that you want multicasted to, and it will return a new multicaster object.

  reg = Needle::Registry.define do |b|
    b.require 'needle/extras/multicast', 'Needle::Extras::Multicast'

    b.foo { "hello" }
    b.bar { [ 1, 2, 3 ] }
    b.baz { "test" }

    b.multicaster { |c,| c.multicast c.foo, c.bar, c.baz }
  end

Once you’ve registered your service, you can send messages to the observing services by sending messages to the multicaster:

  m = reg.multicaster
  p m.length #-> [ 5, 3, 4 ]

The multicaster will return an array of the return values of all of the observing services. Thus, in the above example, an array of the lengths of each of the foo, bar, and baz services is returned.