Sha256: 38c08ea28191baf76cb74ec2f1c139109e2188ebf513581dfa7a0b29d8f4ccc5

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require_relative "../errors/invalid_action_response"
require_relative "../support"
module Moleculer
  module Service
    ##
    # Represents a service event.
    class Event
      # @!attribute [r] name
      #   @return [String] the name of the action
      # @!attribute [r] service
      #   @return [Moleculer::Service] the service that this event is tied to
      attr_reader :name, :service

      ##
      # @param name [String] the name of the action
      # @param service [Moleculer::Service] the service to which the action belongs
      # @param method [Symbol] the method which the event calls when executed
      # @param options [Hash] the method options
      # TODO: add ability to group events
      def initialize(name, service, method, options = {})
        @name    = name
        @service = service
        @method  = method
        @service = service
        @options = options
      end

      ##
      # Executes the event
      # @param data [Hash] the event data
      # @param broker [Moleculer::Broker] the moleculer broker
      def execute(data, broker)
        @service.new(broker).public_send(@method, data)
      rescue StandardError => e
        broker.config.handle_error(e)
      end

      ##
      # @return [Moleculer::Node] the node of the service this event is tied to
      def node
        @service.node
      end

      ##
      # @return [Hash] a hash representing this event as it would be in JSON
      def to_h
        {
          name: name,
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moleculer-0.3.0 lib/moleculer/service/event.rb