Comments (9)
How about on
accepts a regex?
Wisper.add_listener(ItemListener.new, on: /item_*_rs/, with: :add_item)
from wisper.
Makes more sense @krisleech.
from wisper.
@apotema can you try #80 please?
gem 'wisper', github: 'krisleech/wisper', branch: 'add-regex-support-for-on-arg'
from wisper.
By the way I don't think /item_*_rs/
is the correct regexp to match your events: http://rubular.com/r/2farGcgXD0
from wisper.
@krisleech the regex-support is working, all my 22 tests passed.
One thing that I noticed, is that all methods on the Listener stop working, once you set the 'on' condition.
This is my listener
class ItemListener
def add_item user, models
ItemService.new(user, models).save
end
end
It works great if I call him like this.
Wisper.subscribe(ItemListener.new, on: /item_(.*)(add|query)_rs/, with: :add_item)
But if I require that Listener to respond to another call, like 'item_remove_rs', I can't put another function in there, and expect that it will respond to it.
class ItemListener
def add_item user, models
ItemService.new(user, models).save
end
def item_remove_rs user,models
ItemService.new(user, models).save
end
end
I have to subscribe this listener twice.
Wisper.subscribe(ItemListener.new, on: /item_(.*)(add|query)_rs/, with: :add_item)
Wisper.subscribe(ItemListener.new)
Now it will respond for my 'regex' and for the 'item_remove_rs'.
The 'on' parameter has the 'side effect' of making all the other methods on the listener inert.
Maybe we could think the 'on' parameter, not as something that will exclude other methods on the listener, but give a specific method a rule and let the other methods works a usual.
from wisper.
One thing that I noticed, is that all methods on the Listener stop working, once you set the 'on' condition.
Correct, the on
option is used to restrict which events are broadcast to the subscribers. When used in conjuction with the with
option this restriction remains.
You could indeed subscribe the listener twice, but I suspect you will end up with some events being broadcast twice also.
Maybe we could think the 'on' parameter, not as something that will exclude other methods on the listener, but give a specific method a rule and let the other methods works a usual.
At this stage I can't change the behaviour of these options.
What you might consider doing in your listener is just aliasing the methods:
class ItemListener
def add_item
# ...
end
alias_method :add_item, :item_query_rs
alias_method :add_item, :item_discount_add_rs
# etc...
end
from wisper.
@apotema Any further thoughts on this? I'd like to merge the PR.
from wisper.
No @krisleech. That is it for me.
Thanks
from wisper.
Thanks, PR merged. @apotema if you ever fancy blogging about how you are using eventing or even posting a gist or example to the Wiki I would be very intrested to take a look.
from wisper.
Related Issues (20)
- 'coveralls' missing as a build dependency in gemspec HOT 7
- Please release new version without `console` HOT 7
- What's the proper way to test global listeners? HOT 1
- Any suggestions for how to persisting events? HOT 1
- Should we choose WisperNext over Wisper? HOT 1
- An exception raised inside a listener is logged but not raised
- Using Fibers breaks the execution of Wisper HOT 1
- Ruby 3.0 support for broadcast with kwargs HOT 14
- Unable to unsubscribe global listeners in Rails 6 when config.cache_classes is false HOT 1
- Does this work with Ruby 3 yet? HOT 1
- Fix Travis CI HOT 1
- switch from `master` to `main` as primary branch
- Build status badge (image) broken on README HOT 1
- Please modify your gemspec to take the local bin directory out of the global $PATH HOT 1
- Release Wisper 3.0-rc1 HOT 2
- Release wisper 3.0.0 HOT 2
- Request to Contribute Documentation Updates HOT 1
- An easy way of disabling wisper within a block HOT 1
- console" from wisper conflicts with installed executable from fastlane HOT 3
- Incorrect event name logging with :with option in Wisper event broadcasting
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wisper.