I have a stream that calls a service that returns a stream that needs the info of the original stream for the request.
this.messages$ = this.selectedChat$
.switchMap(chat => this.chatService.subscribeMessages(chat.room))
.map(messages) =>
messages.map((message: any) => {
//Here need add information of the chat object
return message;
})
);
All operators that I see that can merge the streams and separate them later (like combineLatest
) receive parameters, therefore, I can´t use the original stream info to call the service that generates the other stream, and I would not like to use subscribe on the original stream and return a new stream inside the subscribe because it's a little messy code.
Any advice?
Use the selector function
in the switchMap
operation:
this.messages$ = this.selectedChat$
.switchMap(chat => this.chatService.subscribeMessages(chat.room),
(chat,messages)=>({chat,messages})) // create a new object, using the inner observable and the mapped one
.map(data =>
data.messages.map((message: any) => {
// Here data.chat has the chat information
return message;
})
);
For more information about the switchMap
operator check here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With