Update nostr.ts & fix dangling promise of subscription channels (#202)

This commit is contained in:
BlowaterNostr 2023-09-27 19:49:23 +00:00 committed by GitHub
parent 919e4f672e
commit ea741e69ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 43 deletions

View File

@ -8,26 +8,15 @@ export class EventSyncer {
constructor(private readonly pool: ConnectionPool, private readonly db: Database_Contextual_View) {}
syncEvent(id: NoteID) {
const subID = EventSyncer.name + ":syncEvent";
for (const e of this.db.events) {
if (e.id == id.hex) {
return e;
}
}
return (async () => {
let events: Error | {
filter: NostrFilters;
chan: Channel<{
res: RelayResponse_REQ_Message;
url: string;
}>;
} = await this.pool.newSub("EventSyncer", {
ids: [id.hex],
});
if (events instanceof SubscriptionAlreadyExist) {
events = await this.pool.updateSub("EventSyncer", {
ids: [id.hex],
});
}
await this.pool.closeSub(subID);
let events = await this.pool.newSub(subID, { ids: [id.hex] });
if (events instanceof Error) {
return events;
}
@ -35,11 +24,6 @@ export class EventSyncer {
if (res.type != "EVENT") {
continue;
}
const ok = await verifyEvent(res.event);
if (!ok) {
console.warn(res.event, url, "not valid");
continue;
}
await this.db.addEvent(res.event);
return; // just need to read from 1 relay
}
@ -47,16 +31,8 @@ export class EventSyncer {
}
async syncEvents(filter: NostrFilters) {
let events: Error | {
filter: NostrFilters;
chan: Channel<{
res: RelayResponse_REQ_Message;
url: string;
}>;
} = await this.pool.newSub("syncEvents", filter);
if (events instanceof SubscriptionAlreadyExist) {
events = await this.pool.updateSub("syncEvents", filter);
}
await this.pool.closeSub(EventSyncer.name);
let events = await this.pool.newSub(EventSyncer.name, filter);
if (events instanceof Error) {
return events;
}
@ -64,11 +40,6 @@ export class EventSyncer {
if (res.type != "EVENT") {
continue;
}
const ok = await verifyEvent(res.event);
if (!ok) {
console.warn(res.event, url, "not valid");
continue;
}
await this.db.addEvent(res.event);
}
}

View File

@ -11,7 +11,8 @@ export class ProfilesSyncer {
constructor(
private readonly database: Database_Contextual_View,
private readonly pool: ConnectionPool,
) {}
) {
}
async add(...users: string[]) {
const size = this.userSet.size;
@ -21,13 +22,11 @@ export class ProfilesSyncer {
if (this.userSet.size == size) {
return;
}
const resp = await this.pool.updateSub(
"profilesStream",
{
authors: Array.from(this.userSet),
kinds: [NostrKind.META_DATA],
},
);
await this.pool.closeSub(ProfilesSyncer.name);
const resp = await this.pool.newSub(ProfilesSyncer.name, {
authors: Array.from(this.userSet),
kinds: [NostrKind.META_DATA],
});
if (resp instanceof Error) {
console.error(resp.message);
return;

@ -1 +1 @@
Subproject commit c1047e8348d89be7934516e1f5ddda26eb14b75c
Subproject commit 95603f6c6cca8b4affba80502a7906cca1f92bb3