Giter VIP home page Giter VIP logo

dry-events's Introduction

dry-events Gem Version CI Status

Links

Supported Ruby versions

This library officially supports the following Ruby versions:

  • MRI >= 3.0.0
  • jruby >= 9.4 (not tested on CI)

License

See LICENSE file.

dry-events's People

Contributors

actions-user avatar deepj avatar dry-bot avatar esparta avatar flash-gordon avatar guilherme-andrade avatar gustavocaso avatar landongrindheim avatar mensfeld avatar olleolleolle avatar solnic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dry-events's Issues

Later registered events with class method will not be registered on the instance too

Later registered events with class method will not be registered on the instance too, because the duplication of events and listeners is being done only when any instance method ( any that use __bus__) is called.

# Subscribe to events.
#
# If the query parameter is provided, filters events by payload.
#
# @param [Symbol,String,Object] object_or_event_id The event identifier or a listener object
# @param [Hash] query An optional event filter
#
# @return [Object] self
#
# @api public
def subscribe(object_or_event_id, query = EMPTY_HASH, &block)
if block
__bus__.subscribe(object_or_event_id, query, &block)
else
__bus__.attach(object_or_event_id, query)
end
self
end
# Unsubscribe a listener
#
# @param [Object] listener The listener object
#
# @return [self]
#
# @api public
def unsubscribe(listener)
__bus__.detach(listener)
end
# Return true if a given listener has been subscribed to any event
#
# @api public
def subscribed?(listener)
__bus__.subscribed?(listener)
end
# Utility method which yields event with each of its listeners
#
# Listeners are already filtered out when query was provided during
# subscription
#
# @param [Symbol,String] event_id The event identifier
# param [Hash] payload An optional payload
#
# @api public
def process(event_id, payload = EMPTY_HASH, &block)
__bus__.process(event_id, payload, &block)
end
# Internal event bus
#
# @return [Bus]
#
# @api private
def __bus__
@__bus__ ||= self.class.new_bus
end

For example, given that dry-monitor registers :sql event with the .register_event(:name):

https://github.com/dry-rb/dry-monitor/blob/7a0625c199c8cbd8d053956b2735d4074a22b0dd/lib/dry/monitor/sql/logger.rb#L5-L10

loading the sql dry-monitor extension after an instance of Publisher was created and consumed will not registers new events if the entire app uses the same instance of 'Publisher' (Dry::Web::Container creates an instance of Notifications publisher that is supposed to be used globally, at least for logging)

Feature request: API to reach registered events and errors upon non-existing subscription

Moved from here: dry-rb/dry-monitor#11

Hey guys,

We use dry-monitor together with dry-events in karafka. In order to provide a "safe" API (safe in terms of typo mistakes etc) we've implemented two additional methods for the monitor that could be useful when working with it. Would it fit as a PR in this library?

      # Allows us to subscribe to events with a code that will be yielded upon events
      # @param event_name_or_listener [String, Object] name of the event we want to subscribe to
      #   or a listener if we decide to go with object listener
      def subscribe(event_name_or_listener)
        return super unless event_name_or_listener.is_a?(String)
        return super if available_events.include?(event_name_or_listener)
        raise Errors::UnregisteredMonitorEvent, event_name_or_listener
      end

      # @return [Array<String>] names of available events to which we can subscribe
      def available_events
        __bus__.events.keys
      end

I believe that this behavior would prevent users from subscribing to non-registered events.

Whole monitor can be found here: https://github.com/karafka/karafka/blob/master/lib/karafka/instrumentation/monitor.rb

Fail when publishing to non-existing event

I would expect calling app.publish('non-existing-event', {}) to fail with a UnknownEvent-like error.

Current behaviour is that you cannot subscribe to a non-existing event (raises Dry::Events::InvalidSubscriberError) but publishing is a no-op.

Since:

listeners[event_id].each do |listener, filter|

In this method, there is simply no listener so the loop is a no-op.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.