链可观察和调用空隙法
我有3个服务:当前的用户服务,其他用户服务,消息服务。 MessageService的方法具有需要的用户和其他用户名:
createHubConnection(user: IUser, otherUsername: string): void {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.hubUrl + "message?user=" + otherUsername, {
accessTokenFactory: () => user.token
})
.withAutomaticReconnect()
.build();
this.hubConnection.start().catch(error => console.log(error));
this.hubConnection.on("ReceiveMessageThread", messages => {
console.log("fsafsafsa")
this.messageThreadSource.next(messages);
});
this.hubConnection.on("NewMessage", message => {
this.messageThread$.pipe(take(1)).subscribe(messages => {
this.messageThreadSource.next([...messages, message])
})
})
}
毕竟我要调用方法groupMessages()
,我尝试执行此操作,但是它并没有进入Tap:
currentUser: IUser;
this.currentUser.currentUser$.pipe(
switchMap((user: IUser) => {
this.currentUser = user;
return this.threadUsername.getUsername();
}),
tap((other: string) => {
this.messageService.createHubConnection(this.currentUser, other) // It's not getting there
}))
.subscribe(res => this.groupMessages());
groupMessage()正在与一起工作。消息$
可观察到在createHubConnection()
方法中get的可观察到的。
I have 3 services: Current user service, OtherUsername service, MessageService. And MessageService has method that need's User and OtherUsername:
createHubConnection(user: IUser, otherUsername: string): void {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.hubUrl + "message?user=" + otherUsername, {
accessTokenFactory: () => user.token
})
.withAutomaticReconnect()
.build();
this.hubConnection.start().catch(error => console.log(error));
this.hubConnection.on("ReceiveMessageThread", messages => {
console.log("fsafsafsa")
this.messageThreadSource.next(messages);
});
this.hubConnection.on("NewMessage", message => {
this.messageThread$.pipe(take(1)).subscribe(messages => {
this.messageThreadSource.next([...messages, message])
})
})
}
After all that i want to call method groupMessages()
and i tried to do this but it's not going into tap:
currentUser: IUser;
this.currentUser.currentUser$.pipe(
switchMap((user: IUser) => {
this.currentUser = user;
return this.threadUsername.getUsername();
}),
tap((other: string) => {
this.messageService.createHubConnection(this.currentUser, other) // It's not getting there
}))
.subscribe(res => this.groupMessages());
groupMessage() is working with message$
observable that get's initialized in createHubConnection()
method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
tap
实际上被调用。您应该能够在回调函数中console.log
。关于为什么不调用的问题是因为从中返回的可观察到的问题没有被订阅。
解决方案
以下列出的解决方案工作。评估每个人并选择一个。
you
.subscribe
在tap
回调中。 不是我建议,但这是一个有效的解决方案。flatten “ nofollow noreferrer”>高级订购以某种方式观察。对于您的用例,我会使用
mapto
操作员:Your
tap
callback is getting called actually. You should be able toconsole.log
within the callback function. The problem as to whyis not invoked, is because the observable returned from it, is not being subscribed to.
Solutions
Either solution listed below works. Assess each and pick one.
You
.subscribe
within thetap
callback. Not that I recommend, but it's a valid solution.Flatten higher-order observables in some way. For your use-case, I would do something like the following, using the
mapTo
operator:The only downside to this is that the execution of
this.groupMessages()
will have to wait untilthis.messageService.createHubConnection()
completes. Nevertheless, it does the job without introducing problems with the first approach.