optimize messages list computation
This commit is contained in:
parent
63c2a80816
commit
268a654617
1 changed files with 11 additions and 4 deletions
|
@ -14,6 +14,10 @@ class Store {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log(...e) {
|
||||||
|
return storeLog(`[${this.name}]`, ...e);
|
||||||
|
}
|
||||||
|
|
||||||
// like subscribe, but without initially calling the handler
|
// like subscribe, but without initially calling the handler
|
||||||
watch(handler) {
|
watch(handler) {
|
||||||
storeLog(`[Watch] (${this.name})`, "handler:", handler);
|
storeLog(`[Watch] (${this.name})`, "handler:", handler);
|
||||||
|
@ -174,15 +178,18 @@ class MessageStore extends Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
_recomputeMessages() {
|
_recomputeMessages() {
|
||||||
|
const start = performance.now();
|
||||||
for (let i = 0; i < this.value.length; i++) {
|
for (let i = 0; i < this.value.length; i++) {
|
||||||
this.value[i] = this._processMessage(this.value[i], this.value[i - 1]);
|
this.value[i] = this._processMessage(this.value[i], this.value[i - 1]);
|
||||||
}
|
}
|
||||||
|
const delta = performance.now() - start;
|
||||||
|
this.log(`[_recomputeMessages] Computed ${this.value.length} messages in ${delta}ms`);
|
||||||
}
|
}
|
||||||
|
|
||||||
_processMessage(message, previous=null) {
|
_processMessage(message, previous=null) {
|
||||||
message._createdAtDate = new Date(parseInt(message.created_at));
|
message._createdAtDate = message._createdAtDate || new Date(parseInt(message.created_at));
|
||||||
message._createdAtTimeString = new Intl.DateTimeFormat(getItem("ui:locale"), { hour: "numeric", minute: "numeric" }).format(message._createdAtDate);
|
message._createdAtTimeString = message._createdAtTimeString || new Intl.DateTimeFormat(getItem("ui:locale"), { hour: "numeric", minute: "numeric" }).format(message._createdAtDate);
|
||||||
message._createdAtDateString = message._createdAtDate.toLocaleDateString();
|
message._createdAtDateString = message._createdAtDateString || message._createdAtDate.toLocaleDateString();
|
||||||
message._mentions = false;
|
message._mentions = false;
|
||||||
message._editable = false;
|
message._editable = false;
|
||||||
message._clumped = false;
|
message._clumped = false;
|
||||||
|
@ -196,7 +203,7 @@ class MessageStore extends Store {
|
||||||
if (previous && (message._createdAtDate.getTime() - previous._createdAtDate.getTime()) <= 100 * 1000 && message.author_id === previous.author_id) {
|
if (previous && (message._createdAtDate.getTime() - previous._createdAtDate.getTime()) <= 100 * 1000 && message.author_id === previous.author_id) {
|
||||||
message._clumped = true;
|
message._clumped = true;
|
||||||
}
|
}
|
||||||
if (!previous || (previous._createdAtDateString !== message._createdAtDateString)) {
|
if (!previous || (previous._createdAtDateString !== message._createdAtDateString && !message._aboveDateMarker)) {
|
||||||
message._aboveDateMarker = new Intl.DateTimeFormat(getItem("ui:locale"), { month: "long", day: "numeric", year: "numeric" }).format(message._createdAtDate);
|
message._aboveDateMarker = new Intl.DateTimeFormat(getItem("ui:locale"), { month: "long", day: "numeric", year: "numeric" }).format(message._createdAtDate);
|
||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
|
|
Loading…
Reference in a new issue