Use selection effect in normal function / eventChannels

I am using the sagas reduction event channel. I have a specific use case when I want the getStore state when the event is fired, because eventChannel is not a function of the generator, I cannot use the selected reduction sax effect. Any other way to achieve this?

I also tried to import the repository and use store.getState() , but I get undefined as the saga file is imported before the file is initialized.

 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 ie last message timestamp and untilDate that is currentDate }) } 
+7
redux-saga
source share
1 answer

One possible solution would be to use the redux-saga channel function. The idea would be to inject an event into the channel each time a socket event occurs. At the same time, you have another saga that listens to events on the channel and reacts accordingly. Since the listening part is a saga, you can use the general effects of the saga, such as select .

Perhaps your code looks like this:

 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) ... } } } 

Hope this helps you.

+1
source share

All Articles