fix excessive itemIdsToAdd updates (#117)

fixes #40
This commit is contained in:
Nolan Lawson 2018-04-14 11:52:47 -07:00 committed by GitHub
parent 827ed42eaf
commit dc91dbd1e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 17 deletions

View File

@ -5,6 +5,7 @@ import { store } from '../_store/store'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import uniqBy from 'lodash-es/uniqBy' import uniqBy from 'lodash-es/uniqBy'
import uniq from 'lodash-es/uniq' import uniq from 'lodash-es/uniq'
import isEqual from 'lodash-es/isEqual'
import { isMobile } from '../_utils/isMobile' import { isMobile } from '../_utils/isMobile'
const STREAMING_THROTTLE_DELAY = 3000 const STREAMING_THROTTLE_DELAY = 3000
@ -30,10 +31,12 @@ async function insertUpdatesIntoTimeline (instanceName, timelineName, updates) {
await database.insertTimelineItems(instanceName, timelineName, updates) await database.insertTimelineItems(instanceName, timelineName, updates)
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || [] let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || []
let newItemIdsToAdd = uniq([].concat(itemIdsToAdd).concat(updates.map(_ => _.id)))
itemIdsToAdd = uniq(itemIdsToAdd.concat(updates.map(_ => _.id))) if (!isEqual(itemIdsToAdd, newItemIdsToAdd)) {
console.log('adding ', itemIdsToAdd.length, 'items to itemIdsToAdd on instance', instanceName, 'on timeline', timelineName) console.log('adding ', (newItemIdsToAdd.length - itemIdsToAdd.length),
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: itemIdsToAdd}) 'items to itemIdsToAdd for timeline', timelineName)
store.setForTimeline(instanceName, timelineName, {itemIdsToAdd: newItemIdsToAdd})
}
} }
async function insertUpdatesIntoThreads (instanceName, updates) { async function insertUpdatesIntoThreads (instanceName, updates) {
@ -45,17 +48,19 @@ async function insertUpdatesIntoThreads (instanceName, updates) {
for (let timelineName of Object.keys(threads)) { for (let timelineName of Object.keys(threads)) {
let thread = threads[timelineName] let thread = threads[timelineName]
let updatesForThisThread = updates.filter(status => { let updatesForThisThread = updates.filter(
return thread.includes(status.in_reply_to_id) && !thread.includes(status.id) status => thread.includes(status.in_reply_to_id) && !thread.includes(status.id)
}) )
let itemIdsToAdd = store.getForTimeline(instanceName, timelineName, 'itemIdsToAdd') || [] if (!updatesForThisThread.length) {
for (let update of updatesForThisThread) { continue
if (!itemIdsToAdd.includes(update.id)) { }
itemIdsToAdd.push(update.id) 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() let updates = freshUpdates.slice()
store.setForTimeline(instanceName, timelineName, {freshUpdates: []}) store.setForTimeline(instanceName, timelineName, {freshUpdates: []})
await insertUpdatesIntoTimeline(instanceName, timelineName, updates) await Promise.all([
await insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id)) insertUpdatesIntoTimeline(instanceName, timelineName, updates),
insertUpdatesIntoThreads(instanceName, updates.filter(status => status.in_reply_to_id))
])
} }
stop('processFreshUpdates') stop('processFreshUpdates')
} }
@ -85,7 +92,7 @@ export function addStatusOrNotification (instanceName, timelineName, newStatusOr
export function addStatusesOrNotifications (instanceName, timelineName, newStatusesOrNotifications) { export function addStatusesOrNotifications (instanceName, timelineName, newStatusesOrNotifications) {
let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || [] let freshUpdates = store.getForTimeline(instanceName, timelineName, 'freshUpdates') || []
freshUpdates = freshUpdates.concat(newStatusesOrNotifications) freshUpdates = [].concat(freshUpdates).concat(newStatusesOrNotifications)
freshUpdates = uniqBy(freshUpdates, _ => _.id) freshUpdates = uniqBy(freshUpdates, _ => _.id)
store.setForTimeline(instanceName, timelineName, {freshUpdates: freshUpdates}) store.setForTimeline(instanceName, timelineName, {freshUpdates: freshUpdates})
lazilyProcessFreshUpdates(instanceName, timelineName) lazilyProcessFreshUpdates(instanceName, timelineName)