class EventdObject

EventdObject

Implements an event-based architecture centered around the concept of channels and listeners. Each channel can contain multiple listeners. An “emission” can be sent on a channel, which will be then sent to all corresponding listeners on that channel. Emissions can have optional data sent with them to the listeners.

Shot Framework - Copyright © Jesse Aaron Dunlap <me@jessedunlap.me> Licensed under the MIT License. For full licensing information, please see LICENSE.md. github.com/JesseDunlap/shot/

Attributes

events[RW]

Public Class Methods

new() click to toggle source

Initialize the EventdObject and set up the events hash.

Note

It is important that you always call +super()+ so that the events hash can be initialized

# File lib/eventd/eventd_object.rb, line 18
def initialize
        @events = {}
end

Public Instance Methods

emit(channel, data = nil) click to toggle source

Send an emission on a channel

Attributes

  • channel - Channel to emit on

  • data - Optional. Data to send along with the emission.

# File lib/eventd/eventd_object.rb, line 48
def emit(channel, data = nil)
        emit 'emission', { :channel => channel, :data => data } unless channel == 'emission'

        @events[channel] = (@events[channel] or [])

        @events[channel].each do |callback|
                if data == nil then callback.call else callback.call data end
        end
end
off(channel) click to toggle source

Remove all event listeners from a specified channel

Attributes

  • channel - Channel to unsubscribe from

# File lib/eventd/eventd_object.rb, line 38
def off(channel)
        @events[channel] = []
end
on(channel, &callback) click to toggle source

Add an event listener to a specified channel

Attributes

  • channel - Channel to subscribe to

  • callback - Callback block, which is triggered when an emission occurs on the channel

# File lib/eventd/eventd_object.rb, line 28
def on(channel, &callback)
        @events[channel] = (@events[channel] or [])
        @events[channel].push callback
end