pinafore/routes/_utils/statuses.js

32 lines
945 B
JavaScript
Raw Normal View History

2018-01-19 05:25:34 +01:00
// 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) {
2018-01-19 05:25:34 +01:00
let leftIndex = 0
let rightIndex = 0
let merged = []
while (leftIndex < leftStatusIds.length || rightIndex < rightStatusIds.length) {
if (leftIndex === leftStatusIds.length) {
merged.push(rightStatusIds[rightIndex])
2018-01-19 05:25:34 +01:00
rightIndex++
continue
}
if (rightIndex === rightStatusIds.length) {
merged.push(leftStatusIds[leftIndex])
2018-01-19 05:25:34 +01:00
leftIndex++
continue
}
let left = leftStatusIds[leftIndex]
let right = rightStatusIds[rightIndex]
if (right === left) {
2018-01-19 05:25:34 +01:00
rightIndex++
leftIndex++
} else if (parseInt(right, 10) > parseInt(left, 10)) {
2018-01-19 05:25:34 +01:00
merged.push(right)
rightIndex++
} else {
merged.push(left)
leftIndex++
}
}
return merged
}