Skip to content
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

Conversation

GustavoCaso
Copy link
Member

Fixes #3

@solnic @mensfeld I added the feature request to the library. Hope it helps 😄

@GustavoCaso GustavoCaso force-pushed the check-event-registration-before-subscribing branch from 9d3a4de to 35902e3 Compare March 2, 2018 10:30
@GustavoCaso GustavoCaso force-pushed the check-event-registration-before-subscribing branch from 35902e3 to 13a4009 Compare March 2, 2018 10:31
@GustavoCaso
Copy link
Member Author

@solnic any update on this one?

@@ -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)
Copy link
Member

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?


# @api private
def register_event?(event_id)
events.keys.any? { |value| value == event_id }
Copy link
Member

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)

Copy link
Collaborator

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

Copy link
Member

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 😅

Copy link
Collaborator

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 ;)

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

register => registered

else
true
end
end
Copy link
Member

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?

Copy link
Member Author

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.

@@ -18,6 +18,14 @@ def initialize(id)
end
end

# @api public
UnregisterEventError = Class.new(StandardError) do
Copy link
Member

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

@AMHOL
Copy link
Member

AMHOL commented Mar 19, 2018

unregistered implies that the event was previously registered but has been removed, is this the case or is it the case for events that have never been registered? If it's the latter I'd go for something like NoEventRegisteredError and similar messages

@GustavoCaso
Copy link
Member Author

@solnic @mensfeld @AMHOL I applied the changes. 😄

@GustavoCaso
Copy link
Member Author

@solnic any update on this one?

@GustavoCaso GustavoCaso reopened this Apr 18, 2018
@GustavoCaso
Copy link
Member Author

Ups I accidentally close it 😄

@mensfeld
Copy link
Collaborator

Uff...

InvalidEventNameError = Class.new(StandardError) do
# @api private
def initialize
super("please provide a valid event name, it could be either String or Symbol")
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

# Initialize a new event
#
# @param [Symbol] id The event identifier
# @param [Symbol, String] id The event identifier
# @param [Hash] payload Optional payload
Copy link
Member

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

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")
Copy link
Member

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
Copy link
Member

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?

Copy link
Member Author

@GustavoCaso GustavoCaso Jul 11, 2018

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline at EOF

expect(event.listener_method).to eq :on_test
end

it 'replace dots for underscore' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/s/replace/replaces

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/s/underscore/underscores

@matugm
Copy link

matugm commented Nov 29, 2018

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.

@mensfeld
Copy link
Collaborator

@matugm that is a good idea. I've already wasted a lot of time because of that ;)

@drqCode
Copy link

drqCode commented Feb 12, 2019

This would help a lot if it will be merged and released:)

@solnic
Copy link
Member

solnic commented Feb 13, 2019

@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.

@mensfeld
Copy link
Collaborator

mensfeld commented Jul 3, 2019

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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