Comments (7)
I not sure I understand, can you give an example of what the API would look like please?
from wisper.
Something like this.
Wisper.subscribe(LoggerSubscriber.new, with: :log_event)
class LoggerSubscriber
def log_event(*args, event_name)
logger.info "#{event_name} occurred"
end
end
from wisper.
My gut feeling is the PR would add too much complexity in terms of code branching to be acceptable.
But there is a way. You could write a custom broadcaster which passes the event name in addition to the args.
Have a look here for more information. You could even release a gem :)
Let me know if you have any problems.
from wisper.
No problem. As I said I found a simple enough solution to the issue.
from wisper.
I think one of your comments has gone missing :)
Feel free to update the Wiki if you think the solution would be helpful to others.
from wisper.
Except for method_missing
there is one more way to solve the problem
of this type.
I define a listener as a decorator, providing necessary callbacks. In the
decorator I define two additional methods: #otherwise and #finalize.
The first one does some default actions, the second one calls #otherwise
when no expected notification has been received beforehand.
class Listener < SimpleDelegator
def on_success
# ...do something
end
# ... other callbacks the listener looks for
def otherwise
# ...do something
end
def finalize
otherwise unless @notified
end
# the publisher checks callback existense before calling it,
# and @notified remembers the fact
def respond_to?(*)
super ? (@notified = true) : false
end
end
The bright side of this trick is I can separate callbacks the listener looks for
from any others (including the case the publisher dies silently and sends
nothing).
The dark side is I need to explicitly call #finalize after service call.
publisher.subscribe listener
publisher.call # does something and sends notifications
listener.finalize
I don't think it is a universal solution, but in controllers or
when one publisher calls another and listens to its notification, it
works fine.
from wisper.
@krisleech I was referring to using method_missing
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.