-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Prevents BehaviorSubject from dispatching undefined
to subscribers if initialized without a value.
#27
Conversation
Conflicts: package.json src/core/disposables/disposable.js
…ers if initialized without a value.
…oesn't dispatch an undefined value.
…ith different naming.
Not sure if I want to break compatibility with the current .NET implementation which does not allow for such a thing. |
Does .NET require a BehaviorSubject is initialized with a value?
If the BehaviorSubject isn't initialized with a value, it shouldn't report that it was initialized with BehaviorSubjects are excellent wrappers for Model values, but filtering the initial undefined value is painful to remember, write, and explain, and is hurting adoption in my present project. var userModel = new Rx.BehaviorSubject();
var userSubscription = userModel
.where(_.identity) // even with this shorthand... why? the userModel doesn't have a value yet.
.subscribe(function(val) {
$('.username').text(val.name);
});
// ... later
userModel.onNext(new User('Paul')); |
.NET's Strictly speaking, a BehaviorSubject I believe that by allowing a behavior subject to be instantiated without a value we would be disregarding the functional purity of Rx in favor of a convenient shortcut. :/ |
It's not unreasonable that a BehaviorSubject instance can exist in a "pending" state, like a JSON request would. Other Observables/Subjects don't dispatch If you really want purity, throw an error if BehaviorSubject is initialized without a value. Purity shouldn't be enforced if it provides negative utility. Code is just a tool we have to make the future happen sooner. |
That being said, this situation matches a different pattern that involves a Subject and an Observable capable of yielding the latest value to new subscribers (this is different than a BehaviorSubject, in that a BehaviorSubject yields the current value instead of the latest value). I just looked it up, and once again, this already exists in the form of a replay subject! :)
I would like to suggest adding a function to a
Idea for implementation:
I'm not suggesting this is the best idea, but I don't believe it violates any of the principles current in place in Rx. |
Look, I get that BehaviorSubject is supposed to wrap a value, but that's traditionally been enforced by the compiler. Short of throwing an error, you can't enforce that BehaviorSubject is initialized with a value in JS. But throwing an error here isn't user-friendly, so why not duck type the thing and mimic the ReplaySubject if BehaviorSubject is initialized without a value? � In the first case, we're explicitly initializing without a value, including the It doesn't break backwards compatibility unless people are initializing BehaviorSubjects with no arguments and doing something with the undefined first dispatch, and I doubt many people are doing that. If they are, make 'em change their code to Adding a "latest" function to the ReplaySubject seems more problematic than changing BehaviorSubject in this way. First, why restrict it to getting the latest value? Why can't you get any cached value? To be clear, I'm not advocating this approach. |
Well, if you feel that way, I'm open to creating different Subjects which wrap said behavior, but the one as is should stay as it is. |
Another Subject type is fine with me. From what I can tell, the thing I want is close to a reactive Maybe, but I'm not really qualified to say. As for names, MaybeSubject, FutureSubject, or PromiseSubject? @cwharris also proposed adding a "maybe" instance method on BehaviorSubject. It's not a bad idea if there's concern over the impact on lib size, since the new type is only slightly different. var presentValue = new BehaviorSubject();
var futureValue = new BehaviorSubject().maybe(); |
fix global build (fixes Reactive-Extensions#22)
No description provided.