Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: overload of BehaviorSubject #583

Closed
roganov opened this issue Oct 22, 2015 · 10 comments
Closed

Proposal: overload of BehaviorSubject #583

roganov opened this issue Oct 22, 2015 · 10 comments

Comments

@roganov
Copy link

roganov commented Oct 22, 2015

While building apps with Rx, I have quite often had a need for an observable that remembers its latest value, but doesn't have an initial value. Now this can be achieved by using ReplaySubject(1), but ReplaySubject is 'heavier'.
What I'm proposing is to extend BehaviorSubject so that when it didn't get passed and initial value, it would wait for first value to come in before emitting anything.

const bs = new BehaviourSubject();
bs.subscribe(::console.log); // would print 'undefined'

Proposal:

const bs1 = new BehaviourSubject();
bs1.subscribe(::console.log); // wouldn't print anything, waiting for the first event
const bs2 = new BehaviourSubject(1);
bs2.subscribe(::console.log); // would print '1'

This, to my understanding, is implemented, in RxJava's BehaviorSubject:

public static <T> BehaviorSubject<T> create()
public static <T> BehaviorSubject<T> create(T defaultValue)
@kwonoj
Copy link
Member

kwonoj commented Oct 22, 2015

Seems RxJS4 also have it, maybe simply not migrated yet? (or dropped for some reason? at least wasn't able to lookup issues)

@mattpodwysocki
Copy link
Collaborator

👎 we have been over this in RxJS and declined it then

@trxcllnt
Copy link
Member

@mattpodwysocki I know this is a dup of my pull from ~2 years ago, but perhaps we can introduce a new type, LatestSubject maybe?

@mattpodwysocki
Copy link
Collaborator

@trxcllnt yes, as I said before in the old PR that I was perfectly willing to make a new Subject type but was unwilling to change the meaning of the BehaviorSubject due to its downstream dependencies with publishValue etc

@roganov
Copy link
Author

roganov commented Oct 23, 2015

Yeah a new subject type would be OK. Or we could probably make ReplaySubject(1) fast (treat it as a special case).

@paulpdaniels
Copy link
Contributor

Couldn't we just make all bounded buffers a special case and implement a circular buffer instead of doing splicing?

@staltz
Copy link
Member

staltz commented Oct 23, 2015

Actually you can hack around BehaviorSubject if you don't want the first init event.

const bs1 = new BehaviourSubject('ignore me');
const interface = bs1.filter(x => x !== 'ignore me');
interface.subscribe(::console.log); // wouldn't print anything, waiting for the first event

But I also think something like LatestSubject would be helpful. Maybe we could bikeshed on that name still, we could consider HoldSubject or KeepSubject, taking a bit of inspiration from Sodium which has the hold function, a bit similar to publishBehavior.

@staltz
Copy link
Member

staltz commented Oct 23, 2015

Yeah a new subject type would be OK. Or we could probably make ReplaySubject(1) fast (treat it as a special case).

@roganov ReplaySubject and BehaviorSubject have different semantics for observers after they complete. See #453

@ghetolay
Copy link
Contributor

ghetolay commented May 5, 2017

So I came to the exact same conclusion when I needed to use a publishReplay(1) but seems too heavy compared to a publishBehavior() + filter().

Is the idea of a new Subject dropped or are we just waiting for a PR ?

Also I don't like HoldSubject because it sounds exactly like the current BehaviorSubject behavior: meaning there is always a value. MaybeHoldSubject would fit better as it may be empty.
I think LatestSubject is the more explicit.

@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants