正常使用select效果function/eventChannels
Use select effect in normal function/eventChannels
我正在使用 redux sagas 事件通道。我有一个特定的用例,我想在事件被触发时获取 getStore 状态,因为 eventChannel 不是生成器函数我无法使用 redux sagas 的 select 效果。还有其他方法可以实现吗?
我也曾尝试导入商店并使用 store.getState()
但我变得不确定,因为在商店初始化之前导入了 saga 文件。
function subscribe(socket) {
return eventChannel(emit => {
socket.subscribe(listenerTopic, {qos: 1});
socket.on('reconnection', () => {
// i need to fetch latest unread message, for that i need to get timestamp
// for last message received which is in store so the command will be to
// send sinceDate i.e last message timestamp and untilDate that is currentDate
})
}
一种可能的解决方案是使用 redux-saga
频道功能。
这个想法是每次发生套接字事件时将事件推送到通道中。同时,您还有另一个 saga 监听通道上的事件并做出相应的反应。因为听力部分是saga,所以可以使用常见的saga效果如select
.
您的代码可能如下所示:
import { channel } from 'redux-saga'
import { select } from 'redux-saga/effects'
const socketChannel = channel()
function subscribe(socket) {
return eventChannel(emit => {
socket.on('reconnection', () => {
socketChannel.put({ type: RECONNECTION })
})
})
}
export function* watchSocketChannel() {
while (true) {
const action = yield take(socketChannel)
if (action.type === RECONNECTION) {
const lastMessageTimestamp = yield select(selectors.getLastMessageTimestamp)
...
}
}
}
希望对您有所帮助。
我正在使用 redux sagas 事件通道。我有一个特定的用例,我想在事件被触发时获取 getStore 状态,因为 eventChannel 不是生成器函数我无法使用 redux sagas 的 select 效果。还有其他方法可以实现吗?
我也曾尝试导入商店并使用 store.getState()
但我变得不确定,因为在商店初始化之前导入了 saga 文件。
function subscribe(socket) {
return eventChannel(emit => {
socket.subscribe(listenerTopic, {qos: 1});
socket.on('reconnection', () => {
// i need to fetch latest unread message, for that i need to get timestamp
// for last message received which is in store so the command will be to
// send sinceDate i.e last message timestamp and untilDate that is currentDate
})
}
一种可能的解决方案是使用 redux-saga
频道功能。
这个想法是每次发生套接字事件时将事件推送到通道中。同时,您还有另一个 saga 监听通道上的事件并做出相应的反应。因为听力部分是saga,所以可以使用常见的saga效果如select
.
您的代码可能如下所示:
import { channel } from 'redux-saga'
import { select } from 'redux-saga/effects'
const socketChannel = channel()
function subscribe(socket) {
return eventChannel(emit => {
socket.on('reconnection', () => {
socketChannel.put({ type: RECONNECTION })
})
})
}
export function* watchSocketChannel() {
while (true) {
const action = yield take(socketChannel)
if (action.type === RECONNECTION) {
const lastMessageTimestamp = yield select(selectors.getLastMessageTimestamp)
...
}
}
}
希望对您有所帮助。