Are there any benefits of using wisper
gem over rails-observers
?
They both looks quite similar at first sight, but wisper seems to be more supported by community (based on GH stars, commits and releases). Are there any significant differences between them?
Rails observers suffer the same problems as ActiveRecord callbacks, primarily that they can't be turned off. With ActiveRecord callbacks you forever couple the model to whatever is referenced in the callback and whatever side effect occurs in the callback always happens in all circumstances. Using observers only really moves the problem.
What if we want to use our model in a context, which we don't foresee today, in which we don't want the observer callback to occur?
If you search StackOverflow for “Rails callbacks”, a large number of the results pertain to seeking means to avoid issuing the callback in certain contexts. It almost seems as though Rails developers discover a need to avoid callbacks as soon as they discover their existence.
ref: http://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/
Wisper (disclaimer, author here) allows publishers (e.g. a model) to broadcast events when something significant happens. Listeners are subscribed to the publishers, at runtime. The publishers and subscribers know nothing of each other. Instead of having a hard dependency on each other, they only depend on the event.
Our systems, by necessity, have dependencies between objects, they need to communicate. However we want this dependency to be as light as possible so that an object can be used, in the future, in different contexts we currently might not be able to foresee today. Also see Connascence for examples of light/hard dependencies.
Using Wisper, in different contexts you can choose to subscribe a listener or not.
For example in a controller I might subscribe some listeners, but in my unit test I might want to test the model isolation, without these side effect occurring. Another example might be a rake task, where I want to save a model but do not want certain side effects to occur. Or an admin controller where I do/don't want something to which should/shouldn't happen in other contexts.
Finally Wisper has built-in support for asynchronous broadcasting of events which Observers do not, they are always synchronous.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With