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

Enforce event registration #4226

Merged
merged 1 commit into from
Jan 5, 2022

Conversation

waiting-for-dev
Copy link
Contributor

Description

Make it mandatory to register an event before being fired, subscribed
, or unsubscribed.

It helps with two scenarios:

  • It avoids typo errors, like subscribing to an event ordr_finalized
    instead of order_finalized.

  • It avoids naming collisions between user-defined events and core ones.

Example:

Spree::Event.register('foo')
Spree::Event.fire('foo')

We add this behavior only to the new, recommended adapter. However, we
add into the deprecation error for the legacy adapter the instructions
to update.

We also add a new #unregister method for the test interface.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@waiting-for-dev waiting-for-dev marked this pull request as draft December 21, 2021 12:34
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_registration branch 2 times, most recently from e7e56c4 to 7a8b9a6 Compare December 22, 2021 04:31
@waiting-for-dev waiting-for-dev marked this pull request as ready for review December 22, 2021 04:54
@waiting-for-dev waiting-for-dev requested a review from a team December 22, 2021 04:54
Copy link
Member

@aldesantis aldesantis left a comment

Choose a reason for hiding this comment

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

😍

def register(event_name, adapter: default_adapter)
warn_registration_on_legacy_adapter if deprecation_handler.render_deprecation_message?(adapter)
return if deprecation_handler.legacy_adapter?(adapter)

Copy link
Member

Choose a reason for hiding this comment

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

Extra blank line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Removed.

@@ -59,6 +59,13 @@ def adapter
order.do_something
Spree::Event.fire 'event_name', order: order

- You need to register your custom events before firing or subscribing
to them (no need for Solidus' custom ones). Example:
Copy link
Member

Choose a reason for hiding this comment

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

Solidus' native ones?

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps "not necessary for events provided by Solidus or other extensions" would be more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, I used @jarednorman's suggestion 👍

Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

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

Looks good, other than the slightly confusing wording that Alessandro pointed out.

@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_registration branch from 7a8b9a6 to 5aa4a21 Compare December 27, 2021 05:00
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

Thanks, Marc!

I have one note: it is not quite clear to me what is the suggested approach at registering events. Considering that people may fire events multiple times in their codebase, is the initializer the right place where people should place these Spree::Event.register calls? Maybe worth mentioning or providing an example?

private

def suggestions(event_name)
dictionary = DidYouMean::SpellChecker.new(dictionary: event_names)
Copy link
Member

Choose a reason for hiding this comment

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

👍

@waiting-for-dev
Copy link
Contributor Author

it is not quite clear to me what is the suggested approach at registering events.

Good point. I'm checking now, and I'm finding some issues because of the loading of module subscribers. Please, put it on hold until I have a clearer picture.

Make it mandatory to register an event before being fired, subscribed
, or unsubscribed.

It helps with two scenarios:

- It avoids typo errors, like subscribing to an event `ordr_finalized`
  instead of `order_finalized`.

- It avoids naming collisions between user-defined events and core ones.

Example:

```ruby
Spree::Event.register('foo')
Spree::Event.fire('foo')
```

We add this behavior only to the new, recommended adapter. However, we
add into the deprecation error for the legacy adapter the instructions
to update.

We also add a new `#unregister` method for the test interface.
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_registration branch from 5aa4a21 to f420cba Compare December 28, 2021 09:29
@waiting-for-dev
Copy link
Contributor Author

Good point. I'm checking now, and I'm finding some issues because of the loading of module subscribers. Please, put it on hold until I have a clearer picture.

@kennyadsl, no significant issues in the end.

An event must be registered before being fired/subscribed, etc. In theory, that should mean that we can register them anywhere in the code before that. However, we have to add module subscribers into the equation. They're loaded in a #to_prepare block. That runs before the application code is loaded but after the initializers.

That means that we must register them on an initializer. However, the initializer must run after the adapter has been configured. As Rails loads initializer in alphabetical order, one called spree_events.rb would do the trick, but events.rb wouldn't. For that reason, I think it's simpler to recommend adding the registrations at the end of the spree.rb file. It's what I've done, updating the error and warning messages, as you pointed out.

@kennyadsl
Copy link
Member

@waiting-for-dev thanks, it works for me.

@waiting-for-dev waiting-for-dev merged commit 252a34d into master Jan 5, 2022
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/events_registration branch January 5, 2022 04:34
@waiting-for-dev waiting-for-dev mentioned this pull request Apr 15, 2022
5 tasks
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.

4 participants