BehaviorSubject should preserve .value after calling .pipe() #7506
dariomannu
started this conversation in
Report issues other than bug
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
BehaviorSubject.pipe(...operators), according to the docs, returns an Observable, not another BehaviorSubject.
However, the resulting Observable still has a
.next()
method, which makes it like a BehaviorSubject.There is one important exception, though, which is that
.value
andgetValue()
are now missing from the abovestream
.I'm pretty convinced that the return type of
BehaviorSubject.pipe()
should be alsoBehaviorSubject
, not justObservable
(same forSubject.pipe()
which should return anotherSubject
)I wonder if this was an oversight, an in-progress feature under test and consideration, or something, but it would be certainly useful in a few key scenarios.
I do understand the potential for imperative-progamming misuse, but as long as BehaviorSubject exposes
.value
, there are many others anyway.I don't thing we need to address these here, as I'm convinced exposing
.value
on the result of.pipe()
would not add any more potentials for misuse other than thoseBehaviorSubject.value
already has. Feel free to disagree with me on this, in case.There are certain use cases in which getting the last emitted value from the stream (= from the output of the whole pipeline) has some key benefits.
1: I'm working on a UI library for RxJS, in which a
BehaviorSubject
can be used to statically render its initial (or latest/current) value into an HTML template. The.value
property will be used and printed synchronously to the HTML. If we didn't have.value
, we'd have to subscribe to the stream and asynchronously update the DOM, potentially causing unnecessary reflows on the page..value
saves us from that.Unfortunately, that's hindered by
BehaviorSubject.pipe()
, as it loses.value
.2: Other use case: some advanced server-side rendering: If we have a BehaviorSubject with an initial/latest/current value, then we can stream it straight to the client and let it render its content. Subsequent emissions from the BehaviorSubject would be genuine updates that we can still send downstream requiring updates on the page.
Playground on Stackblitz
I wonder if there's still any good reason not to expose
.value
on the result ofBehaviorSubject.pipe()
, or just make.pipe()
explicitly return a normal BehaviorSubject, and if so, are the RxJS team happy to support this proposal?Beta Was this translation helpful? Give feedback.
All reactions