Fixes for user and chat reducers

This commit is contained in:
Alexander Zinchuk 2021-12-14 22:40:37 +04:00
parent c3d55e58b5
commit 1285499e96
2 changed files with 42 additions and 22 deletions

View File

@ -2,7 +2,7 @@ import { GlobalState } from '../../global/types';
import { ApiChat, ApiPhoto } from '../../api/types'; import { ApiChat, ApiPhoto } from '../../api/types';
import { ARCHIVED_FOLDER_ID } from '../../config'; import { ARCHIVED_FOLDER_ID } from '../../config';
import { mapValues, omit } from '../../util/iteratees'; import { omit } from '../../util/iteratees';
import { selectChatListType } from '../selectors'; import { selectChatListType } from '../selectors';
export function replaceChatListIds( export function replaceChatListIds(
@ -54,6 +54,9 @@ export function updateChat(
const { byId } = global.chats; const { byId } = global.chats;
const updatedChat = getUpdatedChat(global, chatId, chatUpdate, photo); const updatedChat = getUpdatedChat(global, chatId, chatUpdate, photo);
if (!updatedChat) {
return global;
}
return replaceChats(global, { return replaceChats(global, {
...byId, ...byId,
@ -62,9 +65,14 @@ export function updateChat(
} }
export function updateChats(global: GlobalState, newById: Record<string, ApiChat>): GlobalState { export function updateChats(global: GlobalState, newById: Record<string, ApiChat>): GlobalState {
const updatedById = mapValues(newById, (chat, id) => { const updatedById = Object.keys(newById).reduce((acc: Record<string, ApiChat>, id) => {
return getUpdatedChat(global, id, chat); const updatedChat = getUpdatedChat(global, id, newById[id]);
}); if (updatedChat) {
acc[id] = updatedChat;
}
return acc;
}, {});
global = replaceChats(global, { global = replaceChats(global, {
...global.chats.byId, ...global.chats.byId,
@ -77,20 +85,22 @@ export function updateChats(global: GlobalState, newById: Record<string, ApiChat
// @optimization Allows to avoid redundant updates which cause a lot of renders // @optimization Allows to avoid redundant updates which cause a lot of renders
export function addChats(global: GlobalState, newById: Record<string, ApiChat>): GlobalState { export function addChats(global: GlobalState, newById: Record<string, ApiChat>): GlobalState {
const { byId } = global.chats; const { byId } = global.chats;
let isAdded = false; let isUpdated = false;
const addedById = Object.keys(newById).reduce<Record<string, ApiChat>>((acc, id) => { const addedById = Object.keys(newById).reduce<Record<string, ApiChat>>((acc, id) => {
if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) { if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) {
acc[id] = getUpdatedChat(global, id, newById[id]); const updatedChat = getUpdatedChat(global, id, newById[id]);
if (updatedChat) {
if (!isAdded) { acc[id] = updatedChat;
isAdded = true; if (!isUpdated) {
isUpdated = true;
}
} }
} }
return acc; return acc;
}, {}); }, {});
if (!isAdded) { if (!isUpdated) {
return global; return global;
} }
@ -116,7 +126,7 @@ function getUpdatedChat(
}; };
if (!updatedChat.id || !updatedChat.type) { if (!updatedChat.id || !updatedChat.type) {
return updatedChat; return undefined;
} }
return updatedChat; return updatedChat;

View File

@ -1,7 +1,7 @@
import { GlobalState } from '../../global/types'; import { GlobalState } from '../../global/types';
import { ApiUser, ApiUserStatus } from '../../api/types'; import { ApiUser, ApiUserStatus } from '../../api/types';
import { mapValues, omit, pick } from '../../util/iteratees'; import { omit, pick } from '../../util/iteratees';
import { MEMO_EMPTY_ARRAY } from '../../util/memo'; import { MEMO_EMPTY_ARRAY } from '../../util/memo';
export function replaceUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState { export function replaceUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
@ -40,6 +40,9 @@ export function updateUser(global: GlobalState, userId: string, userUpdate: Part
const { byId } = global.users; const { byId } = global.users;
const updatedUser = getUpdatedUser(global, userId, userUpdate); const updatedUser = getUpdatedUser(global, userId, userUpdate);
if (!updatedUser) {
return global;
}
global = updateContactList(global, [updatedUser]); global = updateContactList(global, [updatedUser]);
@ -50,9 +53,14 @@ export function updateUser(global: GlobalState, userId: string, userUpdate: Part
} }
export function updateUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState { export function updateUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
const updatedById = mapValues(newById, (user, id) => { const updatedById = Object.keys(newById).reduce((acc: Record<string, ApiUser>, id) => {
return getUpdatedUser(global, id, user); const updatedUser = getUpdatedUser(global, id, newById[id]);
}); if (updatedUser) {
acc[id] = updatedUser;
}
return acc;
}, {});
global = replaceUsers(global, { global = replaceUsers(global, {
...global.users.byId, ...global.users.byId,
@ -67,20 +75,22 @@ export function updateUsers(global: GlobalState, newById: Record<string, ApiUser
// @optimization Allows to avoid redundant updates which cause a lot of renders // @optimization Allows to avoid redundant updates which cause a lot of renders
export function addUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState { export function addUsers(global: GlobalState, newById: Record<string, ApiUser>): GlobalState {
const { byId } = global.users; const { byId } = global.users;
let isAdded = false; let isUpdated = false;
const addedById = Object.keys(newById).reduce<Record<string, ApiUser>>((acc, id) => { const addedById = Object.keys(newById).reduce<Record<string, ApiUser>>((acc, id) => {
if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) { if (!byId[id] || (byId[id].isMin && !newById[id].isMin)) {
acc[id] = getUpdatedUser(global, id, newById[id]); const updatedUser = getUpdatedUser(global, id, newById[id]);
if (updatedUser) {
if (!isAdded) { acc[id] = updatedUser;
isAdded = true; if (!isUpdated) {
isUpdated = true;
}
} }
} }
return acc; return acc;
}, {}); }, {});
if (!isAdded) { if (!isUpdated) {
return global; return global;
} }
@ -106,7 +116,7 @@ function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial
}; };
if (!updatedUser.id || !updatedUser.type) { if (!updatedUser.id || !updatedUser.type) {
return user; return undefined;
} }
return updatedUser; return updatedUser;