doFetchModBlockedList: don't block ui thread #7674

Merged
jessopb merged 2 commits from background-modBlockedList into master 2022-08-12 23:23:04 +02:00

View file

@ -1143,6 +1143,9 @@ export function doCommentModUnBlockAsModerator(commenterUri: string, creatorUri:
export function doFetchModBlockedList() { export function doFetchModBlockedList() {
return async (dispatch: Dispatch, getState: GetState) => { return async (dispatch: Dispatch, getState: GetState) => {
const LOOP_CHUNK_SIZE = 1;
const yieldThread = () => new Promise((resolve) => setTimeout(resolve));
const state = getState(); const state = getState();
const myChannels = selectMyChannelClaims(state); const myChannels = selectMyChannelClaims(state);
if (!myChannels) { if (!myChannels) {
@ -1172,7 +1175,7 @@ export function doFetchModBlockedList() {
}) })
) )
) )
.then((res) => { .then(async (res) => {
let personalBlockList = []; let personalBlockList = [];
let adminBlockList = []; let adminBlockList = [];
let moderatorBlockList = []; let moderatorBlockList = [];
@ -1185,15 +1188,21 @@ export function doFetchModBlockedList() {
const adminTimeoutMap = {}; const adminTimeoutMap = {};
const moderatorTimeoutMap = {}; const moderatorTimeoutMap = {};
const blockListsPerChannel = res.map((r) => r.value); const blockListsPerChannel = [];
blockListsPerChannel for (let i = 0; i < res.length; ++i) {
.sort((a, b) => { blockListsPerChannel.push(res[i].value);
return 1; if (i % 2 === 0) {
}) await yieldThread();
.forEach((channelBlockLists) => { }
const storeList = (fetchedList, blockedList, timeoutMap, blockedByMap) => { }
for (let i = 0; i < blockListsPerChannel.length; ++i) {
const storeList = async (fetchedList, blockedList, timeoutMap, blockedByMap) => {
if (fetchedList) { if (fetchedList) {
fetchedList.forEach((blockedChannel) => { for (let j = 0; j < fetchedList.length; ++j) {
const blockedChannel = fetchedList[j];
if (j > 0 && i % LOOP_CHUNK_SIZE === 0) {
await yieldThread();
}
if (blockedChannel.blocked_channel_name) { if (blockedChannel.blocked_channel_name) {
const channelUri = buildURI({ const channelUri = buildURI({
channelName: blockedChannel.blocked_channel_name, channelName: blockedChannel.blocked_channel_name,
@ -1227,24 +1236,28 @@ export function doFetchModBlockedList() {
} }
} }
} }
}); }
} }
}; };
const channelBlockLists = blockListsPerChannel[i];
const blocked_channels = channelBlockLists && channelBlockLists.blocked_channels; const blocked_channels = channelBlockLists && channelBlockLists.blocked_channels;
const globally_blocked_channels = channelBlockLists && channelBlockLists.globally_blocked_channels; const globally_blocked_channels = channelBlockLists && channelBlockLists.globally_blocked_channels;
const delegated_blocked_channels = channelBlockLists && channelBlockLists.delegated_blocked_channels; const delegated_blocked_channels = channelBlockLists && channelBlockLists.delegated_blocked_channels;
storeList(blocked_channels, personalBlockList, personalTimeoutMap); if (i > 0 && i % LOOP_CHUNK_SIZE === 0) {
storeList(globally_blocked_channels, adminBlockList, adminTimeoutMap); await yieldThread();
storeList( }
await storeList(blocked_channels, personalBlockList, personalTimeoutMap);
await storeList(globally_blocked_channels, adminBlockList, adminTimeoutMap);
await storeList(
delegated_blocked_channels, delegated_blocked_channels,
moderatorBlockList, moderatorBlockList,
moderatorTimeoutMap, moderatorTimeoutMap,
moderatorBlockListDelegatorsMap moderatorBlockListDelegatorsMap
); );
}); }
dispatch({ dispatch({
type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_COMPLETED, type: ACTIONS.COMMENT_MODERATION_BLOCK_LIST_COMPLETED,
data: { data: {