Reputation: 9425
I am trying to figure out the 'correct' way of implementing the dispose handler on my custom-created Observable using Rx.Observable.create
named dispose function returned:
Rx.Observable.create(obs => {
//emit values
obs.onComplete();
return function dispose() {
console.log('diposed');
};
});
anonymous function returned:
Rx.Observable.create(obs => {
//emit values
obs.onComplete();
return () => {
console.log('disposed');
};
});
explicit Disposable.create returned:
Rx.Observable.create(obs => {
//emit values
obs.onComplete();
return Rx.Disposable.create(() => {
console.log('disposed');
});
});
They all seem to work exactly alike but the documentation is not really clear about what is the preferred way to go.
Upvotes: 1
Views: 138
Reputation: 18663
It will come down to really what your preference is on what you want to do. Though I think the three uses are actually, undefined
, function
, Disposable
. The choice between named function and anonymous lambda is more of a JavaScript distinction rather than an Rx one so I encourage you to read up on the differences (semantic this
, callstack readability, etc...).
From a usability standpoint it doesn't really matter because the framework will wrap false-y values and functions in either a Disposable.empty
or Disposable.create
respectively.
One advantage of the (arrow) function approach is that if you at some point upgrade to RxJS5 where Disposable
-> Subscription
you can avoid having to make a whole bunch of refactorings where ever you explicitly called that type (or do some sort of global aliasing), since the new version does the same sort of conversion magic.
The choice is up to you.
Upvotes: 2