diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-05-01 10:49:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-01 10:49:34 -0400 |
commit | 2d22fb5652e547b4fe169dee2ea9cd14f747a485 (patch) | |
tree | a27e0a814ca2563ca9d20970201e397aa88afc6f | |
parent | 83f819451a80a767170b927eb2f0d5ed63f03239 (diff) | |
download | chat-2d22fb5652e547b4fe169dee2ea9cd14f747a485.tar.gz chat-2d22fb5652e547b4fe169dee2ea9cd14f747a485.tar.bz2 chat-2d22fb5652e547b4fe169dee2ea9cd14f747a485.zip |
Prevent unnecessary store emits (#6285)
* Remove unnecessary store emits
* Drastically reduce number of emitted changes
-rw-r--r-- | webapp/actions/channel_actions.jsx | 2 | ||||
-rw-r--r-- | webapp/actions/global_actions.jsx | 11 | ||||
-rw-r--r-- | webapp/actions/user_actions.jsx | 5 | ||||
-rw-r--r-- | webapp/components/needs_team/needs_team.jsx | 2 | ||||
-rw-r--r-- | webapp/stores/channel_store.jsx | 79 | ||||
-rw-r--r-- | webapp/stores/team_store.jsx | 35 |
6 files changed, 57 insertions, 77 deletions
diff --git a/webapp/actions/channel_actions.jsx b/webapp/actions/channel_actions.jsx index 79dfe3212..6acb05454 100644 --- a/webapp/actions/channel_actions.jsx +++ b/webapp/actions/channel_actions.jsx @@ -90,7 +90,7 @@ export function executeCommand(message, args, success, error) { export function setChannelAsRead(channelIdParam) { const channelId = channelIdParam || ChannelStore.getCurrentId(); viewChannel(channelId)(dispatch, getState); - ChannelStore.resetCounts(channelId); + ChannelStore.resetCounts([channelId]); ChannelStore.emitChange(); if (channelId === ChannelStore.getCurrentId()) { ChannelStore.emitLastViewed(Number.MAX_VALUE, false); diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx index 1dd5d6952..97d3b1071 100644 --- a/webapp/actions/global_actions.jsx +++ b/webapp/actions/global_actions.jsx @@ -36,7 +36,7 @@ import store from 'stores/redux_store.jsx'; const dispatch = store.dispatch; const getState = store.getState; import {removeUserFromTeam} from 'mattermost-redux/actions/teams'; -import {viewChannel, getChannelStats, getChannelMember} from 'mattermost-redux/actions/channels'; +import {viewChannel, getChannelStats, getMyChannelMember} from 'mattermost-redux/actions/channels'; export function emitChannelClickEvent(channel) { function userVisitedFakeChannel(chan, success, fail) { @@ -53,23 +53,22 @@ export function emitChannelClickEvent(channel) { } function switchToChannel(chan) { const channelMember = ChannelStore.getMyMember(chan.id); - const getMyChannelMemberPromise = getChannelMember(chan.id, UserStore.getCurrentId())(dispatch, getState); + const getMyChannelMemberPromise = getMyChannelMember(chan.id)(dispatch, getState); const oldChannelId = ChannelStore.getCurrentId(); getMyChannelMemberPromise.then(() => { getChannelStats(chan.id)(dispatch, getState); viewChannel(chan.id)(dispatch, getState); loadPosts(chan.id); + + // Mark previous and next channel as read + ChannelStore.resetCounts([chan.id, oldChannelId]); }); // Subtract mentions for the team const {msgs, mentions} = ChannelStore.getUnreadCounts()[chan.id] || {msgs: 0, mentions: 0}; TeamStore.subtractUnread(chan.team_id, msgs, mentions); - // Mark previous and next channel as read - ChannelStore.resetCounts(oldChannelId); - ChannelStore.resetCounts(chan.id); - BrowserStore.setGlobalItem(chan.team_id, chan.id); loadProfilesForSidebar(); diff --git a/webapp/actions/user_actions.jsx b/webapp/actions/user_actions.jsx index eacb0df80..8dfc819b9 100644 --- a/webapp/actions/user_actions.jsx +++ b/webapp/actions/user_actions.jsx @@ -24,6 +24,8 @@ import store from 'stores/redux_store.jsx'; const dispatch = store.dispatch; const getState = store.getState; +import * as Selectors from 'mattermost-redux/selectors/entities/users'; + import { getProfiles, getProfilesInChannel, @@ -239,7 +241,8 @@ function populateDMChannelsWithProfiles(userIds) { for (let i = 0; i < userIds.length; i++) { const channelName = getDirectChannelName(currentUserId, userIds[i]); const channel = ChannelStore.getByName(channelName); - if (channel) { + const profilesInChannel = Selectors.getUserIdsInChannels(getState())[channel.id] || new Set(); + if (channel && !profilesInChannel.has(userIds[i])) { UserStore.saveUserIdInChannel(channel.id, userIds[i]); } } diff --git a/webapp/components/needs_team/needs_team.jsx b/webapp/components/needs_team/needs_team.jsx index 2ae3cbeec..6c18ee4e4 100644 --- a/webapp/components/needs_team/needs_team.jsx +++ b/webapp/components/needs_team/needs_team.jsx @@ -119,7 +119,7 @@ export default class NeedsTeam extends React.Component { window.isActive = true; $(window).on('focus', () => { this.props.actions.viewChannel(ChannelStore.getCurrentId()); - ChannelStore.resetCounts(ChannelStore.getCurrentId()); + ChannelStore.resetCounts([ChannelStore.getCurrentId()]); ChannelStore.emitChange(); window.isActive = true; diff --git a/webapp/stores/channel_store.jsx b/webapp/stores/channel_store.jsx index d33f3ac9a..1440c1590 100644 --- a/webapp/stores/channel_store.jsx +++ b/webapp/stores/channel_store.jsx @@ -31,24 +31,31 @@ class ChannelStoreClass extends EventEmitter { store.subscribe(() => { const newEntities = store.getState().entities.channels; + let doEmit = false; - if (newEntities.currentTeamId !== this.entities.currentChannelId) { - this.emitChange(); + if (newEntities.currentChannelId !== this.entities.currentChannelId) { + doEmit = true; } if (newEntities.channels !== this.entities.channels) { - this.emitChange(); + this.setUnreadCountsByChannels(Object.values(newEntities.channels)); + doEmit = true; } if (newEntities.myMembers !== this.entities.myMembers) { this.setUnreadCountsByMembers(Object.values(newEntities.myMembers)); - this.emitChange(); + this.emitLastViewed(); + doEmit = true; } if (newEntities.membersInChannel !== this.entities.membersInChannel) { - this.emitChange(); + doEmit = true; } if (newEntities.stats !== this.entities.stats) { this.emitStatsChange(); } + if (doEmit) { + this.emitChange(); + } + this.entities = newEntities; }); } @@ -155,24 +162,21 @@ class ChannelStoreClass extends EventEmitter { }); } - resetCounts(id) { - const members = Object.assign({}, this.getMyMembers()); - for (const cmid in members) { - if (!members.hasOwnProperty(cmid)) { - continue; - } - const member = {...members[cmid]}; - if (member.channel_id === id) { - const channel = this.get(id); - if (channel) { - member.msg_count = channel.total_msg_count; - member.mention_count = 0; - this.storeMyChannelMember(member); - this.setUnreadCountByChannel(id); - } - break; + resetCounts(ids) { + const membersToStore = []; + ids.forEach((id) => { + const member = this.getMyMember(id); + const channel = this.get(id); + if (member && channel) { + const memberToStore = {...member}; + memberToStore.msg_count = channel.total_msg_count; + memberToStore.mention_count = 0; + membersToStore.push(memberToStore); + this.setUnreadCountByChannel(id); } - } + }); + + this.storeMyChannelMembersList(membersToStore); } getCurrentId() { @@ -458,7 +462,7 @@ class ChannelStoreClass extends EventEmitter { }); if (markRead) { - this.resetCounts(id); + this.resetCounts([id]); } else { this.unreadCounts[id].msgs++; } @@ -490,13 +494,11 @@ var ChannelStore = new ChannelStoreClass(); ChannelStore.dispatchToken = AppDispatcher.register((payload) => { var action = payload.action; - var currentId; switch (action.type) { case ActionTypes.CLICK_CHANNEL: ChannelStore.setCurrentId(action.id); ChannelStore.setPostMode(ChannelStore.POST_MODE_CHANNEL); - ChannelStore.emitChange(); break; case ActionTypes.RECEIVED_FOCUSED_POST: { @@ -509,8 +511,6 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => { case ActionTypes.RECEIVED_CHANNELS: ChannelStore.storeChannels(action.channels); - ChannelStore.setUnreadCountsByChannels(action.channels); - ChannelStore.emitChange(); break; case ActionTypes.RECEIVED_CHANNEL: @@ -518,41 +518,19 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => { if (action.member) { ChannelStore.storeMyChannelMember(action.member); } - currentId = ChannelStore.getCurrentId(); - if (currentId && window.isActive) { - ChannelStore.resetCounts(currentId); - } - ChannelStore.setUnreadCountByChannel(action.channel.id); - ChannelStore.emitChange(); break; case ActionTypes.RECEIVED_MY_CHANNEL_MEMBERS: ChannelStore.storeMyChannelMembersList(action.members); - currentId = ChannelStore.getCurrentId(); - if (currentId && window.isActive) { - ChannelStore.resetCounts(currentId); - } - ChannelStore.setUnreadCountsByMembers(action.members); - ChannelStore.emitChange(); - ChannelStore.emitLastViewed(); break; case ActionTypes.RECEIVED_CHANNEL_MEMBER: ChannelStore.storeMyChannelMember(action.member); - currentId = ChannelStore.getCurrentId(); - if (currentId && window.isActive) { - ChannelStore.resetCounts(currentId); - } - ChannelStore.setUnreadCountsByCurrentMembers(); - ChannelStore.emitChange(); - ChannelStore.emitLastViewed(); break; case ActionTypes.RECEIVED_MORE_CHANNELS: ChannelStore.storeMoreChannels(action.channels); - ChannelStore.emitChange(); break; case ActionTypes.RECEIVED_MEMBERS_IN_CHANNEL: ChannelStore.saveMembersInChannel(action.channel_id, action.channel_members); - ChannelStore.emitChange(); break; case ActionTypes.RECEIVED_CHANNEL_STATS: store.dispatch({ @@ -577,18 +555,15 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => { if (TeamStore.getCurrentId() === teamId || teamId === '') { ChannelStore.incrementMentionsIfNeeded(id, action.websocketMessageProps); ChannelStore.incrementMessages(id, markRead); - ChannelStore.emitChange(); } break; case ActionTypes.CREATE_POST: ChannelStore.incrementMessages(action.post.channel_id, true); - ChannelStore.emitChange(); break; case ActionTypes.CREATE_COMMENT: ChannelStore.incrementMessages(action.post.channel_id, true); - ChannelStore.emitChange(); break; default: diff --git a/webapp/stores/team_store.jsx b/webapp/stores/team_store.jsx index ac48e4352..f017c411e 100644 --- a/webapp/stores/team_store.jsx +++ b/webapp/stores/team_store.jsx @@ -31,24 +31,29 @@ class TeamStoreClass extends EventEmitter { store.subscribe(() => { const newEntities = store.getState().entities.teams; + let doEmit = false; if (newEntities.currentTeamId !== this.entities.currentTeamId) { - this.emitChange(); + doEmit = true; } if (newEntities.teams !== this.entities.teams) { - this.emitChange(); + doEmit = true; } if (newEntities.myMembers !== this.entities.myMembers) { - this.emitChange(); + doEmit = true; this.emitUnreadChange(); } if (newEntities.membersInTeam !== this.entities.membersInTeam) { - this.emitChange(); + doEmit = true; } if (newEntities.stats !== this.entities.stats) { this.emitStatsChange(); } + if (doEmit) { + this.emitChange(); + } + this.entities = newEntities; }); } @@ -343,6 +348,11 @@ class TeamStoreClass extends EventEmitter { member = Object.assign({}, member); member.msg_count -= (totalMsgCount - channelMember.msg_count); member.mention_count -= channelMember.mention_count; + + store.dispatch({ + type: TeamTypes.RECEIVED_MY_TEAM_MEMBER, + data: member + }); } } @@ -355,6 +365,11 @@ class TeamStoreClass extends EventEmitter { member = Object.assign({}, member); member.msg_count = (msgCount > 0) ? msgCount : 0; member.mention_count = (mentionCount > 0) ? mentionCount : 0; + + store.dispatch({ + type: TeamTypes.RECEIVED_MY_TEAM_MEMBER, + data: member + }); } } @@ -399,49 +414,38 @@ TeamStore.dispatchToken = AppDispatcher.register((payload) => { switch (action.type) { case ActionTypes.RECEIVED_MY_TEAM: TeamStore.saveMyTeam(action.team); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_TEAM: TeamStore.saveTeam(action.team); - TeamStore.emitChange(); break; case ActionTypes.CREATED_TEAM: TeamStore.saveTeam(action.team); TeamStore.appendMyTeamMember(action.member); - TeamStore.emitChange(); break; case ActionTypes.UPDATE_TEAM: TeamStore.saveTeam(action.team); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_ALL_TEAMS: TeamStore.saveTeams(action.teams); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_MY_TEAM_MEMBERS: TeamStore.saveMyTeamMembers(action.team_members); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_MY_TEAMS_UNREAD: TeamStore.saveMyTeamMembersUnread(action.team_members); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_ALL_TEAM_LISTINGS: TeamStore.saveTeamListings(action.teams); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_MEMBERS_IN_TEAM: TeamStore.saveMembersInTeam(action.team_id, action.team_members); - TeamStore.emitChange(); break; case ActionTypes.RECEIVED_TEAM_STATS: TeamStore.saveStats(action.team_id, action.stats); - TeamStore.emitStatsChange(); break; case ActionTypes.CLICK_CHANNEL: if (action.channelMember) { TeamStore.updateUnreadCount(action.team_id, action.total_msg_count, action.channelMember); - TeamStore.emitUnreadChange(); } break; case ActionTypes.RECEIVED_POST: @@ -453,7 +457,6 @@ TeamStore.dispatchToken = AppDispatcher.register((payload) => { if (id && TeamStore.getCurrentId() !== id) { TeamStore.incrementMessages(id, action.post.channel_id); TeamStore.incrementMentionsIfNeeded(id, action.websocketMessageProps); - TeamStore.emitChange(); } break; default: |