Sha256: f9dcc273970d1172c62b0f11e96480ecbd90d8096f63475ff9196694ea370b87

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 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
        raise e unless broker.rescue_event

        broker.rescue_event.call(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 as_json
        {
          name: name,
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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