Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails-observers vs wisper gem

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?

like image 771
jmarceli Avatar asked Apr 20 '16 16:04

jmarceli


1 Answers

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.

like image 140
Kris Avatar answered Nov 17 '22 07:11

Kris