dfklfg/packages/system/examples/simple.ts

42 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-09-15 11:42:39 +00:00
import { NostrSystem, RequestBuilder, FlatNoteStore, StoreSnapshot, NoteCollection } from "../src";
2023-06-15 11:03:05 +00:00
// Singleton instance to store all connections and access query fetching system
2023-09-15 11:42:39 +00:00
const System = new NostrSystem({});
2023-06-15 11:03:05 +00:00
(async () => {
2023-09-15 11:42:39 +00:00
// Setup cache system
await System.Init();
2023-07-22 18:37:46 +00:00
// connec to one "bootstrap" relay to pull profiles/relay lists from
// also used as a fallback relay when gossip model doesnt know which relays to pick, or "authors" are not provided in the request
await System.ConnectToRelay("wss://relay.snort.social", { read: true, write: false });
// ID should be unique to the use case, this is important as all data fetched from this ID will be merged into the same NoteStore
const rb = new RequestBuilder("get-posts");
rb.withFilter()
.authors(["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"]) // Kieran pubkey
.kinds([1])
.limit(10);
2023-09-15 11:42:39 +00:00
const q = System.Query(NoteCollection, rb);
// basic usage using "onEvent", fired every 100ms
q.feed.onEvent(evs => {
console.log(evs);
// something else..
});
2023-07-22 18:37:46 +00:00
// Hookable type using change notification, limited to every 500ms
const release = q.feed.hook(() => {
// since we use the FlatNoteStore we expect NostrEvent[]
// other stores provide different data, like a single event instead of an array (latest version)
2023-09-15 11:42:39 +00:00
const state = q.feed.snapshot as StoreSnapshot<ReturnType<NoteCollection["getSnapshotData"]>>;
2023-07-22 18:37:46 +00:00
// do something with snapshot of store
2023-09-15 11:42:39 +00:00
console.log(`We have ${state.data?.length} events now!`);
2023-07-22 18:37:46 +00:00
});
// release the hook when its not needed anymore
// these patterns will be managed in @snort/system-react to make it easier to use react or other UI frameworks
2023-09-15 11:42:39 +00:00
release();
2023-07-22 18:37:46 +00:00
})();