链可观察和调用空隙法

发布于 2025-02-05 19:32:09 字数 1493 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-02-12 19:32:09

您的tap实际上被调用。您应该能够在回调函数中console.log。关于为什么不调用的问题

this.messageService.createHubConnection(this.currentUser, other)

是因为从中返回的可观察到的问题没有被订阅。

解决方案

以下列出的解决方案工作。评估每个人并选择一个。

  1. you .subscribetap回调中。 不是我建议,但这是一个有效的解决方案。

      //您的抽头逻辑应如下所示
    点击((其他:String)=> {
    
      this.messageservice.createhubConnection(this.currentuser,other).subscribe();
      //您可能要添加未取消标准逻辑
    
    })))
     
  2. flatten “ nofollow noreferrer”>高级订购以某种方式观察。对于您的用例,我会使用 mapto 操作员:

      this.currentuser
    .currentuser $
    。管道(
    switchmap((用户:iuser)=> {
    this.currentuser =用户;
    返回this.threadUsername.getUsername();
    }),
    switchmap((其他:string)=> {
    返回this.messageservice //

Your tap callback is getting called actually. You should be able to console.log within the callback function. The problem as to why

this.messageService.createHubConnection(this.currentUser, other)

is 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.

  1. You .subscribe within the tap callback. Not that I recommend, but it's a valid solution.

    // your tap logic should look something like below
    tap((other: string) => {
    
      this.messageService.createHubConnection(this.currentUser, other).subscribe();
      // you might want to add unsubscription logic
    
    }))
    
  2. Flatten higher-order observables in some way. For your use-case, I would do something like the following, using the mapTo operator:

    this.currentUser
      .currentUser$
      .pipe(
        switchMap((user: IUser) => {
          this.currentUser = user;
          return this.threadUsername.getUsername();
        }),
        switchMap((other: string) => {
          return this.messageService                      // ???? return observable to be subscribed down the chain...
            .createHubConnection(this.currentUser, other) // ???? that creates hub connection from source observable value...
            .pipe(
              mapTo(other)                                // ???? after which, return the very same source observable value just like what tap does
            );
        })
      )
      .subscribe(res => this.groupMessages());
    

    The only downside to this is that the execution of this.groupMessages() will have to wait until this.messageService.createHubConnection() completes. Nevertheless, it does the job without introducing problems with the first approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文