forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #158
Closed
waiting-for-dev
wants to merge
2
commits into
waiting-for-dev/events_testability
from
waiting-for-dev/events_registration
Closed
Enforce event registration #158
waiting-for-dev
wants to merge
2
commits into
waiting-for-dev/events_testability
from
waiting-for-dev/events_registration
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given the global nature of our event bus, we need a system to scope the execution of a block to a selected list of subscribers. That's useful for testing purposes, as we need to be sure that others subscribers are not interfering with our expectations. This commit introduces a `Spree::Event.performing_only(listeners)` method. It takes a block during the execution of which only the provided listeners are subscribed: ```ruby listener1 = Spree::Event.subscribe('foo') { do_something } listener2 = Spree::Event.subscribe('foo') { do_something_else } Spree::Event.performing_only(listener1) do Spree::Event.fire('foo') # only listener1 will run end Spree::Event.fire('foo') # both listener1 & listener2 will run ``` This behavior is only available for the new `Spree::Event::Adapters::Default` adapter. We only need that for testing purposes, so the method is made available after calling `Spree::Event.enable_test_interface`. It prevents the main `Spree::Event` API from being bloated and sends a more explicit message to users. We also add a `Spree::Subscriber#listeners` method, which returns the set of generated listeners for a given subscriber module. It's called automatically by `Spree::Event.performing_only` so that users can directly specify that they only want the listeners for a given subscriber module to be run. `Spree::Subscriber#listeners` accepts an array of event names as arguments in case more fine-grained control is required. ```ruby module EmailSubscriber include Spree::Event::Subscriber event_action :foo event_action :bar def foo(_event) do_something end def bar(_event) do_something_else end end Spree::Event.performing_only(EmailSubscriber) do Spree::Event.fire('foo') # both foo & bar methods will run end Spree::Event.performing_only(EmailSubscriber.listeners('foo')) do Spree::Event.fire('foo') # only foo method will run end ``` A specialized `Spree::Event.silenced` method calls `Spree::Event.performing_only` with no listeners at all.
waiting-for-dev
force-pushed
the
waiting-for-dev/events_registration
branch
from
August 4, 2021 04:10
50085a9
to
cc255a6
Compare
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 so that we can't avoid polluting the global bus.
waiting-for-dev
force-pushed
the
waiting-for-dev/events_registration
branch
from
August 4, 2021 11:30
cc255a6
to
b4cbf0d
Compare
aldesantis
approved these changes
Sep 8, 2021
|
||
The registration happened at: | ||
|
||
#{registration.caller_location} |
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.
😍
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
from
September 9, 2021 08:16
e12da7e
to
e98d018
Compare
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
4 times, most recently
from
November 16, 2021 07:29
16e92c5
to
98726c2
Compare
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
3 times, most recently
from
November 30, 2021 05:54
57d6c60
to
dd9db5d
Compare
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
from
December 3, 2021 09:45
dd9db5d
to
5881d86
Compare
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
from
December 13, 2021 05:27
5881d86
to
7d9b4e9
Compare
Moved to solidusio#4226 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Make it mandatory to register an event before being fired, subscribed
to, or unsubscribed from.
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:
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 so that wecan't avoid polluting the global bus.
Checklist: