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 #158

Conversation

waiting-for-dev
Copy link

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:

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.

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)

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 waiting-for-dev force-pushed the waiting-for-dev/events_registration branch from 50085a9 to cc255a6 Compare August 4, 2021 04:10
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 waiting-for-dev force-pushed the waiting-for-dev/events_registration branch from cc255a6 to b4cbf0d Compare August 4, 2021 11:30

The registration happened at:

#{registration.caller_location}
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 waiting-for-dev force-pushed the waiting-for-dev/events_testability branch from e12da7e to e98d018 Compare September 9, 2021 08:16
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch 4 times, most recently from 16e92c5 to 98726c2 Compare November 16, 2021 07:29
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch 3 times, most recently from 57d6c60 to dd9db5d Compare November 30, 2021 05:54
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch from dd9db5d to 5881d86 Compare December 3, 2021 09:45
@waiting-for-dev waiting-for-dev force-pushed the waiting-for-dev/events_testability branch from 5881d86 to 7d9b4e9 Compare December 13, 2021 05:27
@waiting-for-dev
Copy link
Author

Moved to solidusio#4226

@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/events_registration branch December 22, 2021 04:56
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.

2 participants