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

Support .Publish(...) #1629

Closed
Dorus opened this issue Apr 15, 2016 · 6 comments
Closed

Support .Publish(...) #1629

Dorus opened this issue Apr 15, 2016 · 6 comments

Comments

@Dorus
Copy link

Dorus commented Apr 15, 2016

For reference, the Rx Design Guidelines 5.10 suggest to use this operator.

The operator .publish() returns an ConnectableObservable, this means you first need to subscribe all your sources, .connect() it, keep a reference to your connect to prevent leaks, and then you also end up with a hot observable, so downstream subscribers better be ready as well.

Most of the time you only want to duplicate a stream locally. RxJS 4 and many other Rx implementations allow you to write

xs.publish(ob => ob.zip(ob.skip(1), (x, y) => {...}))

Here we use publish to peek at the next item in the source. Another favorite of mine is

xs.publish(ob => ob.window(ob.debounce(100)))

Because often you want to use a source for its own window-boundary.

This operator is very similar to .let, but avoid subscribing to the source twice. Useful on cold observables, but also to avoid the overhead of subscribing multiple times to a hot source. (imagine you use this trick 4x in a row with let, you end up with 2^4th subscriptions.)

@kwonoj
Copy link
Member

kwonoj commented Apr 15, 2016

Would you elaborate bit more? so far I know current publish already returns ConnectableObservable (

export function publish<T>(): ConnectableObservable<T> {
) would you mind to share some snippet of pusedo for suggested behavior?

@Dorus
Copy link
Author

Dorus commented Apr 15, 2016

The operator publish([selector]) should not return an ConnectableObservable, instead it returns the result of creating the connectable observable, passing the connectable observable to the selector,

aka

function publish(source, selector) {
  return Rx.Observable.create(ob => {
    var xs = source.publish();
    return selector(xs).subscribe(ob).add(xs.connect());
  });
}

Similar to publish in RxJS 4 and Rx.Net.

selector
Type: System.Func<IObservable, IObservable>
The selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence.

@kwonoj
Copy link
Member

kwonoj commented Apr 18, 2016

Thanks for clarification. I actually noticed RxJS 5 missing publish accepts selector compare to RxJS 4, and wasn't able to look up reason. @staltz , @Blesh , remember there was discussions to align sharing operators in RxJS 5, is this conclusion of it or it's yet migrated from RxJS 4?

@staltz
Copy link
Member

staltz commented Apr 18, 2016

Yeah, I think this makes sense to implement.

@kwonoj
Copy link
Member

kwonoj commented Apr 18, 2016

I'll try to introduce those changes.

kwonoj added a commit to kwonoj/rxjs that referenced this issue Apr 26, 2016
kwonoj added a commit to kwonoj/rxjs that referenced this issue Apr 27, 2016
@benlesh benlesh closed this as completed in 0e5991d May 2, 2016
@lock
Copy link

lock bot commented Jun 7, 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 7, 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

3 participants