Prevent unnecessary store emits (#6285)
* Remove unnecessary store emits * Drastically reduce number of emitted changes
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
83f819451a
Коммит
2d22fb5652
@@ -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;
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Ссылка в новой задаче
Block a user