Casting Events
Event casts wrap Nostr events into observable models with reactive properties. Each cast provides type-safe access to event data and related content through observables.
Import casts from applesauce-common/casts:
import { castEvent, Note, Article } from "applesauce-common/casts";castEvent()
Cast any Nostr event to a specific cast class:
const note = castEvent(event, Note, eventStore);
const article = castEvent(event, Article, eventStore);Events are cached per class, so casting the same event twice returns the same instance:
const cast1 = castEvent(event, Note, eventStore);
const cast2 = castEvent(event, Note, eventStore);
console.log(cast1 === cast2); // trueCasting Observables
Use castEventStream() and castTimelineStream() to cast observables of events:
import { castEventStream, castTimelineStream } from "applesauce-common/observable";
// Cast a single event observable
const note$ = eventStore.event(eventPointer).pipe(castEventStream(Note, eventStore));
// Cast a timeline observable
const notes$ = eventStore.timeline({ kinds: [1] }).pipe(castTimelineStream(Note, eventStore));Example: Thread Viewer
Base EventCast
All casts extend the EventCast base class which provides common properties:
cast.id; // Event ID
cast.uid; // Unique ID (id:pubkey)
cast.createdAt; // Date object
cast.author; // User cast
cast.seen; // Set of relay URLs where event was seen
cast.event; // Original Nostr event
cast.store; // Event store referenceFeature casts
Each feature page shows how to read its casts alongside the loaders, factories, and actions that go with them.
| Casts | Feature |
|---|---|
Note | Notes & Threads |
Article | Articles |
Comment | Comments |
Profile | Profiles |
Reaction, Share | Reactions & Reposts |
Zap, ZapGoal | Zaps |
Mutes, Report | Mutes & Reports |
BookmarksList, BookmarksSet | Bookmarks |
FavoriteRelays, SearchRelays, BlockedRelays, LookupRelayList | Relay Lists |
GroupsList | Groups |
Stream, StreamChatMessage | Live Streams |
GitRepository, GitIssue, GitPullRequest, GitPatch | Git |
CodeSnippet | Code Snippets |
Torrent | Torrents |
RelayMonitor, RelayDiscovery | Relay Monitoring |
Nsite, Napplet | Nostr Sites, Napplets |
AppHandler, AppRecommendation | App Handlers |
The full list of casts is in the reference.
TIP
All $ suffixed properties return reactive observables that update when the underlying data changes. Use use$ from applesauce-react/hooks to consume them in React components.