forked from adamritter/nostr-relaypool-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.ts
55 lines (52 loc) · 1.42 KB
/
collect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type {Event} from "nostr-tools";
import type {OnEvent} from "./on-event-filters";
const binarySearch = function (a: Event[], target: Event) {
var l = 0,
h = a.length - 1,
m,
comparison;
let comparator = function (a: Event, b: Event) {
return a.created_at - b.created_at;
};
while (l <= h) {
m = (l + h) >>> 1; /* equivalent to Math.floor((l + h) / 2) but faster */
comparison = comparator(a[m], target);
if (comparison < 0) {
l = m + 1;
} else if (comparison > 0) {
h = m - 1;
} else {
return m;
}
}
return ~l;
};
const binaryInsert = function (a: Event[], target: Event) {
const duplicate = true; // it's OK to have the same created_at multiple times
var i = binarySearch(a, target);
if (i >= 0) {
/* if the binarySearch return value was zero or positive, a matching object was found */
if (!duplicate) {
return i;
}
} else {
/* if the return value was negative, the bitwise complement of the return value is the correct index for this object */
i = ~i;
}
a.splice(i, 0, target);
return i;
};
export function collect(
onEvents: (events: Event[]) => void,
skipSort: boolean = false
): OnEvent {
let events: Event[] = [];
return (event: Event, afterEose: boolean, url: string | undefined) => {
if (skipSort) {
events.push(event);
} else {
binaryInsert(events, event);
}
onEvents(events);
};
}