在我当前的项目中,我正在处理firebase websocket订阅。不同的组件可以订阅不同的数据,例如,在每个ListItem
组件的订单列表中,通过分派componentDidMount
中的SUBSCRIBE
动作和通过分派componentWillUnmount
中的UNSUBSCRIBE
动作取消订阅,订阅了该特定项目的websocket“事件”。使用redux-saga处理订阅/取消订阅大量事件
我的传奇故事是这样的:
const subscriptions = {}
export function * subscribeLoop() {
while (true) {
const { path } = yield take(SUBSCRIBE)
subscriptions[path] = yield fork(subscription, path)
}
}
export function * unsubscribeLoop() {
while (true) {
const { path } = yield take(UNSUBSCRIBE)
yield cancel(subscriptions[path])
}
}
export function * subscription (path) {
let ref
try {
const updateChannel = channel()
ref = api.child(path)
ref.on('value', snapshot => {
updateChannel.put(snapshot.val())
})
while (true) {
const data = yield take(updateChannel)
yield put(handleUpdate(path, data))
}
} finally {
if (yield cancelled()) {
ref.off()
ref = null
}
}
}
我想这是不处理这个正确的方式 - 它确实是500个项目的清单上相当缓慢。
如何优化性能?
- 我是否还需要分叉?
- 我应该引入某种延迟来给线程一些空间来处理其他事情吗?
任何提示表示赞赏。