如何监听 Firebase 更新并触发 effect/store 副作用来更新 Store 状态?

How to listen to Firebase update and trigger effect/store side effect to update the Store state?

我对将 Firebase 与 ngrx/store 集成感到非常困惑。由于 Firebase 具有基于套接字的 "live" 更新,因此每当我更新 Firebase 时,商店数据已经与 Firebase 数据同步。问题是,我不知道如何收听 Firebase 更新并使用 effect/store 触发副作用以对 Firebase 和客户端存储进行更多更新。关于我应该如何解决这个问题的任何建议。现在,我必须使用长拉 Observable.interval(5000) 策略来触发服务函数来查询 Firebase 以查明是否发生了更新。代码如下:

    @Effect() newMessages$ = Observable.interval(5000)
        .withLatestFrom(this.store.select<UiState>("uiState"))
        .map(([any,uiState]) => uiState)
        .debug("We are querying Firebase to see if any update happened...")
        .filter(uiState => Boolean(uiState.userId))
        .switchMap(uiState => this.threadsService.loadNewMessagesForUser(uiState.userId)
          .take(1)
          .switchMap(data => this.threadsService.deleteMessagesQueuePerUser(uiState.userId).mapTo(data))
        )
        .debug("new messages received from server, and deleted temp record from Firebase")
        .withLatestFrom(this.store.select<UiState>("uiState"))
        .map(([unreadMessages, uiState]) => new NewMessagesReceivedAction({
          unreadMessages,
          currentThreadId: uiState.currentThreadId,
          currentUserId: uiState.userId
        }))

loadNewMessagesForUser 和 deleteMessagesQueuePerUser 是 firebase AngualrFire2 函数。它基本上使用列表来获取(跟踪)firebase 上的新更新,并在我们知道自最后 5 秒以来发生了什么新更新时删除 firebase 上的跟踪数据。我很想删除该逻辑,只听 firebase 直接更新并触发我的副作用。

理论中,这应该触发连续更新并在uiState改变时切换到new-update流。

@Effect() newMessages$ = this.store.select<UiState>("uiState")
        .debug("We are querying Firebase to see if any update happened...")
        .filter(uiState => Boolean(uiState.userId))
        .switchMap(uiState => this.threadsService.loadNewMessagesForUser(uiState.userId)
          .do(() => this.threadsService.deleteMessagesQueuePerUser(uiState.userId).subscribe())
          .map(unreadMessages => new NewMessagesReceivedAction({
              unreadMessages,
              currentThreadId: uiState.currentThreadId,
              currentUserId: uiState.userId
            })
          )
        )
        .debug("new messages received from server, and deleted temp record from Firebase");