dry-rb / dry-events Goto Github PK
View Code? Open in Web Editor NEWPub/sub system
Home Page: https://dry-rb.org/gems/dry-events
License: MIT License
Pub/sub system
Home Page: https://dry-rb.org/gems/dry-events
License: MIT License
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
We found (with @mensfeld) that we can notify developers with unused subscriber code. Now we have a question: could it be helpful for community or not?
@solnic I have been looking at the code for learning purposes, and notice in the Publisher
module, when a new bus is instantiated that name
will always be nil
dry-events/lib/dry/events/publisher.rb
Line 62 in 71a402d
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:
dry-events/lib/dry/events/bus.rb
Line 34 in 67ab020
In this method, there is simply no listener so the loop is a no-op.
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.
dry-events/lib/dry/events/publisher.rb
Lines 181 to 238 in 8094007
For example, given that dry-monitor registers :sql
event with the .register_event(:name)
:
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.