Sha256: a9d4dcf742762ce989a16c4527bca1ab90c87203229607b16021998c4618b929

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8

module Punchblock
  module Component
    class ComponentNode < CommandNode
      include HasGuardedHandlers

      def initialize(*args)
        super
        @complete_event_resource = FutureResource.new
        register_internal_handlers
      end

      def register_internal_handlers
        register_handler :internal, Event::Complete do |event|
          self.complete_event = event
          throw :pass
        end
      end

      def add_event(event)
        trigger_handler :internal, event
      end

      def trigger_event_handler(event)
        trigger_handler :event, event
      end

      def register_event_handler(*guards, &block)
        register_handler :event, *guards, &block
      end

      def write_action(action)
        client.execute_command action, :target_call_id => target_call_id, :component_id => component_id
        action
      end

      def response=(other)
        if other.is_a?(Ref)
          @component_id = other.id
          client.register_component self if client
        end
        super
      end

      def complete_event(timeout = nil)
        @complete_event_resource.resource timeout
      end

      def complete_event=(other)
        return if @complete_event_resource.set_yet?
        @complete_event_resource.resource = other
        complete!
      end

      ##
      # Create a Rayo stop message
      #
      # @return [Stop] a Rayo stop message
      #
      def stop_action
        Stop.new :component_id => component_id, :target_call_id => target_call_id
      end

      ##
      # Sends a Rayo stop message for the current component
      #
      def stop!(options = {})
        raise InvalidActionError, "Cannot stop a #{self.class.name.split("::").last} that is not executing" unless executing?
        stop_action.tap { |action| write_action action }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
punchblock-1.6.1 lib/punchblock/component/component_node.rb
punchblock-1.6.0 lib/punchblock/component/component_node.rb
punchblock-1.5.3 lib/punchblock/component/component_node.rb
punchblock-1.5.2 lib/punchblock/component/component_node.rb
punchblock-1.5.1 lib/punchblock/component/component_node.rb
punchblock-1.5.0 lib/punchblock/component/component_node.rb