applesauce-actions
Version:
A package for performing common nostr actions
36 lines (35 loc) • 1.98 kB
TypeScript
import { Observable } from "rxjs";
import { NostrEvent } from "nostr-tools";
import { EventFactory } from "applesauce-factory";
import { IEventStoreActions, IEventStoreRead } from "applesauce-core";
/** A callback used to tell the upstream app to publish an event */
export type PublishMethod = (event: NostrEvent) => void | Promise<void>;
/** The context that is passed to actions for them to use to preform actions */
export type ActionContext = {
/** The event store to load events from */
events: IEventStoreRead;
/** The pubkey of the signer in the event factory */
self: string;
/** The event factory used to build and modify events */
factory: EventFactory;
};
/** An action that can be run in a context to preform an action */
export type Action = (ctx: ActionContext) => Observable<NostrEvent> | AsyncGenerator<NostrEvent> | Generator<NostrEvent>;
export type ActionConstructor<Args extends Array<any>> = (...args: Args) => Action;
/** The main class that runs actions */
export declare class ActionHub {
events: IEventStoreRead & IEventStoreActions;
factory: EventFactory;
publish?: PublishMethod | undefined;
/** Whether to save all events created by actions to the event store */
saveToStore: boolean;
constructor(events: IEventStoreRead & IEventStoreActions, factory: EventFactory, publish?: PublishMethod | undefined);
protected context: ActionContext | undefined;
protected getContext(): Promise<ActionContext>;
/** Runs an action in a ActionContext and converts the result to an Observable */
static runAction(ctx: ActionContext, action: Action): Observable<NostrEvent>;
/** Run an action and publish events using the publish method */
run<Args extends Array<any>>(Action: ActionConstructor<Args>, ...args: Args): Promise<void>;
/** Run an action without publishing the events */
exec<Args extends Array<any>>(Action: ActionConstructor<Args>, ...args: Args): Observable<NostrEvent>;
}