mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-02-01 14:55:51 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fc7788a4f | ||
|
|
2180c7a1fe | ||
|
|
b4bec2097d | ||
|
|
fb7de7f1aa | ||
|
|
ccb9641fb9 |
@@ -22,9 +22,14 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
|
|||||||
automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
// onRelayConnectionFailure is called with the URL of a relay that failed the initial connection
|
// onRelayConnectionFailure is called with the URL of a relay that failed the initial connection
|
||||||
onRelayConnectionFailure?: (url: string) => void
|
onRelayConnectionFailure?: (url: string) => void
|
||||||
|
// onRelayConnectionSuccess is called with the URL of a relay that succeeds the initial connection
|
||||||
|
onRelayConnectionSuccess?: (url: string) => void
|
||||||
// allowConnectingToRelay takes a relay URL and the operation being performed
|
// allowConnectingToRelay takes a relay URL and the operation being performed
|
||||||
// return false to skip connecting to that relay
|
// return false to skip connecting to that relay
|
||||||
allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
||||||
|
// maxWaitForConnection takes a number in milliseconds that will be given to ensureRelay such that we
|
||||||
|
// don't get stuck forever when attempting to connect to a relay, it is 3000 (3 seconds) by default
|
||||||
|
maxWaitForConnection: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
@@ -47,7 +52,9 @@ export class AbstractSimplePool {
|
|||||||
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
public trustedRelayURLs: Set<string> = new Set()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
public onRelayConnectionFailure?: (url: string) => void
|
public onRelayConnectionFailure?: (url: string) => void
|
||||||
|
public onRelayConnectionSuccess?: (url: string) => void
|
||||||
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
|
||||||
|
public maxWaitForConnection: number
|
||||||
|
|
||||||
private _WebSocket?: typeof WebSocket
|
private _WebSocket?: typeof WebSocket
|
||||||
|
|
||||||
@@ -58,7 +65,9 @@ export class AbstractSimplePool {
|
|||||||
this.enableReconnect = opts.enableReconnect || false
|
this.enableReconnect = opts.enableReconnect || false
|
||||||
this.automaticallyAuth = opts.automaticallyAuth
|
this.automaticallyAuth = opts.automaticallyAuth
|
||||||
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
|
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
|
||||||
|
this.onRelayConnectionSuccess = opts.onRelayConnectionSuccess
|
||||||
this.allowConnectingToRelay = opts.allowConnectingToRelay
|
this.allowConnectingToRelay = opts.allowConnectingToRelay
|
||||||
|
this.maxWaitForConnection = opts.maxWaitForConnection || 3000
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(
|
async ensureRelay(
|
||||||
@@ -199,7 +208,10 @@ export class AbstractSimplePool {
|
|||||||
let relay: AbstractRelay
|
let relay: AbstractRelay
|
||||||
try {
|
try {
|
||||||
relay = await this.ensureRelay(url, {
|
relay = await this.ensureRelay(url, {
|
||||||
connectionTimeout: params.maxWait ? Math.max(params.maxWait * 0.8, params.maxWait - 1000) : undefined,
|
connectionTimeout:
|
||||||
|
this.maxWaitForConnection < (params.maxWait || 0)
|
||||||
|
? Math.max(params.maxWait! * 0.8, params.maxWait! - 1000)
|
||||||
|
: this.maxWaitForConnection,
|
||||||
abort: params.abort,
|
abort: params.abort,
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -208,6 +220,8 @@ export class AbstractSimplePool {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.onRelayConnectionSuccess?.(url)
|
||||||
|
|
||||||
let subscription = relay.subscribe(filters, {
|
let subscription = relay.subscribe(filters, {
|
||||||
...params,
|
...params,
|
||||||
oneose: () => handleEose(i),
|
oneose: () => handleEose(i),
|
||||||
@@ -314,7 +328,11 @@ export class AbstractSimplePool {
|
|||||||
publish(
|
publish(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
event: Event,
|
event: Event,
|
||||||
options?: { onauth?: (evt: EventTemplate) => Promise<VerifiedEvent> },
|
params?: {
|
||||||
|
onauth?: (evt: EventTemplate) => Promise<VerifiedEvent>
|
||||||
|
maxWait?: number
|
||||||
|
abort?: AbortSignal
|
||||||
|
},
|
||||||
): Promise<string>[] {
|
): Promise<string>[] {
|
||||||
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
||||||
if (arr.indexOf(url) !== i) {
|
if (arr.indexOf(url) !== i) {
|
||||||
@@ -328,7 +346,13 @@ export class AbstractSimplePool {
|
|||||||
|
|
||||||
let r: Relay
|
let r: Relay
|
||||||
try {
|
try {
|
||||||
r = await this.ensureRelay(url)
|
r = await this.ensureRelay(url, {
|
||||||
|
connectionTimeout:
|
||||||
|
this.maxWaitForConnection < (params?.maxWait || 0)
|
||||||
|
? Math.max(params!.maxWait! * 0.8, params!.maxWait! - 1000)
|
||||||
|
: this.maxWaitForConnection,
|
||||||
|
abort: params?.abort,
|
||||||
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.onRelayConnectionFailure?.(url)
|
this.onRelayConnectionFailure?.(url)
|
||||||
return String('connection failure: ' + String(err))
|
return String('connection failure: ' + String(err))
|
||||||
@@ -337,8 +361,8 @@ export class AbstractSimplePool {
|
|||||||
return r
|
return r
|
||||||
.publish(event)
|
.publish(event)
|
||||||
.catch(async err => {
|
.catch(async err => {
|
||||||
if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) {
|
if (err instanceof Error && err.message.startsWith('auth-required: ') && params?.onauth) {
|
||||||
await r.auth(options.onauth)
|
await r.auth(params.onauth)
|
||||||
return r.publish(event) // retry
|
return r.publish(event) // retry
|
||||||
}
|
}
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ export class AbstractRelay {
|
|||||||
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
||||||
private reconnectAttempts: number = 0
|
private reconnectAttempts: number = 0
|
||||||
private closedIntentionally: boolean = false
|
private skipReconnection: boolean = false
|
||||||
|
|
||||||
private connectionPromise: Promise<void> | undefined
|
private connectionPromise: Promise<void> | undefined
|
||||||
private openCountRequests = new Map<string, CountResolver>()
|
private openCountRequests = new Map<string, CountResolver>()
|
||||||
@@ -120,12 +120,9 @@ export class AbstractRelay {
|
|||||||
this._connected = false
|
this._connected = false
|
||||||
this.connectionPromise = undefined
|
this.connectionPromise = undefined
|
||||||
|
|
||||||
const wasIntentional = this.closedIntentionally
|
|
||||||
this.closedIntentionally = false // reset for next time
|
|
||||||
|
|
||||||
this.onclose?.()
|
this.onclose?.()
|
||||||
|
|
||||||
if (this.enableReconnect && !wasIntentional) {
|
if (this.enableReconnect && !this.skipReconnection) {
|
||||||
this.reconnect()
|
this.reconnect()
|
||||||
} else {
|
} else {
|
||||||
this.closeAllSubscriptions(reason)
|
this.closeAllSubscriptions(reason)
|
||||||
@@ -139,11 +136,13 @@ export class AbstractRelay {
|
|||||||
|
|
||||||
this.challenge = undefined
|
this.challenge = undefined
|
||||||
this.authPromise = undefined
|
this.authPromise = undefined
|
||||||
|
this.skipReconnection = false
|
||||||
this.connectionPromise = new Promise((resolve, reject) => {
|
this.connectionPromise = new Promise((resolve, reject) => {
|
||||||
if (opts?.timeout) {
|
if (opts?.timeout) {
|
||||||
connectionTimeoutHandle = setTimeout(() => {
|
connectionTimeoutHandle = setTimeout(() => {
|
||||||
reject('connection timed out')
|
reject('connection timed out')
|
||||||
this.connectionPromise = undefined
|
this.connectionPromise = undefined
|
||||||
|
this.skipReconnection = true
|
||||||
this.onclose?.()
|
this.onclose?.()
|
||||||
this.handleHardClose('relay connection timed out')
|
this.handleHardClose('relay connection timed out')
|
||||||
}, opts.timeout)
|
}, opts.timeout)
|
||||||
@@ -153,17 +152,8 @@ export class AbstractRelay {
|
|||||||
opts.abort.onabort = reject
|
opts.abort.onabort = reject
|
||||||
}
|
}
|
||||||
|
|
||||||
const connectionFailed = () => {
|
|
||||||
clearTimeout(connectionTimeoutHandle)
|
|
||||||
reject('connection failed')
|
|
||||||
this.connectionPromise = undefined
|
|
||||||
this.onclose?.()
|
|
||||||
this.handleHardClose('relay connection failed')
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.ws = new this._WebSocket(this.url)
|
this.ws = new this._WebSocket(this.url)
|
||||||
this.ws.addEventListener('error', connectionFailed)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
clearTimeout(connectionTimeoutHandle)
|
clearTimeout(connectionTimeoutHandle)
|
||||||
reject(err)
|
reject(err)
|
||||||
@@ -171,8 +161,6 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
this.ws?.removeEventListener('error', connectionFailed)
|
|
||||||
|
|
||||||
if (this.reconnectTimeoutHandle) {
|
if (this.reconnectTimeoutHandle) {
|
||||||
clearTimeout(this.reconnectTimeoutHandle)
|
clearTimeout(this.reconnectTimeoutHandle)
|
||||||
this.reconnectTimeoutHandle = undefined
|
this.reconnectTimeoutHandle = undefined
|
||||||
@@ -202,10 +190,13 @@ export class AbstractRelay {
|
|||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onerror = ev => {
|
this.ws.onerror = () => {
|
||||||
clearTimeout(connectionTimeoutHandle)
|
clearTimeout(connectionTimeoutHandle)
|
||||||
reject((ev as any).message || 'websocket error')
|
reject('connection failed')
|
||||||
this.handleHardClose('relay connection errored')
|
this.connectionPromise = undefined
|
||||||
|
this.skipReconnection = true
|
||||||
|
this.onclose?.()
|
||||||
|
this.handleHardClose('relay connection failed')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.ws.onclose = ev => {
|
this.ws.onclose = ev => {
|
||||||
@@ -477,7 +468,7 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.closedIntentionally = true
|
this.skipReconnection = true
|
||||||
if (this.reconnectTimeoutHandle) {
|
if (this.reconnectTimeoutHandle) {
|
||||||
clearTimeout(this.reconnectTimeoutHandle)
|
clearTimeout(this.reconnectTimeoutHandle)
|
||||||
this.reconnectTimeoutHandle = undefined
|
this.reconnectTimeoutHandle = undefined
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.22.1",
|
"version": "2.22.2",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.22.1",
|
"version": "2.22.2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
2
pool.ts
2
pool.ts
@@ -15,7 +15,7 @@ export function useWebSocketImplementation(websocketImplementation: any) {
|
|||||||
|
|
||||||
export class SimplePool extends AbstractSimplePool {
|
export class SimplePool extends AbstractSimplePool {
|
||||||
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
|
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
|
||||||
super({ verifyEvent, websocketImplementation: _WebSocket, ...options })
|
super({ verifyEvent, websocketImplementation: _WebSocket, maxWaitForConnection: 3000, ...options })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
insertEventIntoDescendingList,
|
insertEventIntoDescendingList,
|
||||||
binarySearch,
|
binarySearch,
|
||||||
normalizeURL,
|
normalizeURL,
|
||||||
|
mergeReverseSortedLists,
|
||||||
} from './utils.ts'
|
} from './utils.ts'
|
||||||
|
|
||||||
import type { Event } from './core.ts'
|
import type { Event } from './core.ts'
|
||||||
@@ -270,6 +271,94 @@ test('binary search', () => {
|
|||||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
|
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('mergeReverseSortedLists', () => {
|
||||||
|
test('merge empty lists', () => {
|
||||||
|
const list1: Event[] = []
|
||||||
|
const list2: Event[] = []
|
||||||
|
expect(mergeReverseSortedLists(list1, list2)).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('merge list with empty list', () => {
|
||||||
|
const list1 = [buildEvent({ id: 'a', created_at: 30 }), buildEvent({ id: 'b', created_at: 20 })]
|
||||||
|
const list2: Event[] = []
|
||||||
|
const result = mergeReverseSortedLists(list1, list2)
|
||||||
|
expect(result).toHaveLength(2)
|
||||||
|
expect(result.map(e => e.id)).toEqual(['a', 'b'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('merge two simple lists', () => {
|
||||||
|
const list1 = [
|
||||||
|
buildEvent({ id: 'a', created_at: 30 }),
|
||||||
|
buildEvent({ id: 'b', created_at: 10 }),
|
||||||
|
buildEvent({ id: 'f', created_at: 3 }),
|
||||||
|
buildEvent({ id: 'g', created_at: 2 }),
|
||||||
|
]
|
||||||
|
const list2 = [
|
||||||
|
buildEvent({ id: 'c', created_at: 25 }),
|
||||||
|
buildEvent({ id: 'd', created_at: 5 }),
|
||||||
|
buildEvent({ id: 'e', created_at: 1 }),
|
||||||
|
]
|
||||||
|
const result = mergeReverseSortedLists(list1, list2)
|
||||||
|
expect(result.map(e => e.id)).toEqual(['a', 'c', 'b', 'd', 'f', 'g', 'e'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('merge lists with same timestamps', () => {
|
||||||
|
const list1 = [
|
||||||
|
buildEvent({ id: 'a', created_at: 30 }),
|
||||||
|
buildEvent({ id: 'b', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'f', created_at: 10 }),
|
||||||
|
]
|
||||||
|
const list2 = [
|
||||||
|
buildEvent({ id: 'c', created_at: 30 }),
|
||||||
|
buildEvent({ id: 'd', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'e', created_at: 20 }),
|
||||||
|
]
|
||||||
|
const result = mergeReverseSortedLists(list1, list2)
|
||||||
|
expect(result.map(e => e.id)).toEqual(['c', 'a', 'd', 'e', 'b', 'f'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('deduplicate events with same timestamp and id', () => {
|
||||||
|
const list1 = [
|
||||||
|
buildEvent({ id: 'a', created_at: 30 }),
|
||||||
|
buildEvent({ id: 'b', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'b', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'c', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'd', created_at: 10 }),
|
||||||
|
]
|
||||||
|
const list2 = [
|
||||||
|
buildEvent({ id: 'a', created_at: 30 }),
|
||||||
|
buildEvent({ id: 'c', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'b', created_at: 20 }),
|
||||||
|
buildEvent({ id: 'd', created_at: 10 }),
|
||||||
|
buildEvent({ id: 'e', created_at: 10 }),
|
||||||
|
buildEvent({ id: 'd', created_at: 10 }),
|
||||||
|
]
|
||||||
|
console.log('==================')
|
||||||
|
const result = mergeReverseSortedLists(list1, list2)
|
||||||
|
console.log(
|
||||||
|
'result:',
|
||||||
|
result.map(e => e.id),
|
||||||
|
)
|
||||||
|
expect(result.map(e => e.id)).toEqual(['a', 'c', 'b', 'd', 'e'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('merge when one list is completely before the other', () => {
|
||||||
|
const list1 = [buildEvent({ id: 'a', created_at: 50 }), buildEvent({ id: 'b', created_at: 40 })]
|
||||||
|
const list2 = [buildEvent({ id: 'c', created_at: 30 }), buildEvent({ id: 'd', created_at: 20 })]
|
||||||
|
const result = mergeReverseSortedLists(list1, list2)
|
||||||
|
expect(result).toHaveLength(4)
|
||||||
|
expect(result.map(e => e.id)).toEqual(['a', 'b', 'c', 'd'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('merge when one list is completely after the other', () => {
|
||||||
|
const list1 = [buildEvent({ id: 'a', created_at: 10 }), buildEvent({ id: 'b', created_at: 5 })]
|
||||||
|
const list2 = [buildEvent({ id: 'c', created_at: 30 }), buildEvent({ id: 'd', created_at: 20 })]
|
||||||
|
const result = mergeReverseSortedLists(list1, list2)
|
||||||
|
expect(result).toHaveLength(4)
|
||||||
|
expect(result.map(e => e.id)).toEqual(['c', 'd', 'a', 'b'])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('normalizeURL', () => {
|
describe('normalizeURL', () => {
|
||||||
test('normalizes wss:// URLs', () => {
|
test('normalizes wss:// URLs', () => {
|
||||||
expect(normalizeURL('wss://example.com')).toBe('wss://example.com/')
|
expect(normalizeURL('wss://example.com')).toBe('wss://example.com/')
|
||||||
|
|||||||
62
utils.ts
62
utils.ts
@@ -1,4 +1,4 @@
|
|||||||
import type { Event } from './core.ts'
|
import type { NostrEvent } from './core.ts'
|
||||||
|
|
||||||
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
||||||
export const utf8Encoder: TextEncoder = new TextEncoder()
|
export const utf8Encoder: TextEncoder = new TextEncoder()
|
||||||
@@ -22,7 +22,7 @@ export function normalizeURL(url: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] {
|
export function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {
|
||||||
const [idx, found] = binarySearch(sortedArray, b => {
|
const [idx, found] = binarySearch(sortedArray, b => {
|
||||||
if (event.id === b.id) return 0
|
if (event.id === b.id) return 0
|
||||||
if (event.created_at === b.created_at) return -1
|
if (event.created_at === b.created_at) return -1
|
||||||
@@ -34,7 +34,7 @@ export function insertEventIntoDescendingList(sortedArray: Event[], event: Event
|
|||||||
return sortedArray
|
return sortedArray
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertEventIntoAscendingList(sortedArray: Event[], event: Event): Event[] {
|
export function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {
|
||||||
const [idx, found] = binarySearch(sortedArray, b => {
|
const [idx, found] = binarySearch(sortedArray, b => {
|
||||||
if (event.id === b.id) return 0
|
if (event.id === b.id) return 0
|
||||||
if (event.created_at === b.created_at) return -1
|
if (event.created_at === b.created_at) return -1
|
||||||
@@ -68,6 +68,62 @@ export function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, b
|
|||||||
return [start, false]
|
return [start, false]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {
|
||||||
|
const result: NostrEvent[] = new Array(list1.length + list2.length)
|
||||||
|
result.length = 0
|
||||||
|
let i1 = 0
|
||||||
|
let i2 = 0
|
||||||
|
let sameTimestampIds: string[] = []
|
||||||
|
|
||||||
|
while (i1 < list1.length && i2 < list2.length) {
|
||||||
|
let next: NostrEvent
|
||||||
|
if (list1[i1]?.created_at > list2[i2]?.created_at) {
|
||||||
|
next = list1[i1]
|
||||||
|
i1++
|
||||||
|
} else {
|
||||||
|
next = list2[i2]
|
||||||
|
i2++
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
|
||||||
|
if (sameTimestampIds.includes(next.id)) continue
|
||||||
|
} else {
|
||||||
|
sameTimestampIds.length = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push(next)
|
||||||
|
sameTimestampIds.push(next.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i1 < list1.length) {
|
||||||
|
const next = list1[i1]
|
||||||
|
i1++
|
||||||
|
|
||||||
|
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
|
||||||
|
if (sameTimestampIds.includes(next.id)) continue
|
||||||
|
} else {
|
||||||
|
sameTimestampIds.length = 0
|
||||||
|
}
|
||||||
|
result.push(next)
|
||||||
|
sameTimestampIds.push(next.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i2 < list2.length) {
|
||||||
|
const next = list2[i2]
|
||||||
|
i2++
|
||||||
|
|
||||||
|
if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {
|
||||||
|
if (sameTimestampIds.includes(next.id)) continue
|
||||||
|
} else {
|
||||||
|
sameTimestampIds.length = 0
|
||||||
|
}
|
||||||
|
result.push(next)
|
||||||
|
sameTimestampIds.push(next.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
export class QueueNode<V> {
|
export class QueueNode<V> {
|
||||||
public value: V
|
public value: V
|
||||||
public next: QueueNode<V> | null = null
|
public next: QueueNode<V> | null = null
|
||||||
|
|||||||
Reference in New Issue
Block a user