Handle messages with no content, improve prepend perf
This commit is contained in:
parent
50d735aaa3
commit
3c105c493b
File diff suppressed because one or more lines are too long
@ -82,7 +82,7 @@ describe('message reducer', () => {
|
|||||||
server: 'srv',
|
server: 'srv',
|
||||||
tab: '#chan1',
|
tab: '#chan1',
|
||||||
prepend: true,
|
prepend: true,
|
||||||
messages: [{ id: 1 }, { id: 2 }]
|
messages: [{ id: 1, date: new Date() }, { id: 2, date: new Date() }]
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(state).toMatchObject({
|
expect(state).toMatchObject({
|
||||||
|
@ -67,28 +67,43 @@ function isSameDay(d1, d2) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function reducerAddMessage(message, server, tab, state, prepend) {
|
function reducerPrependMessages(messages, server, tab, state) {
|
||||||
const messages = state[server][tab];
|
const msgs = [];
|
||||||
|
|
||||||
if (messages.length > 0) {
|
for (let i = 0; i < messages.length; i++) {
|
||||||
if (prepend) {
|
if (i > 0 && !isSameDay(messages[i - 1].date, messages[i].date)) {
|
||||||
const firstMessage = messages[0];
|
msgs.push(createDateMessage(messages[i].date));
|
||||||
if (firstMessage.date && !isSameDay(firstMessage.date, message.date)) {
|
}
|
||||||
messages.unshift(createDateMessage(firstMessage.date));
|
msgs.push(messages[i]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
const lastMessage = messages[messages.length - 1];
|
const m = state[server][tab];
|
||||||
if (lastMessage.date && !isSameDay(lastMessage.date, message.date)) {
|
|
||||||
messages.push(createDateMessage(message.date));
|
if (m.length > 0) {
|
||||||
}
|
const lastNewMessage = msgs[msgs.length - 1];
|
||||||
|
const firstMessage = m[0];
|
||||||
|
if (
|
||||||
|
firstMessage.date &&
|
||||||
|
!isSameDay(firstMessage.date, lastNewMessage.date)
|
||||||
|
) {
|
||||||
|
msgs.push(createDateMessage(firstMessage.date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prepend) {
|
m.unshift(...msgs);
|
||||||
messages.unshift(message);
|
}
|
||||||
} else {
|
|
||||||
messages.push(message);
|
function reducerAddMessage(message, server, tab, state) {
|
||||||
|
const messages = state[server][tab];
|
||||||
|
|
||||||
|
if (messages.length > 0) {
|
||||||
|
const lastMessage = messages[messages.length - 1];
|
||||||
|
if (lastMessage.date && !isSameDay(lastMessage.date, message.date)) {
|
||||||
|
messages.push(createDateMessage(message.date));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
messages.push(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default createReducer(
|
export default createReducer(
|
||||||
@ -102,12 +117,16 @@ export default createReducer(
|
|||||||
[actions.ADD_MESSAGES](state, { server, tab, messages, prepend }) {
|
[actions.ADD_MESSAGES](state, { server, tab, messages, prepend }) {
|
||||||
if (prepend) {
|
if (prepend) {
|
||||||
init(state, server, tab);
|
init(state, server, tab);
|
||||||
for (let i = messages.length - 1; i >= 0; i--) {
|
reducerPrependMessages(messages, server, tab, state);
|
||||||
reducerAddMessage(messages[i], server, tab, state, true);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
|
if (!messages[0].tab) {
|
||||||
|
init(state, server, tab);
|
||||||
|
}
|
||||||
|
|
||||||
messages.forEach(message => {
|
messages.forEach(message => {
|
||||||
init(state, server, message.tab || tab);
|
if (message.tab) {
|
||||||
|
init(state, server, message.tab);
|
||||||
|
}
|
||||||
reducerAddMessage(message, server, message.tab || tab, state);
|
reducerAddMessage(message, server, message.tab || tab, state);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -172,6 +191,8 @@ function initMessage(message, tab, state) {
|
|||||||
message.channel = true;
|
message.channel = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message.content = message.content || '';
|
||||||
|
|
||||||
// Collapse multiple adjacent spaces into a single one
|
// Collapse multiple adjacent spaces into a single one
|
||||||
message.content = message.content.replace(/\s\s+/g, ' ');
|
message.content = message.content.replace(/\s\s+/g, ' ');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user