This commit is contained in:
parent
9a13888db8
commit
37948d1579
@ -7,61 +7,48 @@ Simple example:
|
|||||||
```js
|
```js
|
||||||
import {
|
import {
|
||||||
NostrSystem,
|
NostrSystem,
|
||||||
EventPublisher,
|
|
||||||
UserRelaysCache,
|
|
||||||
RequestBuilder,
|
RequestBuilder,
|
||||||
FlatNoteStore,
|
StoreSnapshot,
|
||||||
StoreSnapshot
|
NoteCollection
|
||||||
} from "@snort/system"
|
} from "@snort/system"
|
||||||
|
|
||||||
// Provided in-memory / indexedDb cache for relays
|
|
||||||
// You can also implement your own with "RelayCache" interface
|
|
||||||
const RelaysCache = new UserRelaysCache();
|
|
||||||
|
|
||||||
// example auth handler using NIP-07
|
|
||||||
const AuthHandler = async (challenge: string, relay: string) => {
|
|
||||||
const pub = await EventPublisher.nip7();
|
|
||||||
if (pub) {
|
|
||||||
return await pub.nip42Auth(challenge, relay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Singleton instance to store all connections and access query fetching system
|
// Singleton instance to store all connections and access query fetching system
|
||||||
const System = new NostrSystem({
|
const System = new NostrSystem({});
|
||||||
relayCache: RelaysCache,
|
|
||||||
authHandler: AuthHandler // can be left undefined if you dont care about NIP-42 Auth
|
|
||||||
});
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
// connec to one "bootstrap" relay to pull profiles/relay lists from
|
// Setup cache system
|
||||||
// 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.Init();
|
||||||
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
|
// connec to one "bootstrap" relay to pull profiles/relay lists from
|
||||||
const rb = new RequestBuilder("get-posts");
|
// also used as a fallback relay when gossip model doesnt know which relays to pick, or "authors" are not provided in the request
|
||||||
rb.withFilter()
|
await System.ConnectToRelay("wss://relay.snort.social", { read: true, write: false });
|
||||||
.authors(["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"]) // Kieran pubkey
|
|
||||||
.kinds([1])
|
|
||||||
.limit(10);
|
|
||||||
|
|
||||||
const q = System.Query<FlatNoteStore>(FlatNoteStore, rb);
|
// 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
|
||||||
// basic usage using "onEvent", fired for every event added to the store
|
const rb = new RequestBuilder("get-posts");
|
||||||
q.onEvent = (sub, e) => {
|
rb.withFilter()
|
||||||
console.debug(sub, e);
|
.authors(["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"]) // Kieran pubkey
|
||||||
}
|
.kinds([1])
|
||||||
|
.limit(10);
|
||||||
|
|
||||||
// Hookable type using change notification, limited to every 500ms
|
const q = System.Query(NoteCollection, rb);
|
||||||
const release = q.feed.hook(() => {
|
// basic usage using "onEvent", fired every 100ms
|
||||||
// since we use the FlatNoteStore we expect NostrEvent[]
|
q.feed.onEvent(evs => {
|
||||||
// other stores provide different data, like a single event instead of an array (latest version)
|
console.log(evs);
|
||||||
const state = q.feed.snapshot as StoreSnapshot<ReturnType<FlatNoteStore["getSnapshotData"]>>;
|
// something else..
|
||||||
|
});
|
||||||
|
|
||||||
// do something with snapshot of store
|
// Hookable type using change notification, limited to every 500ms
|
||||||
console.log(`We have ${state.data.length} events now!`)
|
const release = q.feed.hook(() => {
|
||||||
});
|
// since we use the NoteCollection we expect NostrEvent[]
|
||||||
|
// other stores provide different data, like a single event instead of an array (latest version)
|
||||||
|
const state = q.feed.snapshot as StoreSnapshot<ReturnType<NoteCollection["getSnapshotData"]>>;
|
||||||
|
|
||||||
// release the hook when its not needed anymore
|
// do something with snapshot of store
|
||||||
// these patterns will be managed in @snort/system-react to make it easier to use react or other UI frameworks
|
console.log(`We have ${state.data?.length} events now!`);
|
||||||
// release();
|
});
|
||||||
|
|
||||||
|
// 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
|
||||||
|
release();
|
||||||
})();
|
})();
|
||||||
```
|
```
|
||||||
|
@ -1,24 +1,12 @@
|
|||||||
import { NostrSystem, EventPublisher, UserRelaysCache, RequestBuilder, FlatNoteStore, StoreSnapshot } from "../src";
|
import { NostrSystem, RequestBuilder, FlatNoteStore, StoreSnapshot, NoteCollection } from "../src";
|
||||||
|
|
||||||
// Provided in-memory / indexedDb cache for relays
|
|
||||||
// You can also implement your own with "RelayCache" interface
|
|
||||||
const RelaysCache = new UserRelaysCache();
|
|
||||||
|
|
||||||
// example auth handler using NIP-07
|
|
||||||
const AuthHandler = async (challenge: string, relay: string) => {
|
|
||||||
const pub = await EventPublisher.nip7();
|
|
||||||
if (pub) {
|
|
||||||
return await pub.nip42Auth(challenge, relay);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Singleton instance to store all connections and access query fetching system
|
// Singleton instance to store all connections and access query fetching system
|
||||||
const System = new NostrSystem({
|
const System = new NostrSystem({});
|
||||||
relayCache: RelaysCache,
|
|
||||||
authHandler: AuthHandler, // can be left undefined if you dont care about NIP-42 Auth
|
|
||||||
});
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
|
// Setup cache system
|
||||||
|
await System.Init();
|
||||||
|
|
||||||
// connec to one "bootstrap" relay to pull profiles/relay lists from
|
// 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
|
// 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 });
|
await System.ConnectToRelay("wss://relay.snort.social", { read: true, write: false });
|
||||||
@ -30,23 +18,24 @@ const System = new NostrSystem({
|
|||||||
.kinds([1])
|
.kinds([1])
|
||||||
.limit(10);
|
.limit(10);
|
||||||
|
|
||||||
const q = System.Query<FlatNoteStore>(FlatNoteStore, rb);
|
const q = System.Query(NoteCollection, rb);
|
||||||
// basic usage using "onEvent", fired for every event added to the store
|
// basic usage using "onEvent", fired every 100ms
|
||||||
q.onEvent = (sub, e) => {
|
q.feed.onEvent(evs => {
|
||||||
console.debug(sub, e);
|
console.log(evs);
|
||||||
};
|
// something else..
|
||||||
|
});
|
||||||
|
|
||||||
// Hookable type using change notification, limited to every 500ms
|
// Hookable type using change notification, limited to every 500ms
|
||||||
const release = q.feed.hook(() => {
|
const release = q.feed.hook(() => {
|
||||||
// since we use the FlatNoteStore we expect NostrEvent[]
|
// since we use the FlatNoteStore we expect NostrEvent[]
|
||||||
// other stores provide different data, like a single event instead of an array (latest version)
|
// other stores provide different data, like a single event instead of an array (latest version)
|
||||||
const state = q.feed.snapshot as StoreSnapshot<ReturnType<FlatNoteStore["getSnapshotData"]>>;
|
const state = q.feed.snapshot as StoreSnapshot<ReturnType<NoteCollection["getSnapshotData"]>>;
|
||||||
|
|
||||||
// do something with snapshot of store
|
// do something with snapshot of store
|
||||||
console.log(`We have ${state.data.length} events now!`);
|
console.log(`We have ${state.data?.length} events now!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// release the hook when its not needed anymore
|
// 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
|
// these patterns will be managed in @snort/system-react to make it easier to use react or other UI frameworks
|
||||||
// release();
|
release();
|
||||||
})();
|
})();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@snort/system",
|
"name": "@snort/system",
|
||||||
"version": "1.0.20",
|
"version": "1.0.21",
|
||||||
"description": "Snort nostr system package",
|
"description": "Snort nostr system package",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
Loading…
Reference in New Issue
Block a user