This commit is contained in:
Nolan Lawson 2018-01-28 15:02:02 -08:00
parent 22399df264
commit 8b282de973
5 changed files with 35 additions and 35 deletions

View File

@ -5,7 +5,7 @@ import { toast } from '../_utils/toast'
import { StatusStream } from '../_utils/mastodon/StatusStream'
import { getInstanceInfo } from '../_utils/mastodon/instance'
import { mark, stop } from '../_utils/marks'
import { mergeStatuses } from '../_utils/timelines'
import { mergeArrays } from '../_utils/arrays'
const FETCH_LIMIT = 20
@ -34,7 +34,7 @@ async function addStatuses(instanceName, timelineName, newStatuses) {
mark('addStatuses')
let newStatusIds = newStatuses.map(status => status.id)
let oldStatusIds = store.getForTimeline(instanceName, timelineName, 'statusIds') || []
let merged = mergeStatuses(oldStatusIds, newStatusIds)
let merged = mergeArrays(oldStatusIds, newStatusIds)
store.setForTimeline(instanceName, timelineName, { statusIds: merged })
stop('addStatuses')
}

View File

@ -296,7 +296,7 @@
import Toolbar from './Toolbar.html'
import { mark, stop } from '../../_utils/marks'
import IntlRelativeFormat from 'intl-relativeformat'
import { replaceAll } from '../../_utils/replaceAll'
import { replaceAll } from '../../_utils/strings'
import { store } from '../../_store/store'
const relativeFormat = new IntlRelativeFormat('en-US');

32
routes/_utils/arrays.js Normal file
View File

@ -0,0 +1,32 @@
// Merge two arrays, assuming both input arrays have the same order
// and items are comparable
export function mergeArrays(leftArray, rightArray) {
let leftIndex = 0
let rightIndex = 0
let merged = []
while (leftIndex < leftArray.length || rightIndex < rightArray.length) {
if (leftIndex === leftArray.length) {
merged.push(rightArray[rightIndex])
rightIndex++
continue
}
if (rightIndex === rightArray.length) {
merged.push(leftArray[leftIndex])
leftIndex++
continue
}
let left = leftArray[leftIndex]
let right = rightArray[rightIndex]
if (right === left) {
rightIndex++
leftIndex++
} else if (parseInt(right, 10) > parseInt(left, 10)) {
merged.push(right)
rightIndex++
} else {
merged.push(left)
leftIndex++
}
}
return merged
}

View File

@ -1,32 +0,0 @@
// Merge two lists of statuses for the same timeline, e.g. one from IDB
// and another from the network. In case of duplicates, prefer the fresh.
export function mergeStatuses(leftStatusIds, rightStatusIds) {
let leftIndex = 0
let rightIndex = 0
let merged = []
while (leftIndex < leftStatusIds.length || rightIndex < rightStatusIds.length) {
if (leftIndex === leftStatusIds.length) {
merged.push(rightStatusIds[rightIndex])
rightIndex++
continue
}
if (rightIndex === rightStatusIds.length) {
merged.push(leftStatusIds[leftIndex])
leftIndex++
continue
}
let left = leftStatusIds[leftIndex]
let right = rightStatusIds[rightIndex]
if (right === left) {
rightIndex++
leftIndex++
} else if (parseInt(right, 10) > parseInt(left, 10)) {
merged.push(right)
rightIndex++
} else {
merged.push(left)
leftIndex++
}
}
return merged
}