Optimize repository loading
This commit is contained in:
+29
-28
@@ -1,7 +1,7 @@
|
|||||||
import {flatten, nth, Emitter, sortBy, inc, chunk, sleep, uniq, omit, now, range, identity} from '@welshman/lib'
|
import {flatten, nth, Emitter, sortBy, inc, chunk, sleep, uniq, omit, now, range, identity} from '@welshman/lib'
|
||||||
import {DELETE} from './Kinds'
|
import {DELETE} from './Kinds'
|
||||||
import {EPOCH, matchFilter} from './Filters'
|
import {EPOCH, matchFilter} from './Filters'
|
||||||
import {getIdAndAddress, isReplaceable, isTrustedEvent} from './Events'
|
import {isReplaceable, isTrustedEvent} from './Events'
|
||||||
import {getAddress} from './Address'
|
import {getAddress} from './Address'
|
||||||
import type {Filter} from './Filters'
|
import type {Filter} from './Filters'
|
||||||
import type {TrustedEvent} from './Events'
|
import type {TrustedEvent} from './Events'
|
||||||
@@ -11,6 +11,7 @@ export const DAY = 86400
|
|||||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||||
|
|
||||||
export class Repository extends Emitter {
|
export class Repository extends Emitter {
|
||||||
|
_shouldNotify = true
|
||||||
eventsById = new Map<string, TrustedEvent>()
|
eventsById = new Map<string, TrustedEvent>()
|
||||||
eventsByWrap = new Map<string, TrustedEvent>()
|
eventsByWrap = new Map<string, TrustedEvent>()
|
||||||
eventsByAddress = new Map<string, TrustedEvent>()
|
eventsByAddress = new Map<string, TrustedEvent>()
|
||||||
@@ -26,6 +27,8 @@ export class Repository extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
load = async (events: TrustedEvent[], chunkSize = 1000) => {
|
load = async (events: TrustedEvent[], chunkSize = 1000) => {
|
||||||
|
this._shouldNotify = false
|
||||||
|
|
||||||
this.clear()
|
this.clear()
|
||||||
|
|
||||||
for (const eventsChunk of chunk(chunkSize, events)) {
|
for (const eventsChunk of chunk(chunkSize, events)) {
|
||||||
@@ -37,6 +40,13 @@ export class Repository extends Emitter {
|
|||||||
await sleep(1)
|
await sleep(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.emit('update', {
|
||||||
|
added: events.filter(e => !this.isDeleted(e)),
|
||||||
|
removed: new Set(),
|
||||||
|
})
|
||||||
|
|
||||||
|
this._shouldNotify = false
|
||||||
}
|
}
|
||||||
|
|
||||||
clear = () => {
|
clear = () => {
|
||||||
@@ -49,16 +59,6 @@ export class Repository extends Emitter {
|
|||||||
this.deletes.clear()
|
this.deletes.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify methods
|
|
||||||
|
|
||||||
notifyEvent = (event: TrustedEvent) => {
|
|
||||||
this.emit('event', event)
|
|
||||||
}
|
|
||||||
|
|
||||||
notifyDelete = (values: string[]) => {
|
|
||||||
this.emit('delete', new Set(values))
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
getEvent = (idOrAddress: string) => {
|
getEvent = (idOrAddress: string) => {
|
||||||
@@ -147,23 +147,15 @@ export class Repository extends Emitter {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete our duplicate first
|
// Delete our duplicate
|
||||||
if (duplicate) {
|
if (duplicate) {
|
||||||
this.notifyDelete(getIdAndAddress(duplicate))
|
this.deletes.set(duplicate.id, event.created_at)
|
||||||
this.eventsById.delete(duplicate.id)
|
|
||||||
|
|
||||||
if (isReplaceable(duplicate)) {
|
|
||||||
this.eventsByAddress.delete(address)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now add our new event
|
// Add our new event by id
|
||||||
this.eventsById.set(event.id, event)
|
this.eventsById.set(event.id, event)
|
||||||
|
|
||||||
if (event.wrap) {
|
// Add our new event by address
|
||||||
this.eventsByWrap.set(event.wrap.id, event)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isReplaceable(event)) {
|
if (isReplaceable(event)) {
|
||||||
this.eventsByAddress.set(address, event)
|
this.eventsByAddress.set(address, event)
|
||||||
}
|
}
|
||||||
@@ -185,14 +177,23 @@ export class Repository extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.isDeleted(event)) {
|
// Notify caller
|
||||||
this.notifyEvent(event)
|
const added = this.isDeleted(event) ? [] : [event]
|
||||||
|
const removed = new Set<string>()
|
||||||
|
|
||||||
// Deletes are tricky, re-evaluate all subscriptions if that's what we're dealing with
|
if (duplicate) {
|
||||||
if (event.kind === DELETE) {
|
removed.add(duplicate!.id)
|
||||||
this.notifyDelete(event.tags.map(nth(1)))
|
}
|
||||||
|
|
||||||
|
if (event.kind === DELETE) {
|
||||||
|
for (const value of event.tags.map(nth(1))) {
|
||||||
|
removed.add(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this._shouldNotify) {
|
||||||
|
this.emit('update', {added, removed})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isDeleted = (event: TrustedEvent) => {
|
isDeleted = (event: TrustedEvent) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user