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

Allow uniquely identifying subscriptions #1

Merged
merged 3 commits into from
Apr 14, 2022

Conversation

waiting-for-dev
Copy link
Contributor

We add an id: param to all the methods to create subscriptions. We can
use it to fetch a given subscription from the bus when a reference to it
is no longer available. Example:

subscription = bus.subscribe(:foo, id: :foo_subscription) { do_something }
bus.subscription(:foo) == subscription # => true

That's very useful as it allows us to make the event bus the only global
piece in the code and use it for testing purposes like in the
Omnes::Bus#performing_only method.

As it's possible to subscribe multiple instances of a subscriber class
to the same bus, we also add the possibility of making IDs dependent on
the given instance. For those cases, the id must be given as a lambda
taking the instance as argument:

handle :foo, with: :foo, id: ->(instance) { :"foo_#{instance.object_id}" }

Lastly, we force identifiers to be a Symbol.

We add an `id:` param to all the methods to create subscriptions. We can
use it to fetch a given subscription from the bus when a reference to it
is no longer available. Example:

```ruby
subscription = bus.subscribe(:foo, id: :foo_subscription) { do_something }
bus.subscription(:foo) == subscription # => true
```

That's very useful as it allows us to make the event bus the only global
piece in the code and use it for testing purposes like in the
`Omnes::Bus#performing_only` method.
As it's possible to subscribe multiple instances of a subscriber class
to the same bus, we also add the possibility of making IDs dependent on
the given instance. For those cases, the id must be given as a lambda
taking the instance as argument:

```ruby
handle :foo, with: :foo, id: ->(instance) { :"foo_#{instance.object_id}" }
```
@waiting-for-dev waiting-for-dev merged commit 622483e into main Apr 14, 2022
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/identify_subscriptions branch April 14, 2022 08:17
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.

1 participant