Skip to content

Commit

Permalink
refactor(posts-saga): implement start polling function and conditions…
Browse files Browse the repository at this point in the history
… to stop polling when polling channel is no longer active (#2494)
  • Loading branch information
domw30 authored Dec 5, 2024
1 parent 8b39499 commit f638e66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/store/channels/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { userByMatrixIdSelector } from '../users/selectors';
import { rawChannel } from './selectors';
import cloneDeep from 'lodash/cloneDeep';
import { getHistory } from '../../lib/browser';
import { startPollingPosts } from '../posts/saga';

export const rawChannelSelector = (channelId) => (state) => {
return getDeepProperty(state, `normalized.channels['${channelId}']`, null);
Expand Down Expand Up @@ -74,6 +75,7 @@ export function* openConversation(conversationId) {
yield call(setActiveConversation, conversationId);
yield spawn(markConversationAsRead, conversationId);
yield call(resetConversationManagement);
yield call(startPollingPosts, conversationId);
}

export function* unreadCountUpdated(action) {
Expand Down
14 changes: 13 additions & 1 deletion src/store/posts/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,19 @@ function* pollPosts(action) {
} else {
yield put(setCount(count));
}
yield delay(5000);

const activeConversationId = yield select((state) => state.chat.activeConversationId);
// Only continue polling if we're still in the same conversation
if (activeConversationId === channelId) {
yield delay(5000);
yield put({ type: SagaActionTypes.PollPosts, payload: { channelId } });
}
}
}

export function* startPollingPosts(channelId: string) {
const channel = yield select(rawChannelSelector(channelId));
if (channel?.conversationStatus === ConversationStatus.CREATED) {
yield put({ type: SagaActionTypes.PollPosts, payload: { channelId } });
}
}
Expand Down

0 comments on commit f638e66

Please sign in to comment.