less idb queries

This commit is contained in:
Martti Malmi 2023-03-31 14:19:53 +03:00
parent 5cba067f31
commit df0a042379
2 changed files with 47 additions and 9 deletions

View File

@ -18,8 +18,10 @@ export class MyDexie extends Dexie {
const db = new MyDexie(); const db = new MyDexie();
export default { const IndexedDB = {
db, db,
subscribedEventIds: new Set<string>(),
subscribedAuthors: new Set<string>(),
subscriptions: new Set<string>(), subscriptions: new Set<string>(),
saveQueue: [] as Event[], saveQueue: [] as Event[],
clear() { clear() {
@ -78,28 +80,59 @@ export default {
// other events to be loaded on demand // other events to be loaded on demand
}, },
subscribeToAuthors: throttle((limit) => {
const authors = Array.from(IndexedDB.subscribedAuthors.values());
IndexedDB.subscribedAuthors.clear();
db.events
.where('pubkey')
.anyOf(authors)
.limit(limit || 1000)
.each((event) => {
Events.handle(event, false, false);
});
}, 500),
subscribeToEventIds: throttle(() => {
const ids = Array.from(IndexedDB.subscribedEventIds.values());
IndexedDB.subscribedEventIds.clear();
db.events
.where('id')
.anyOf(ids)
.each((event) => {
Events.handle(event, false, false);
});
}, 500),
subscribe(filter: Filter) { subscribe(filter: Filter) {
if (!filter) { if (!filter) {
return; return;
} }
if (filter['#e'] || filter['#p']) {
// currently inefficient lookups
return;
}
let query: any = db.events; let query: any = db.events;
if (filter.ids) { if (filter.ids) {
query = query.where('id').anyOf(filter.ids); filter.ids.forEach((id) => {
this.subscribedEventIds.add(id);
});
this.subscribeToEventIds();
return;
} else if (filter.authors) {
filter.authors.forEach((author) => {
this.subscribedAuthors.add(author);
});
this.subscribeToAuthors();
return;
} else { } else {
const stringifiedFilter = JSON.stringify(filter); const stringifiedFilter = JSON.stringify(filter);
if (this.subscriptions.has(stringifiedFilter)) { if (this.subscriptions.has(stringifiedFilter)) {
return; return;
} }
this.subscriptions.add(stringifiedFilter); this.subscriptions.add(stringifiedFilter);
if (filter.authors) {
query = query.where('pubkey').anyOf(filter.authors);
}
if (filter.kinds) { if (filter.kinds) {
query = query.where query = query.where
? query.where('kind').anyOf(filter.kinds) ? query.where('kind').anyOf(filter.kinds)
: query.and((event) => filter.kinds.includes(event.kind)); : query.and((event) => filter.kinds.includes(event.kind));
} }
query = query.filter(matchFilter);
if (filter.limit) { if (filter.limit) {
query = query.limit(filter.limit); // TODO these are not sorted by created_at desc query = query.limit(filter.limit); // TODO these are not sorted by created_at desc
} }
@ -109,3 +142,5 @@ export default {
}); });
}, },
}; };
export default IndexedDB;

View File

@ -94,14 +94,17 @@ const PubSub = {
if (filter.ids) { if (filter.ids) {
filter.ids = filter.ids.filter((id) => !Events.seen.has(id)); filter.ids = filter.ids.filter((id) => !Events.seen.has(id));
if (!filter.ids.length) {
return () => {
/* noop */
};
}
filter.ids.forEach((a) => { filter.ids.forEach((a) => {
this.subscribedEventIds.add(a); this.subscribedEventIds.add(a);
}); });
} }
setTimeout(() => { IndexedDB.subscribe(filter);
IndexedDB.subscribe(filter); // calls Events.handle which calls subscriptions with matching filters
});
// TODO if asking event by id or profile, ask http proxy // TODO if asking event by id or profile, ask http proxy