Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unsubscribe a handler in Bacon.js?

Tags:

bacon.js

I'm learning to use the fantastic Bacon.js library for functional reactive programming. Adding handlers to a property or stream is easy:

handler = function(value){... do something ...}
property.onValue(handler)

Say somewhere down the line I want to cancel this subscription, like this (pseudocode):

property.unsubscribe(handler)

Is there a way to do this with Bacon.js?

like image 988
jpadvo Avatar asked Mar 08 '26 21:03

jpadvo


1 Answers

Both answers above are correct. Personally, I've never used either solution though. In fact, they mainly exist for internal purposes and for writing custom combinators and integrations.

So instead I recommend combinators like take, takeWhile and takeUntil. In an FRP application, the signal that triggers the need can usually be modeled as an EventStream. In that case you can do

var data, stopper // EventStreams
data.takeUntil(stopper).onValue( .. )
like image 119
raimohanska Avatar answered Mar 12 '26 10:03

raimohanska



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!