Files
boris/node_modules/applesauce-actions/dist/action-hub.js
Gigi 5d53a827e0 feat: initialize markr nostr bookmark client
- Add project structure with TypeScript, React, and Vite
- Implement nostr authentication using browser extension (NIP-07)
- Add NIP-51 compliant bookmark fetching and display
- Create minimal UI with login and bookmark components
- Integrate applesauce-core and applesauce-react libraries
- Add responsive styling with dark/light mode support
- Include comprehensive README with setup instructions

This is a minimal MVP for a nostr bookmark client that allows users to
view their bookmarks according to NIP-51 specification.
2025-10-02 07:17:07 +02:00

52 lines
1.9 KiB
JavaScript

import { from, isObservable, lastValueFrom, switchMap, tap, toArray } from "rxjs";
/** The main class that runs actions */
export class ActionHub {
events;
factory;
publish;
/** Whether to save all events created by actions to the event store */
saveToStore = true;
constructor(events, factory, publish) {
this.events = events;
this.factory = factory;
this.publish = publish;
}
context = undefined;
async getContext() {
if (this.context)
return this.context;
else {
if (!this.factory.context.signer)
throw new Error("Missing signer");
const self = await this.factory.context.signer.getPublicKey();
this.context = { self, events: this.events, factory: this.factory };
return this.context;
}
}
/** Runs an action in a ActionContext and converts the result to an Observable */
static runAction(ctx, action) {
const result = action(ctx);
if (isObservable(result))
return result;
else
return from(result);
}
/** Run an action and publish events using the publish method */
async run(Action, ...args) {
if (!this.publish)
throw new Error("Missing publish method, use ActionHub.exec");
// wait for action to complete and group events
const events = await lastValueFrom(this.exec(Action, ...args).pipe(toArray()));
// publish events
for (const event of events)
await this.publish(event);
}
/** Run an action without publishing the events */
exec(Action, ...args) {
return from(this.getContext()).pipe(switchMap((ctx) => {
const action = Action(...args);
return ActionHub.runAction(ctx, action);
}), tap((event) => this.saveToStore && this.events.add(event)));
}
}