我对将Firebase与ngrx / store集成非常困惑。由于Firebase基于套接字" live"更新,因此每当我更新Firebase时,商店数据都已与Firebase数据同步。问题是,我不知道如何收听Firebase更新并触发带效果/存储的副作用,以便在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函数。它基本上使用list来获取(跟踪)firebase上的新更新,并在我们知道自最近5秒以来发生了什么新更新时删除firebase上的跟踪数据。我想删除这个逻辑,只是直接听firebase更新并触发我的副作用。
答案 0 :(得分:1)
在理论中,这应该触发连续更新,并在uiState
更改时切换到新的更新流。
@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");