From dc91dbd1e7ed5145a0b792667206b5c0f65ce043 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 14 Apr 2018 11:52:47 -0700 Subject: [PATCH] fix excessive itemIdsToAdd updates (#117) fixes #40 --- routes/_actions/addStatusOrNotification.js | 41 +++++++++++++--------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/routes/_actions/addStatusOrNotification.js b/routes/_actions/addStatusOrNotification.js index 8c388196..f9fc8311 100644 --- a/routes/_actions/addStatusOrNotification.js +++ b/routes/_actions/addStatusOrNotification.js @@ -5,6 +5,7 @@ import { store } from '../_store/store' import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import uniqBy from 'lodash-es/uniqBy' import uniq from 'lodash-es/uniq' +import isEqual from 'lodash-es/isEqual' import { isMobile } from '../_utils/isMobile' const STREAMING_THROTTLE_DELAY = 3000 @@ -30,10 +31,12 @@ async function insertUpdatesIntoTimeline (instanceName, timelineName, updates) { await database.insertTimelineItems(instanceName, timelineName, updates) let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || [] - - itemIdsToAdd = uniq(itemIdsToAdd.concat(updates.map(_ => _.id))) - console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd on instance', instanceName, 'on timeline', timelineName) - store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd}) + let newItemIdsToAdd = uniq([].concat(itemIdsToAdd).concat(updates.map(_ => _.id))) + if (!isEqual(itemIdsToAdd, newItemIdsToAdd)) { + console.log('adding ', (newItemIdsToAdd.length - itemIdsToAdd.length), + 'items to itemIdsToAdd for timeline', timelineName) + store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: newItemIdsToAdd}) + } } async function insertUpdatesIntoThreads (instanceName, updates) { @@ -45,17 +48,19 @@ async function insertUpdatesIntoThreads (instanceName, updates) { for (let timelineName of Object.keys(threads)) { let thread = threads[timelineName] - let updatesForThisThread = updates.filter(status => { - return thread.includes(status.in_reply_to_id) && !thread.includes(status.id) - }) - let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || [] - for (let update of updatesForThisThread) { - if (!itemIdsToAdd.includes(update.id)) { - itemIdsToAdd.push(update.id) - } + let updatesForThisThread = updates.filter( + status => thread.includes(status.in_reply_to_id) && !thread.includes(status.id) + ) + if (!updatesForThisThread.length) { + continue + } + let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || [] + let newItemIdsToAdd = uniq([].concat(itemIdsToAdd).concat(updatesForThisThread.map(_ => _.id))) + if (!isEqual(itemIdsToAdd, newItemIdsToAdd)) { + console.log('adding ', (newItemIdsToAdd.length - itemIdsToAdd.length), + 'items to itemIdsToAdd for thread', timelineName) + store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: newItemIdsToAdd}) } - console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd for thread', timelineName) - store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd}) } } @@ -66,8 +71,10 @@ async function processFreshUpdates (instanceName, timelineName) { let updates = freshUpdates.slice() store.setForTimeline(instanceName, timelineName, {freshUpdates: []}) - await insertUpdatesIntoTimeline(instanceName, timelineName, updates) - await insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id)) + await Promise.all([ + insertUpdatesIntoTimeline(instanceName, timelineName, updates), + insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id)) + ]) } stop('processFreshUpdates') } @@ -85,7 +92,7 @@ export function addStatusOrNotification (instanceName, timelineName, newStatusOr export function addStatusesOrNotifications (instanceName, timelineName, newStatusesOrNotifications) { let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || [] - freshUpdates = freshUpdates.concat(newStatusesOrNotifications) + freshUpdates = [].concat(freshUpdates).concat(newStatusesOrNotifications) freshUpdates = uniqBy(freshUpdates, _ => _.id) store.setForTimeline(instanceName, timelineName, {freshUpdates: freshUpdates}) lazilyProcessFreshUpdates(instanceName, timelineName)