update nostr-tools

This commit is contained in:
Shusui MOYATANI
2023-12-21 02:51:51 +09:00
parent 4df42cea0a
commit 47eb2fc57d
48 changed files with 216 additions and 162 deletions

View File

@@ -1,7 +1,10 @@
import { createSignal, createEffect, onMount, onCleanup, on } from 'solid-js';
import uniqBy from 'lodash/uniqBy';
import { utils } from 'nostr-tools';
import { type Filter } from 'nostr-tools/filter';
import { type SubscribeManyParams } from 'nostr-tools/pool';
import { type Event as NostrEvent } from 'nostr-tools/pure';
import { insertEventIntoDescendingList } from 'nostr-tools/utils';
import useConfig from '@/core/useConfig';
import { sortEvents } from '@/nostr/event/comparator';
@@ -9,12 +12,10 @@ import usePool from '@/nostr/usePool';
import useStats from '@/nostr/useStats';
import epoch from '@/utils/epoch';
import type { Event as NostrEvent, Filter, SubscriptionOptions } from 'nostr-tools';
export type UseSubscriptionProps = {
relayUrls: string[];
filters: Filter[];
options?: SubscriptionOptions;
options?: SubscribeManyParams;
/**
* subscribe not only stored events but also new events published after the subscription
* default is true
@@ -72,7 +73,7 @@ const useSubscription = (propsProvider: () => UseSubscriptionProps | null) => {
}
setEvents((current) => {
const sorted = utils.insertEventIntoDescendingList(current, event).slice(0, limit);
const sorted = insertEventIntoDescendingList(current, event).slice(0, limit);
// FIXME なぜか重複して取得される問題があるが一旦uniqByで対処
// https://github.com/syusui-s/rabbit/issues/5
const deduped = uniqBy(sorted, (e) => e.id);
@@ -89,8 +90,6 @@ const useSubscription = (propsProvider: () => UseSubscriptionProps | null) => {
const props = propsProvider();
if (props == null) return;
const { relayUrls, filters, options, onEvent, onEOSE, continuous = true } = props;
const sub = pool().sub(relayUrls, filters, options);
let subscribing = true;
count += 1;
@@ -98,38 +97,45 @@ const useSubscription = (propsProvider: () => UseSubscriptionProps | null) => {
let eose = false;
const storedEvents: NostrEvent[] = [];
sub.on('event', (event: NostrEvent) => {
if (onEvent != null) {
onEvent(event as NostrEvent & { id: string });
}
if (props.clientEventFilter != null && !props.clientEventFilter(event)) {
return;
}
const sub = pool().subscribeMany(
relayUrls,
filters,
options ?? {
eoseTimeout: 12000,
maxWait: 6000,
onevent: (event: NostrEvent) => {
if (onEvent != null) {
onEvent(event as NostrEvent & { id: string });
}
if (props.clientEventFilter != null && !props.clientEventFilter(event)) {
return;
}
if (!eose) {
pushed = true;
storedEvents.push(event);
} else {
addEvent(event);
}
});
if (!eose) {
pushed = true;
storedEvents.push(event);
} else {
addEvent(event);
}
},
oneose: () => {
if (onEOSE != null) {
onEOSE();
}
sub.on('eose', () => {
if (onEOSE != null) {
onEOSE();
}
eose = true;
setEvents(sortEvents(storedEvents));
eose = true;
setEvents(sortEvents(storedEvents));
if (!continuous) {
sub.unsub();
if (subscribing) {
subscribing = false;
count -= 1;
}
}
});
if (!continuous) {
sub.close();
if (subscribing) {
subscribing = false;
count -= 1;
}
}
},
},
);
// avoid updating an array too rapidly while this is fetching stored events
let updating = false;
@@ -150,7 +156,7 @@ const useSubscription = (propsProvider: () => UseSubscriptionProps | null) => {
onCleanup(() => {
console.debug('startSubscription: end');
sub.unsub();
sub.close();
if (subscribing) {
subscribing = false;
count -= 1;