-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Simpler way to convert a stream to Behavior Subjects or some other alternative? #1085
Comments
@tusharmath why not use a |
That would work but only for this use case. Say I add another filter — const divisibleBy177 = subject.filter(x => x % 777 === 0) Now what would be the buffer size? What I am really wanting to do is something like what Can I implement a version of combineLatest (called replayLatest, for the lack of a better name), that works like this — Observable.replayLatest(even, odd).subscribe(x => console.log(x))
/** output
even 104
odd 105
**/ Thought's comments ? |
Would it work to put your replay at the end of each stream, i.e. something like |
Can you paste the exact code which I should be using? |
const subject = new rx.BehaviorSubject(100)
const even = subject.filter(x => x % 2 === 0).replay(null,1)
const odd = subject.filter(x => x % 2 === 1).replay(null,1) I am working in RxJS 4. If you use the newer version, I don't know if the replay operator is still there. |
it doesn't output anything. |
Gosh that's unsurprising. I misunderstood your question. You are subscribing after all the data have been passed. Cf. http://stackoverflow.com/questions/32190445/hot-and-cold-observables-are-there-hot-and-cold-operators/34669444#34669444 |
@brucou Loved the links you just gave. I learnt quite a few things. rx.Observable.prototype.replayLatest = function () {
const subject = new rx.BehaviorSubject()
this.subscribe(subject)
return subject
} And then use it — const subject = new rx.BehaviorSubject(100)
const even = subject.filter(x => x % 2 === 0).replayLatest()
const odd = subject.filter(x => x % 2 === 1).replayLatest()
subject.onNext(101)
subject.onNext(102)
subject.onNext(103)
subject.onNext(104)
subject.onNext(105)
even.subscribe(x => console.log('even', x))
odd.subscribe(x => console.log('odd', x))
/**
OUTPUT
even 104
odd 105
**/ |
For your own needs, you can do whatever you please. If you publish a new Rx operator, it should however respect Rxjs contract and guidelines. Guidelines here : https://github.com/Reactive-Extensions/RxJS/tree/master/doc/designguidelines. Personally I would not find this operator to be a great addition to the library but who am I to judge. But for your personal needs, by all means go ahead. |
Thank you so much @brucou ! |
Add test for groupBy() operator, to verify that it supports the composable lift architecture. Test for issue Reactive-Extensions#1085.
Fix bug Reactive-Extensions#1085 in which groupBy was not using lift(). Using lift() is critical to support the ubiquitous lift-based architecture in RxJS Next Resolves Reactive-Extensions#1085.
I was actually looking for something like this. It works well in the case where .share() does not, ie: all subscribers dont subscribe before the source starts emitting yet want the latest value. Although, it could be improved by adding a subscriber count and unsubscribing when the count = 0; |
Long time passed, let me see what that was about again. alright, main reason I guess I was thinking it is not such a good addition to the library is that there are already plenty of operators out there, and this one seems like it could be obtained easily from the existing ones, I don't think it breaks any concept in reactive programming or anything. It looks very close to Also, with the |
I stumbled on this while trying to get something similar in Angular 4 and thought it may help someone. I cannot yet get the code to work in Angular, not sure if it's because I'm using a BehaviorSubject instead of a straight Observable, but still working through that issue. In the meantime this fiddle does work: https://jsfiddle.net/aspergillusOryzae/swktjsps/
|
In the above case if the subscription is made later in time then only the last emitted value is dispatched.
What I would like is something like this —
To do this the only way I have found is to create two behavior subjects for each one of the streams and and subscribe to those —
Is there a way I can simplify this code and not have to create BehaviorSubjects for each of the the stream?
[closing #1083 #1084 in lieu if this issue]
The text was updated successfully, but these errors were encountered: