@nostr-dev-kit/ndk-svelte
Version:
This package provides convenience functionalities to make usage of NDK with Svelte nicer.
68 lines (65 loc) • 2.21 kB
TypeScript
import NDK, { NDKEvent, NDKFilter, NDKSubscriptionOptions, NDKRelaySet, NDKRelay, NDKSubscription } from '@nostr-dev-kit/ndk';
import { Writable, Unsubscriber } from 'svelte/store';
/**
* Type for NDKEvent classes that have a static `from` method like NDKHighlight.
*/
type ClassWithConvertFunction<T extends NDKEvent> = {
from: (event: NDKEvent) => T | undefined;
};
type ExtendedBaseType<T extends NDKEvent> = T & {
repostedByEvents?: NDKEvent[];
};
type NDKEventStore<T extends NDKEvent> = Writable<ExtendedBaseType<T>[]> & {
id: string;
filters: NDKFilter[] | undefined;
refCount: number;
subscription: NDKSubscription | undefined;
eosed: boolean;
skipDeleted: boolean;
startSubscription: () => void;
unsubscribe: Unsubscriber;
onEose: (cb: () => void) => void;
onEvent: (cb: (event: NDKEvent, relay?: NDKRelay) => void) => void;
ref: () => number;
unref: () => number;
empty: () => void;
changeFilters: (filters: NDKFilter[]) => void;
};
type NDKSubscribeOptions = NDKSubscriptionOptions & {
/**
* Whether the subscription should start when the
* store is created. Defaults to true.
*/
autoStart?: boolean;
/**
* Reposts filters
*/
repostsFilters?: NDKFilter[];
/**
* Wait this amount of ms before unsubscribing when there are zero refs.
*/
unrefUnsubscribeTimeout?: number;
/**
* Relay set to use for the subscription
*/
relaySet?: NDKRelaySet;
/**
* Whether deleted PRE/addressable-events should be skipped.
* @default true
*/
skipDeleted?: boolean;
/**
* Callback to be called when the subscription EOSEs
*/
onEose?: () => void;
/**
* Callback to be called when a new event is received
*/
onEvent?: (event: NDKEvent, relay?: NDKRelay) => void;
};
declare class NDKSvelte extends NDK {
private createEventStore;
private eventIsRepost;
storeSubscribe<T extends NDKEvent>(filters: NDKFilter | NDKFilter[], opts?: NDKSubscribeOptions, klass?: ClassWithConvertFunction<T>): NDKEventStore<ExtendedBaseType<T>>;
}
export { type ExtendedBaseType, type NDKEventStore, NDKSvelte as default };