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

Add .toBehaviorSubject() and .toReplaySubject() methods #2841

Closed
diestrin opened this issue Sep 20, 2017 · 6 comments
Closed

Add .toBehaviorSubject() and .toReplaySubject() methods #2841

diestrin opened this issue Sep 20, 2017 · 6 comments

Comments

@diestrin
Copy link

I find it really verbose to convert an observable to either beahvior or replay subjects.

Every time I need to do this I have to:

const replay$ = new Replay<string>();
source$.subscribe(replay$);

Wouldn't be better to have shortcuts for this? Like:

const replay$ = source$.toReplaySubject();
const behavior$ = source$.toBehaviorSubject('inital value');

I can get into the code and hack it and PR, but I'd like to hear the thoughts first. Maybe there's a reason not to have this?

Regards.

@diestrin
Copy link
Author

Is publishBehavior the same thing?

@diestrin
Copy link
Author

I'm trying to do the same thing with publishBehavior, but it still is a multi step process (now I need 3 lines, and 2 variables) and I can't seem to have access to the observable:

const connectable$ = source$.publishBehavior('initial value');

// this method is protected, and there's no other way to get access
// to the BehaviorSubject created
const behavior$ = connectable$.getSubject();

connectable$.connect();

@benlesh
Copy link
Member

benlesh commented Sep 22, 2017

It looks like we missed putting a selector on publishBehavior like publish has.

However, you can do everything in one step with multicast:

source$.multicast(new ReplaySubject(3), replayed =>
  replayed.map(x => x + x); // do stuff here
)
.subscribe(x => console.log(x))

@benlesh
Copy link
Member

benlesh commented Sep 22, 2017

Closing in favor of #2844 and #2845.

@diestrin
Copy link
Author

Awesome, thanks :)

@benlesh benlesh closed this as completed Sep 22, 2017
@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

2 participants