-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raise an exception when trying to subscribe to an unregister event #4
Raise an exception when trying to subscribe to an unregister event #4
Conversation
9d3a4de
to
35902e3
Compare
35902e3
to
13a4009
Compare
@solnic any update on this one? |
lib/dry/events/bus.rb
Outdated
@@ -76,6 +76,11 @@ def subscribe(event_id, query, &block) | |||
def subscribed?(listener) | |||
listeners.values.any? { |value| value.any? { |(block, _)| block.equal?(listener) } } | |||
end | |||
|
|||
# @api private | |||
def register_event?(event_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be better to call this methods event_registered?
lib/dry/events/bus.rb
Outdated
|
||
# @api private | ||
def register_event?(event_id) | ||
events.keys.any? { |value| value == event_id } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified as events.key?(event_id)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@solnic do we store nils as values? If not it could be simplified to !events[event_id].nil? as it is faster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mensfeld if you do register_event(nil)
then it'll be stored, but we shouldn't allow that 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@solnic so my point is valid ;) @GustavoCaso maybe we should add a dry-validation to validate the format of the event ;)
lib/dry/events/publisher.rb
Outdated
UnregisterEventError = Class.new(StandardError) do | ||
# @api private | ||
def initialize(event_id) | ||
super("you are trying to subscribe to an event: #{event_id} that has not been register") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
register
=> registered
lib/dry/events/publisher.rb
Outdated
else | ||
true | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's OK that this returns true
for other types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the subscribe
method accepts either an object listener or an event name we need to do a check here, I agree with the concern of returning always true
for no event name, maybe we could move the check up to the subscribe
method and only call this method if is an event name.
lib/dry/events/publisher.rb
Outdated
@@ -18,6 +18,14 @@ def initialize(id) | |||
end | |||
end | |||
|
|||
# @api public | |||
UnregisterEventError = Class.new(StandardError) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also another instance of unregister
here
|
@solnic any update on this one? |
Ups I accidentally close it 😄 |
Uff... |
InvalidEventNameError = Class.new(StandardError) do | ||
# @api private | ||
def initialize | ||
super("please provide a valid event name, it could be either String or Symbol") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to actually just accept any non-nil value?
DOT = '.'.freeze | ||
UNDERSCORE = '_'.freeze | ||
|
||
# @!attribute [r] id | ||
# @return [Symbol] The event identifier | ||
# @return [Symbol, String] The event identifier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above.
lib/dry/events/event.rb
Outdated
# Initialize a new event | ||
# | ||
# @param [Symbol] id The event identifier | ||
# @param [Symbol, String] id The event identifier | ||
# @param [Hash] payload Optional payload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Says optional payload but the argument is required now
lib/dry/events/publisher.rb
Outdated
NoEventRegisteredError = Class.new(StandardError) do | ||
# @api private | ||
def initialize(event_id) | ||
super("you are trying to subscribe to an event: #{event_id} that has not been registered") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth quoting event_id
with the traditional
`#{event_id}'
when String, Symbol | ||
__bus__.event_registered?(object_or_event_id) | ||
else | ||
true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this defaults to true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can subscribe
to events or you could subscribe
a listener object, in that case, there is no event registered in the _bus
to check.
Later there is another check https://github.com/dry-rb/dry-events/blob/master/lib/dry/events/bus.rb#L53 in the Bus
to see if the listener object have the appropriate method
expect(ev.listener_method).to eq :on_hello_world | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline at EOF
spec/unit/dry/events/event_spec.rb
Outdated
expect(event.listener_method).to eq :on_test | ||
end | ||
|
||
it 'replace dots for underscore' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/s/replace/replaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/s/underscore/underscores
I think it would also be helpful to raise an exception when trying to publish an event that doesn't exist. It's easy to make a typo & waste time looking for the problem. |
@matugm that is a good idea. I've already wasted a lot of time because of that ;) |
This would help a lot if it will be merged and released:) |
@drqCode looks like @GustavoCaso went M.I.A so somebody could pick it up, update, address issues that were mentioned in the review, and open a new PR. |
@GustavoCaso I don't have permissions to push into your fork, so I'm closing that issue and wil reopen with your code from the origin. |
Fixes #3
@solnic @mensfeld I added the feature request to the library. Hope it helps 😄