feat: 💨

This commit is contained in:
codytseng 2026-01-02 02:53:29 +08:00
parent 167b6627f1
commit 89bb9ad2d0
4 changed files with 30 additions and 15 deletions

View file

@ -1,13 +1,13 @@
import { NostrEvent } from 'nostr-tools'
import { compareEvents } from './event'
export function mergeTimelines(timelines: NostrEvent[][], limit: number) {
export function mergeTimelines(timelines: NostrEvent[][], limit?: number) {
if (timelines.length === 0) return []
if (timelines.length === 1) return [...timelines[0]]
return timelines.reduce((merged, current) => _mergeTimelines(merged, current, limit), [])
}
function _mergeTimelines(a: NostrEvent[], b: NostrEvent[], limit: number): NostrEvent[] {
function _mergeTimelines(a: NostrEvent[], b: NostrEvent[], limit?: number): NostrEvent[] {
if (a.length === 0) return [...b]
if (b.length === 0) return [...a]
@ -29,16 +29,16 @@ function _mergeTimelines(a: NostrEvent[], b: NostrEvent[], limit: number): Nostr
}
}
if (result.length >= limit) {
if (limit && result.length >= limit) {
return result
}
while (i < a.length && result.length < limit) {
while (i < a.length && (!limit || result.length < limit)) {
result.push(a[i])
i++
}
while (j < b.length && result.length < limit) {
while (j < b.length && (!limit || result.length < limit)) {
result.push(b[j])
j++
}