Prevent unnecessary store emits (#6285)

* Remove unnecessary store emits

* Drastically reduce number of emitted changes
Этот коммит содержится в:
Joram Wilander
2017-05-01 10:49:34 -04:00
коммит произвёл GitHub
родитель 83f819451a
Коммит 2d22fb5652
6 изменённых файлов: 57 добавлений и 77 удалений

Просмотреть файл

@@ -90,7 +90,7 @@ export function executeCommand(message, args, success, error) {
export function setChannelAsRead(channelIdParam) { export function setChannelAsRead(channelIdParam) {
const channelId = channelIdParam || ChannelStore.getCurrentId(); const channelId = channelIdParam || ChannelStore.getCurrentId();
viewChannel(channelId)(dispatch, getState); viewChannel(channelId)(dispatch, getState);
ChannelStore.resetCounts(channelId); ChannelStore.resetCounts([channelId]);
ChannelStore.emitChange(); ChannelStore.emitChange();
if (channelId === ChannelStore.getCurrentId()) { if (channelId === ChannelStore.getCurrentId()) {
ChannelStore.emitLastViewed(Number.MAX_VALUE, false); ChannelStore.emitLastViewed(Number.MAX_VALUE, false);

Просмотреть файл

@@ -36,7 +36,7 @@ import store from 'stores/redux_store.jsx';
const dispatch = store.dispatch; const dispatch = store.dispatch;
const getState = store.getState; const getState = store.getState;
import {removeUserFromTeam} from 'mattermost-redux/actions/teams'; 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) { export function emitChannelClickEvent(channel) {
function userVisitedFakeChannel(chan, success, fail) { function userVisitedFakeChannel(chan, success, fail) {
@@ -53,23 +53,22 @@ export function emitChannelClickEvent(channel) {
} }
function switchToChannel(chan) { function switchToChannel(chan) {
const channelMember = ChannelStore.getMyMember(chan.id); 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(); const oldChannelId = ChannelStore.getCurrentId();
getMyChannelMemberPromise.then(() => { getMyChannelMemberPromise.then(() => {
getChannelStats(chan.id)(dispatch, getState); getChannelStats(chan.id)(dispatch, getState);
viewChannel(chan.id)(dispatch, getState); viewChannel(chan.id)(dispatch, getState);
loadPosts(chan.id); loadPosts(chan.id);
// Mark previous and next channel as read
ChannelStore.resetCounts([chan.id, oldChannelId]);
}); });
// Subtract mentions for the team // Subtract mentions for the team
const {msgs, mentions} = ChannelStore.getUnreadCounts()[chan.id] || {msgs: 0, mentions: 0}; const {msgs, mentions} = ChannelStore.getUnreadCounts()[chan.id] || {msgs: 0, mentions: 0};
TeamStore.subtractUnread(chan.team_id, msgs, mentions); 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); BrowserStore.setGlobalItem(chan.team_id, chan.id);
loadProfilesForSidebar(); loadProfilesForSidebar();

Просмотреть файл

@@ -24,6 +24,8 @@ import store from 'stores/redux_store.jsx';
const dispatch = store.dispatch; const dispatch = store.dispatch;
const getState = store.getState; const getState = store.getState;
import * as Selectors from 'mattermost-redux/selectors/entities/users';
import { import {
getProfiles, getProfiles,
getProfilesInChannel, getProfilesInChannel,
@@ -239,7 +241,8 @@ function populateDMChannelsWithProfiles(userIds) {
for (let i = 0; i < userIds.length; i++) { for (let i = 0; i < userIds.length; i++) {
const channelName = getDirectChannelName(currentUserId, userIds[i]); const channelName = getDirectChannelName(currentUserId, userIds[i]);
const channel = ChannelStore.getByName(channelName); 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]); UserStore.saveUserIdInChannel(channel.id, userIds[i]);
} }
} }

Просмотреть файл

@@ -119,7 +119,7 @@ export default class NeedsTeam extends React.Component {
window.isActive = true; window.isActive = true;
$(window).on('focus', () => { $(window).on('focus', () => {
this.props.actions.viewChannel(ChannelStore.getCurrentId()); this.props.actions.viewChannel(ChannelStore.getCurrentId());
ChannelStore.resetCounts(ChannelStore.getCurrentId()); ChannelStore.resetCounts([ChannelStore.getCurrentId()]);
ChannelStore.emitChange(); ChannelStore.emitChange();
window.isActive = true; window.isActive = true;

Просмотреть файл

@@ -31,24 +31,31 @@ class ChannelStoreClass extends EventEmitter {
store.subscribe(() => { store.subscribe(() => {
const newEntities = store.getState().entities.channels; const newEntities = store.getState().entities.channels;
let doEmit = false;
if (newEntities.currentTeamId !== this.entities.currentChannelId) { if (newEntities.currentChannelId !== this.entities.currentChannelId) {
this.emitChange(); doEmit = true;
} }
if (newEntities.channels !== this.entities.channels) { if (newEntities.channels !== this.entities.channels) {
this.emitChange(); this.setUnreadCountsByChannels(Object.values(newEntities.channels));
doEmit = true;
} }
if (newEntities.myMembers !== this.entities.myMembers) { if (newEntities.myMembers !== this.entities.myMembers) {
this.setUnreadCountsByMembers(Object.values(newEntities.myMembers)); this.setUnreadCountsByMembers(Object.values(newEntities.myMembers));
this.emitChange(); this.emitLastViewed();
doEmit = true;
} }
if (newEntities.membersInChannel !== this.entities.membersInChannel) { if (newEntities.membersInChannel !== this.entities.membersInChannel) {
this.emitChange(); doEmit = true;
} }
if (newEntities.stats !== this.entities.stats) { if (newEntities.stats !== this.entities.stats) {
this.emitStatsChange(); this.emitStatsChange();
} }
if (doEmit) {
this.emitChange();
}
this.entities = newEntities; this.entities = newEntities;
}); });
} }
@@ -155,24 +162,21 @@ class ChannelStoreClass extends EventEmitter {
}); });
} }
resetCounts(id) { resetCounts(ids) {
const members = Object.assign({}, this.getMyMembers()); const membersToStore = [];
for (const cmid in members) { ids.forEach((id) => {
if (!members.hasOwnProperty(cmid)) { const member = this.getMyMember(id);
continue; 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);
} }
const member = {...members[cmid]}; });
if (member.channel_id === id) {
const channel = this.get(id); this.storeMyChannelMembersList(membersToStore);
if (channel) {
member.msg_count = channel.total_msg_count;
member.mention_count = 0;
this.storeMyChannelMember(member);
this.setUnreadCountByChannel(id);
}
break;
}
}
} }
getCurrentId() { getCurrentId() {
@@ -458,7 +462,7 @@ class ChannelStoreClass extends EventEmitter {
}); });
if (markRead) { if (markRead) {
this.resetCounts(id); this.resetCounts([id]);
} else { } else {
this.unreadCounts[id].msgs++; this.unreadCounts[id].msgs++;
} }
@@ -490,13 +494,11 @@ var ChannelStore = new ChannelStoreClass();
ChannelStore.dispatchToken = AppDispatcher.register((payload) => { ChannelStore.dispatchToken = AppDispatcher.register((payload) => {
var action = payload.action; var action = payload.action;
var currentId;
switch (action.type) { switch (action.type) {
case ActionTypes.CLICK_CHANNEL: case ActionTypes.CLICK_CHANNEL:
ChannelStore.setCurrentId(action.id); ChannelStore.setCurrentId(action.id);
ChannelStore.setPostMode(ChannelStore.POST_MODE_CHANNEL); ChannelStore.setPostMode(ChannelStore.POST_MODE_CHANNEL);
ChannelStore.emitChange();
break; break;
case ActionTypes.RECEIVED_FOCUSED_POST: { case ActionTypes.RECEIVED_FOCUSED_POST: {
@@ -509,8 +511,6 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => {
case ActionTypes.RECEIVED_CHANNELS: case ActionTypes.RECEIVED_CHANNELS:
ChannelStore.storeChannels(action.channels); ChannelStore.storeChannels(action.channels);
ChannelStore.setUnreadCountsByChannels(action.channels);
ChannelStore.emitChange();
break; break;
case ActionTypes.RECEIVED_CHANNEL: case ActionTypes.RECEIVED_CHANNEL:
@@ -518,41 +518,19 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => {
if (action.member) { if (action.member) {
ChannelStore.storeMyChannelMember(action.member); ChannelStore.storeMyChannelMember(action.member);
} }
currentId = ChannelStore.getCurrentId();
if (currentId && window.isActive) {
ChannelStore.resetCounts(currentId);
}
ChannelStore.setUnreadCountByChannel(action.channel.id);
ChannelStore.emitChange();
break; break;
case ActionTypes.RECEIVED_MY_CHANNEL_MEMBERS: case ActionTypes.RECEIVED_MY_CHANNEL_MEMBERS:
ChannelStore.storeMyChannelMembersList(action.members); ChannelStore.storeMyChannelMembersList(action.members);
currentId = ChannelStore.getCurrentId();
if (currentId && window.isActive) {
ChannelStore.resetCounts(currentId);
}
ChannelStore.setUnreadCountsByMembers(action.members);
ChannelStore.emitChange();
ChannelStore.emitLastViewed();
break; break;
case ActionTypes.RECEIVED_CHANNEL_MEMBER: case ActionTypes.RECEIVED_CHANNEL_MEMBER:
ChannelStore.storeMyChannelMember(action.member); ChannelStore.storeMyChannelMember(action.member);
currentId = ChannelStore.getCurrentId();
if (currentId && window.isActive) {
ChannelStore.resetCounts(currentId);
}
ChannelStore.setUnreadCountsByCurrentMembers();
ChannelStore.emitChange();
ChannelStore.emitLastViewed();
break; break;
case ActionTypes.RECEIVED_MORE_CHANNELS: case ActionTypes.RECEIVED_MORE_CHANNELS:
ChannelStore.storeMoreChannels(action.channels); ChannelStore.storeMoreChannels(action.channels);
ChannelStore.emitChange();
break; break;
case ActionTypes.RECEIVED_MEMBERS_IN_CHANNEL: case ActionTypes.RECEIVED_MEMBERS_IN_CHANNEL:
ChannelStore.saveMembersInChannel(action.channel_id, action.channel_members); ChannelStore.saveMembersInChannel(action.channel_id, action.channel_members);
ChannelStore.emitChange();
break; break;
case ActionTypes.RECEIVED_CHANNEL_STATS: case ActionTypes.RECEIVED_CHANNEL_STATS:
store.dispatch({ store.dispatch({
@@ -577,18 +555,15 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => {
if (TeamStore.getCurrentId() === teamId || teamId === '') { if (TeamStore.getCurrentId() === teamId || teamId === '') {
ChannelStore.incrementMentionsIfNeeded(id, action.websocketMessageProps); ChannelStore.incrementMentionsIfNeeded(id, action.websocketMessageProps);
ChannelStore.incrementMessages(id, markRead); ChannelStore.incrementMessages(id, markRead);
ChannelStore.emitChange();
} }
break; break;
case ActionTypes.CREATE_POST: case ActionTypes.CREATE_POST:
ChannelStore.incrementMessages(action.post.channel_id, true); ChannelStore.incrementMessages(action.post.channel_id, true);
ChannelStore.emitChange();
break; break;
case ActionTypes.CREATE_COMMENT: case ActionTypes.CREATE_COMMENT:
ChannelStore.incrementMessages(action.post.channel_id, true); ChannelStore.incrementMessages(action.post.channel_id, true);
ChannelStore.emitChange();
break; break;
default: default:

Просмотреть файл

@@ -31,24 +31,29 @@ class TeamStoreClass extends EventEmitter {
store.subscribe(() => { store.subscribe(() => {
const newEntities = store.getState().entities.teams; const newEntities = store.getState().entities.teams;
let doEmit = false;
if (newEntities.currentTeamId !== this.entities.currentTeamId) { if (newEntities.currentTeamId !== this.entities.currentTeamId) {
this.emitChange(); doEmit = true;
} }
if (newEntities.teams !== this.entities.teams) { if (newEntities.teams !== this.entities.teams) {
this.emitChange(); doEmit = true;
} }
if (newEntities.myMembers !== this.entities.myMembers) { if (newEntities.myMembers !== this.entities.myMembers) {
this.emitChange(); doEmit = true;
this.emitUnreadChange(); this.emitUnreadChange();
} }
if (newEntities.membersInTeam !== this.entities.membersInTeam) { if (newEntities.membersInTeam !== this.entities.membersInTeam) {
this.emitChange(); doEmit = true;
} }
if (newEntities.stats !== this.entities.stats) { if (newEntities.stats !== this.entities.stats) {
this.emitStatsChange(); this.emitStatsChange();
} }
if (doEmit) {
this.emitChange();
}
this.entities = newEntities; this.entities = newEntities;
}); });
} }
@@ -343,6 +348,11 @@ class TeamStoreClass extends EventEmitter {
member = Object.assign({}, member); member = Object.assign({}, member);
member.msg_count -= (totalMsgCount - channelMember.msg_count); member.msg_count -= (totalMsgCount - channelMember.msg_count);
member.mention_count -= channelMember.mention_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 = Object.assign({}, member);
member.msg_count = (msgCount > 0) ? msgCount : 0; member.msg_count = (msgCount > 0) ? msgCount : 0;
member.mention_count = (mentionCount > 0) ? mentionCount : 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) { switch (action.type) {
case ActionTypes.RECEIVED_MY_TEAM: case ActionTypes.RECEIVED_MY_TEAM:
TeamStore.saveMyTeam(action.team); TeamStore.saveMyTeam(action.team);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_TEAM: case ActionTypes.RECEIVED_TEAM:
TeamStore.saveTeam(action.team); TeamStore.saveTeam(action.team);
TeamStore.emitChange();
break; break;
case ActionTypes.CREATED_TEAM: case ActionTypes.CREATED_TEAM:
TeamStore.saveTeam(action.team); TeamStore.saveTeam(action.team);
TeamStore.appendMyTeamMember(action.member); TeamStore.appendMyTeamMember(action.member);
TeamStore.emitChange();
break; break;
case ActionTypes.UPDATE_TEAM: case ActionTypes.UPDATE_TEAM:
TeamStore.saveTeam(action.team); TeamStore.saveTeam(action.team);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_ALL_TEAMS: case ActionTypes.RECEIVED_ALL_TEAMS:
TeamStore.saveTeams(action.teams); TeamStore.saveTeams(action.teams);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_MY_TEAM_MEMBERS: case ActionTypes.RECEIVED_MY_TEAM_MEMBERS:
TeamStore.saveMyTeamMembers(action.team_members); TeamStore.saveMyTeamMembers(action.team_members);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_MY_TEAMS_UNREAD: case ActionTypes.RECEIVED_MY_TEAMS_UNREAD:
TeamStore.saveMyTeamMembersUnread(action.team_members); TeamStore.saveMyTeamMembersUnread(action.team_members);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_ALL_TEAM_LISTINGS: case ActionTypes.RECEIVED_ALL_TEAM_LISTINGS:
TeamStore.saveTeamListings(action.teams); TeamStore.saveTeamListings(action.teams);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_MEMBERS_IN_TEAM: case ActionTypes.RECEIVED_MEMBERS_IN_TEAM:
TeamStore.saveMembersInTeam(action.team_id, action.team_members); TeamStore.saveMembersInTeam(action.team_id, action.team_members);
TeamStore.emitChange();
break; break;
case ActionTypes.RECEIVED_TEAM_STATS: case ActionTypes.RECEIVED_TEAM_STATS:
TeamStore.saveStats(action.team_id, action.stats); TeamStore.saveStats(action.team_id, action.stats);
TeamStore.emitStatsChange();
break; break;
case ActionTypes.CLICK_CHANNEL: case ActionTypes.CLICK_CHANNEL:
if (action.channelMember) { if (action.channelMember) {
TeamStore.updateUnreadCount(action.team_id, action.total_msg_count, action.channelMember); TeamStore.updateUnreadCount(action.team_id, action.total_msg_count, action.channelMember);
TeamStore.emitUnreadChange();
} }
break; break;
case ActionTypes.RECEIVED_POST: case ActionTypes.RECEIVED_POST:
@@ -453,7 +457,6 @@ TeamStore.dispatchToken = AppDispatcher.register((payload) => {
if (id && TeamStore.getCurrentId() !== id) { if (id && TeamStore.getCurrentId() !== id) {
TeamStore.incrementMessages(id, action.post.channel_id); TeamStore.incrementMessages(id, action.post.channel_id);
TeamStore.incrementMentionsIfNeeded(id, action.websocketMessageProps); TeamStore.incrementMentionsIfNeeded(id, action.websocketMessageProps);
TeamStore.emitChange();
} }
break; break;
default: default: