diff --git a/routes/_actions/timeline.js b/routes/_actions/timeline.js index 0104c8af..7282f4f4 100644 --- a/routes/_actions/timeline.js +++ b/routes/_actions/timeline.js @@ -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') } diff --git a/routes/_components/status/Status.html b/routes/_components/status/Status.html index f08891b1..54687ddf 100644 --- a/routes/_components/status/Status.html +++ b/routes/_components/status/Status.html @@ -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'); diff --git a/routes/_utils/arrays.js b/routes/_utils/arrays.js new file mode 100644 index 00000000..f29a0df7 --- /dev/null +++ b/routes/_utils/arrays.js @@ -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 +} \ No newline at end of file diff --git a/routes/_utils/replaceAll.js b/routes/_utils/strings.js similarity index 100% rename from routes/_utils/replaceAll.js rename to routes/_utils/strings.js diff --git a/routes/_utils/timelines.js b/routes/_utils/timelines.js deleted file mode 100644 index 05868471..00000000 --- a/routes/_utils/timelines.js +++ /dev/null @@ -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 -} \ No newline at end of file