Deprecate stores
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
import {throttle} from "throttle-debounce"
|
import {throttle} from "throttle-debounce"
|
||||||
import {ensurePlural, identity} from "./Tools"
|
import {ensurePlural, identity} from "./Tools"
|
||||||
|
|
||||||
|
// Deprecated: use svelte's stores instead. I did all this to add a `get` convenience
|
||||||
|
// method that was more perfomant than subscribing/unsubscribing. That turned out to be
|
||||||
|
// a mistake, since that makes it much harder to make custom stores that don't run when
|
||||||
|
// there are no subscribers.
|
||||||
|
|
||||||
export type Invalidator<T> = (value?: T) => void
|
export type Invalidator<T> = (value?: T) => void
|
||||||
export type Subscriber<T> = (value: T) => void
|
export type Subscriber<T> = (value: T) => void
|
||||||
|
|
||||||
|
|||||||
+10
-20
@@ -1,5 +1,4 @@
|
|||||||
import {throttle} from 'throttle-debounce'
|
import {throttle} from 'throttle-debounce'
|
||||||
import type {ISubscribable, ISettable, Subscriber, Invalidator} from '@welshman/lib'
|
|
||||||
import {flatten, Emitter, sortBy, inc, chunk, sleep, uniq, omit, now, range, identity} from '@welshman/lib'
|
import {flatten, 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'
|
||||||
@@ -18,26 +17,31 @@ export type RepositoryOptions = {
|
|||||||
throttle?: number
|
throttle?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Repository extends Emitter implements ISubscribable<TrustedEvent[]>, ISettable<TrustedEvent[]> {
|
export class Repository extends Emitter {
|
||||||
eventsById = new Map<string, TrustedEvent>()
|
eventsById = new Map<string, TrustedEvent>()
|
||||||
eventsByAddress = new Map<string, TrustedEvent>()
|
eventsByAddress = new Map<string, TrustedEvent>()
|
||||||
eventsByTag = new Map<string, TrustedEvent[]>()
|
eventsByTag = new Map<string, TrustedEvent[]>()
|
||||||
eventsByDay = new Map<number, TrustedEvent[]>()
|
eventsByDay = new Map<number, TrustedEvent[]>()
|
||||||
eventsByAuthor = new Map<string, TrustedEvent[]>()
|
eventsByAuthor = new Map<string, TrustedEvent[]>()
|
||||||
deletes = new Map<string, number>()
|
deletes = new Map<string, number>()
|
||||||
subs: Subscriber<TrustedEvent[]>[] = []
|
|
||||||
|
|
||||||
constructor(private options: RepositoryOptions) {
|
constructor(private options: RepositoryOptions) {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methods for implementing store interface
|
// Compatibliity with stores
|
||||||
|
|
||||||
get() {
|
get = () => this.dump()
|
||||||
|
|
||||||
|
set = (events: TrustedEvent[], chunkSize = 1000) => this.load(events, chunkSize)
|
||||||
|
|
||||||
|
// Dump/load
|
||||||
|
|
||||||
|
dump() {
|
||||||
return Array.from(this.eventsById.values())
|
return Array.from(this.eventsById.values())
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(events: TrustedEvent[], chunkSize = 1000) {
|
async load(events: TrustedEvent[], chunkSize = 1000) {
|
||||||
this.clear()
|
this.clear()
|
||||||
|
|
||||||
for (const eventsChunk of chunk(chunkSize, events)) {
|
for (const eventsChunk of chunk(chunkSize, events)) {
|
||||||
@@ -60,21 +64,7 @@ export class Repository extends Emitter implements ISubscribable<TrustedEvent[]>
|
|||||||
this.deletes.clear()
|
this.deletes.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(f: Subscriber<TrustedEvent[]>, invalidate?: Invalidator<TrustedEvent[]>) {
|
|
||||||
this.subs.push(f)
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
this.subs = this.subs.filter(sub => sub !== f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
notifyUpdate = maybeThrottle(this.options.throttle, () => {
|
notifyUpdate = maybeThrottle(this.options.throttle, () => {
|
||||||
const events = this.get()
|
|
||||||
|
|
||||||
for (const sub of this.subs) {
|
|
||||||
sub(events)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('update')
|
this.emit('update')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user