Sha256: 127a7c8af03d74f8dfe905ab390b662fe36e0e1efbc5ede6604180d21dafb8ef

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

require 'has_guarded_handlers'
require 'singleton'
require 'celluloid'

module Adhearsion
  module Events

    class Handler
      include HasGuardedHandlers
      include Singleton

      def call_handler(handler, guards, event)
        super
        throw :pass
      end

      alias :register_callback :register_handler

      def method_missing(method_name, *args, &block)
        register_handler method_name, *args, &block
      end

      def respond_to_missing?(method_name, include_private = false)
        true
      end
    end

    class Worker
      include Celluloid

      def work(type, object)
        Handler.instance.trigger_handler type, object
      rescue => e
        raise if type == :exception
        async.work :exception, e
      end
    end

    class << self
      def method_missing(method_name, *args, &block)
        Handler.instance.send method_name, *args, &block
      end

      def respond_to_missing?(method_name, include_private = false)
        Handler.instance.respond_to? method_name, include_private
      end

      def trigger(type, object = nil)
        queue.async.work type, object
      end

      def trigger_immediately(type, object = nil)
        queue.work type, object
      end

      def draw(&block)
        Handler.instance.instance_exec(&block)
      end

      def queue
        @queue || refresh!
      end

      def init
        @queue = Worker.pool(size: Adhearsion.config.core.event_threads)
      end

      def refresh!
        @queue = nil
        Handler.instance.clear_handlers
        init
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adhearsion-3.0.0.beta1 lib/adhearsion/events.rb