Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import { takeLatest, put, call, take, fork, cancel } from 'redux-saga/effects';
import { eventChannel, EventChannel, Task } from 'redux-saga';
import { hackernewsAPI } from '@/services/hackernews';
import {
fetchNewPosts,
fetchNewPostsSuccess,
fetchNewPostsFailure,
fetchTopPosts,
fetchTopPostsSuccess,
fetchTopPostsFailure,
loadNewPostItem,
loadTopPostItem,
updateNewPostItem,
updateTopPostItem,
} from '@/store/reducers/posts/posts-actions';
import type { HNItem, HNItemId } from '@/types/hackernews';
import { paginateArray } from '@/helpers/paginationHelper';
import { getErrorMessage } from '@/helpers/errorHelper';
import { getPostsPerPage } from '@/helpers/configHelper';
const POSTS_PER_PAGE = getPostsPerPage();
let newPostTasks: Task[] = [];
let topPostTasks: Task[] = [];
function createItemChannel(itemId: HNItemId): EventChannel<HNItem> {
return eventChannel((emitter) => {
const unsubscribe = hackernewsAPI.subscribeToItem(itemId, (item) => {
if (item) {
emitter(item);
}
});
return unsubscribe;
});
}
function* handleFetchNewPosts(action: ReturnType<typeof fetchNewPosts>): Generator {
try {
for (const task of newPostTasks) {
yield cancel(task);
}
newPostTasks = [];
const page = action.payload?.page || 1;
const allPostIds: HNItemId[] = yield call(hackernewsAPI.getNewPostIds);
const { paginatedItems: pagePostIds, totalPages } = paginateArray(allPostIds, page, POSTS_PER_PAGE);
const items = pagePostIds.map((id) => ({ id }));
yield put(
fetchNewPostsSuccess({
items,
page,
totalPages,
})
);
for (let i = 0; i < pagePostIds.length; i++) {
const item: HNItem = yield call(hackernewsAPI.getItem, pagePostIds[i]);
if (item) {
yield put(loadNewPostItem({ item, index: i }));
}
const task: Task = yield fork(watchNewPostItem, pagePostIds[i], i);
newPostTasks.push(task);
}
} catch (error) {
const errorMessage = getErrorMessage(error, 'Failed to fetch new posts');
yield put(fetchNewPostsFailure(errorMessage));
}
}
function* handleFetchTopPosts(action: ReturnType<typeof fetchTopPosts>): Generator {
try {
for (const task of topPostTasks) {
yield cancel(task);
}
topPostTasks = [];
const page = action.payload?.page || 1;
const allPostIds: HNItemId[] = yield call(hackernewsAPI.getTopPostIds);
const { paginatedItems: pagePostIds, totalPages } = paginateArray(allPostIds, page, POSTS_PER_PAGE);
const items = pagePostIds.map((id) => ({ id }));
yield put(
fetchTopPostsSuccess({
items,
page,
totalPages,
})
);
for (let i = 0; i < pagePostIds.length; i++) {
const item: HNItem = yield call(hackernewsAPI.getItem, pagePostIds[i]);
if (item) {
yield put(loadTopPostItem({ item, index: i }));
}
const task: Task = yield fork(watchTopPostItem, pagePostIds[i], i);
topPostTasks.push(task);
}
} catch (error) {
const errorMessage = getErrorMessage(error, 'Failed to fetch top posts');
yield put(fetchTopPostsFailure(errorMessage));
}
}
function* watchNewPostItem(itemId: HNItemId, index: number): Generator {
const channel: EventChannel<HNItem> = yield call(createItemChannel, itemId);
try {
while (true) {
const item: HNItem = yield take(channel);
yield put(updateNewPostItem({ item, index }));
}
} finally {
channel.close();
}
}
function* watchTopPostItem(itemId: HNItemId, index: number): Generator {
const channel: EventChannel<HNItem> = yield call(createItemChannel, itemId);
try {
while (true) {
const item: HNItem = yield take(channel);
yield put(updateTopPostItem({ item, index }));
}
} finally {
channel.close();
}
}
export default function* postsSaga(): Generator {
yield takeLatest(fetchNewPosts.type, handleFetchNewPosts);
yield takeLatest(fetchTopPosts.type, handleFetchTopPosts);
}
|