increase idb throttle time, query by pubkey+kind index
This commit is contained in:
@ -20,6 +20,7 @@ class IndexedDB extends Dexie {
|
|||||||
private subscribedEventIds = new Set<string>();
|
private subscribedEventIds = new Set<string>();
|
||||||
private subscribedAuthors = new Set<string>();
|
private subscribedAuthors = new Set<string>();
|
||||||
private subscribedTags = new Set<string>();
|
private subscribedTags = new Set<string>();
|
||||||
|
private subscribedAuthorsAndKinds = new Set<string>();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super("EventDB");
|
super("EventDB");
|
||||||
@ -112,13 +113,13 @@ class IndexedDB extends Dexie {
|
|||||||
.anyOf(authors)
|
.anyOf(authors)
|
||||||
.limit(limit || 1000)
|
.limit(limit || 1000)
|
||||||
.each(callback);
|
.each(callback);
|
||||||
}, 100);
|
}, 200);
|
||||||
|
|
||||||
subscribeToEventIds = this._throttle(async function (callback: (event: TaggedNostrEvent) => void) {
|
subscribeToEventIds = this._throttle(async function (callback: (event: TaggedNostrEvent) => void) {
|
||||||
const ids = [...this.subscribedEventIds];
|
const ids = [...this.subscribedEventIds];
|
||||||
this.subscribedEventIds.clear();
|
this.subscribedEventIds.clear();
|
||||||
await this.events.where("id").anyOf(ids).each(callback);
|
await this.events.where("id").anyOf(ids).each(callback);
|
||||||
}, 100);
|
}, 200);
|
||||||
|
|
||||||
subscribeToTags = this._throttle(async function (callback: (event: TaggedNostrEvent) => void) {
|
subscribeToTags = this._throttle(async function (callback: (event: TaggedNostrEvent) => void) {
|
||||||
const tagPairs = [...this.subscribedTags].map(tag => tag.split("|"));
|
const tagPairs = [...this.subscribedTags].map(tag => tag.split("|"));
|
||||||
@ -129,13 +130,30 @@ class IndexedDB extends Dexie {
|
|||||||
.each(tag => this.subscribedEventIds.add(tag.eventId));
|
.each(tag => this.subscribedEventIds.add(tag.eventId));
|
||||||
|
|
||||||
await this.subscribeToEventIds(callback);
|
await this.subscribeToEventIds(callback);
|
||||||
}, 100);
|
}, 200);
|
||||||
|
|
||||||
|
subscribeToAuthorsAndKinds = this._throttle(async function (callback: (event: TaggedNostrEvent) => void) {
|
||||||
|
const authorsAndKinds = [...this.subscribedAuthorsAndKinds];
|
||||||
|
this.subscribedAuthorsAndKinds.clear();
|
||||||
|
// parse pair[1] as int
|
||||||
|
const pairs = authorsAndKinds.map(pair => {
|
||||||
|
const [author, kind] = pair.split("|");
|
||||||
|
return [author, parseInt(kind)];
|
||||||
|
});
|
||||||
|
await this.events
|
||||||
|
.where("[pubkey+kind]")
|
||||||
|
.anyOf(pairs)
|
||||||
|
.each(callback);
|
||||||
|
}, 200);
|
||||||
|
|
||||||
async find(filter: Filter, callback: (event: TaggedNostrEvent) => void): Promise<void> {
|
async find(filter: Filter, callback: (event: TaggedNostrEvent) => void): Promise<void> {
|
||||||
if (!filter) return;
|
if (!filter) return;
|
||||||
|
|
||||||
// make sure only 1 argument is passed
|
// make sure only 1 argument is passed
|
||||||
const cb = e => callback(e);
|
const cb = e => {
|
||||||
|
this.seenEvents.add(e.id);
|
||||||
|
callback(e);
|
||||||
|
}
|
||||||
|
|
||||||
if (filter["#p"] && Array.isArray(filter["#p"])) {
|
if (filter["#p"] && Array.isArray(filter["#p"])) {
|
||||||
for (const eventId of filter["#p"]) {
|
for (const eventId of filter["#p"]) {
|
||||||
@ -170,6 +188,13 @@ class IndexedDB extends Dexie {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filter.authors?.length && filter.kinds?.length) {
|
||||||
|
const permutations = filter.authors.flatMap(author => filter.kinds!.map(kind => author + "|" + kind));
|
||||||
|
permutations.forEach(permutation => this.subscribedAuthorsAndKinds.add(permutation));
|
||||||
|
await this.subscribeToAuthorsAndKinds(cb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (filter.authors?.length) {
|
if (filter.authors?.length) {
|
||||||
filter.authors.forEach(author => this.subscribedAuthors.add(author));
|
filter.authors.forEach(author => this.subscribedAuthors.add(author));
|
||||||
await this.subscribeToAuthors(cb);
|
await this.subscribeToAuthors(cb);
|
||||||
@ -189,7 +214,6 @@ class IndexedDB extends Dexie {
|
|||||||
}
|
}
|
||||||
// TODO test that the sort is actually working
|
// TODO test that the sort is actually working
|
||||||
await query.each(e => {
|
await query.each(e => {
|
||||||
this.seenEvents.add(e.id);
|
|
||||||
cb(e);
|
cb(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user