module EF::Object

provides methods for publish–subscribe pattern

Public Class Methods

extended(base) click to toggle source

allows you to extend any object

obj = Object.new
obj.extend EF::Object
# File lib/event-framework.rb, line 133
def self.extended(base)
  base.instance_variable_set :@mutex, Mutex.new
  base.instance_variable_set :@thread, Thread.current
  base.instance_variable_set :@observers, []
  base.instance_variable_set :@observables, []
end
included(base) click to toggle source

patches initialize to admix needed variables

# File lib/event-framework.rb, line 106
def self.included(base)
  return if base.respond_to? :ef

  def base.ef; end

  base.class_exec do
    alias alias_initialize initialize

    ##
    # defines needed variables and calls original initialize
    def initialize(*args, &block)
      alias_initialize *args, &block

      @mutex = Mutex.new

      @thread = Thread.current

      @observers = []
      @observables = []
    end
  end
end
new(*args, &block) click to toggle source

defines needed variables and calls original initialize

# File lib/event-framework.rb, line 116
def initialize(*args, &block)
  alias_initialize *args, &block

  @mutex = Mutex.new

  @thread = Thread.current

  @observers = []
  @observables = []
end

Public Instance Methods

listen_to(observable, event, &block) click to toggle source

registrate a handler for the event

# File lib/event-framework.rb, line 170
def listen_to(observable, event, &block)
  raise 'observable is not EF::Object' unless observable.is_a? Object
  raise 'event is not string' unless event.is_a? String
  raise 'block not given' unless block_given?

  @mutex.synchronize do
    @observables << [observable, event, block]

    if self == observable
      @observers << [self, event, block]
    else
      observable.registrate(self, event, block)
    end
  end
end
move_to(thread) click to toggle source

by default handlers will be executed in the thread where the receiver was defined
the method changes it so that handlers will be executed in the passed thread

# File lib/event-framework.rb, line 228
def move_to(thread)
  @mutex.synchronize do
    @thread = thread
  end
end
off(event=nil, block=nil) click to toggle source

stop listening to self

# File lib/event-framework.rb, line 221
def off(event=nil, block=nil)
  stop_listening self, event, block
end
on(event, &block) click to toggle source

listen to self

# File lib/event-framework.rb, line 188
def on(event, &block)
  raise 'event is not string' unless event.is_a? String
  raise 'block not given' unless block_given?

  listen_to self, event, &block
end
stop_listening(observable=nil, event=nil, block=nil) click to toggle source

unregistrate all matching handlers

# File lib/event-framework.rb, line 197
def stop_listening(observable=nil, event=nil, block=nil)
  @mutex.synchronize do
    observables = []

    @observables.each do |o, e, b|
      if (!observable || o == observable) && (!event || e == event) && (!block || b == block)
        if self == o
          @observers.reject! do |o, e, b|
            o == self
          end
        else
          o.unregistrate(self, e, b)
        end
      else
        observables << [o, e, b]
      end
    end

    @observables = observables
  end
end
thread() click to toggle source

returns the thread where the object was defined

# File lib/event-framework.rb, line 142
def thread
  @thread
end
trigger(event, *args) click to toggle source

calls handlers for observers for the event
parameters and the caller will be passed to the handlers
notice: usually in threads of sibscribers

# File lib/event-framework.rb, line 150
def trigger(event, *args)
  raise 'event is not string' unless event.is_a? String

  @mutex.synchronize do
    @observers.each do |o, e, b|
      if e == event
        if Thread.instances.include? o.thread
          o.thread.add self, *args, &b
        elsif Loop.thread
          Loop.thread.add self, *args, &b
        else
          b.call self, *args
        end
      end
    end
  end
end

Protected Instance Methods

registrate(observer, event, block) click to toggle source

adds observer to the list of observers

# File lib/event-framework.rb, line 238
def registrate(observer, event, block)
  @mutex.synchronize do
    @observers << [observer, event, block]
  end
end
unregistrate(observer, event, block) click to toggle source

removes observer from the list of observers

# File lib/event-framework.rb, line 246
def unregistrate(observer, event, block)
  @mutex.synchronize do
    @observers.reject! do |o, e, b|
      o == observer && e == event && b == block
    end
  end
end