This commit is contained in:
@@ -24,46 +24,6 @@ export const projection = <T>($: Readable<T>, get = getter($)) => ({$, get})
|
|||||||
export const projectFrom = <S, U>(src: Projection<S>, read: ($: S) => U): Projection<U> =>
|
export const projectFrom = <S, U>(src: Projection<S>, read: ($: S) => U): Projection<U> =>
|
||||||
projection(derived(src.$, read), () => read(src.get()))
|
projection(derived(src.$, read), () => read(src.get()))
|
||||||
|
|
||||||
/**
|
|
||||||
* Synchronous read access to a keyed collection's current value. Shared by
|
|
||||||
* collections that own a map (`MapPlugin`) and those that are read-only views
|
|
||||||
* over the repository (`DerivedPlugin`).
|
|
||||||
*/
|
|
||||||
export interface ReadableMap<T> {
|
|
||||||
get(key: string): Maybe<T>
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Direct get/set access to a keyed collection the plugin owns. Extends the read
|
|
||||||
* accessors with mutation plus change notification. Only collections that own
|
|
||||||
* their map implement this — repository-backed collections are read-only.
|
|
||||||
*/
|
|
||||||
export interface Mappable<T> extends ReadableMap<T> {
|
|
||||||
set(key: string, value: T): void
|
|
||||||
delete(key: string): void
|
|
||||||
clear(): void
|
|
||||||
onItem(subscriber: (key: string, value: Maybe<T>) => void): Unsubscriber
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lazy, async loading of items by key from the network, with per-key caching
|
|
||||||
* and backoff (`load`) or a cache-bypassing refresh (`forceLoad`).
|
|
||||||
*/
|
|
||||||
export interface Loadable<T> {
|
|
||||||
load(key: string, ...args: any[]): Promise<Maybe<T>>
|
|
||||||
forceLoad(key: string, ...args: any[]): Promise<Maybe<T>>
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reactive derivations over a keyed collection: the whole map (`index`), its
|
|
||||||
* values (`all`), and a per-key on-demand store (`one`).
|
|
||||||
*/
|
|
||||||
export interface Derivable<T> {
|
|
||||||
index: Projection<ItemsByKey<T>>
|
|
||||||
all: Projection<T[]>
|
|
||||||
one(key?: string, ...args: any[]): Readable<Maybe<T>>
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for a reactive, keyed collection of "local" (non-event) data —
|
* Base class for a reactive, keyed collection of "local" (non-event) data —
|
||||||
* things like relay stats or NIP-11 profiles that aren't backed by the
|
* things like relay stats or NIP-11 profiles that aren't backed by the
|
||||||
@@ -73,7 +33,7 @@ export interface Derivable<T> {
|
|||||||
* snapshot via `.get()`. Per-key access is `one(key)`, a plain on-demand store
|
* snapshot via `.get()`. Per-key access is `one(key)`, a plain on-demand store
|
||||||
* (snapshot with svelte's `get(...)`, or read `get(key)` directly).
|
* (snapshot with svelte's `get(...)`, or read `get(key)` directly).
|
||||||
*/
|
*/
|
||||||
export class MapPlugin<T> implements Mappable<T>, Derivable<T> {
|
export class MapPlugin<T> {
|
||||||
protected store = writable(new Map<string, T>())
|
protected store = writable(new Map<string, T>())
|
||||||
index: Projection<ItemsByKey<T>>
|
index: Projection<ItemsByKey<T>>
|
||||||
all: Projection<T[]>
|
all: Projection<T[]>
|
||||||
@@ -143,7 +103,7 @@ export class MapPlugin<T> implements Mappable<T>, Derivable<T> {
|
|||||||
* network. Subclasses implement `fetch`; `load`/`forceLoad`/`one` are derived
|
* network. Subclasses implement `fetch`; `load`/`forceLoad`/`one` are derived
|
||||||
* from it (with per-key caching and backoff via `makeLoadItem`).
|
* from it (with per-key caching and backoff via `makeLoadItem`).
|
||||||
*/
|
*/
|
||||||
export abstract class LoadableMapPlugin<T> extends MapPlugin<T> implements Loadable<T> {
|
export abstract class LoadableMapPlugin<T> extends MapPlugin<T> {
|
||||||
load: (key: string, ...args: any[]) => Promise<Maybe<T>>
|
load: (key: string, ...args: any[]) => Promise<Maybe<T>>
|
||||||
forceLoad: (key: string, ...args: any[]) => Promise<Maybe<T>>
|
forceLoad: (key: string, ...args: any[]) => Promise<Maybe<T>>
|
||||||
|
|
||||||
@@ -181,7 +141,7 @@ export type DerivedPluginOptions<T> = {
|
|||||||
* `index` (map) and `all` (values) are `Projection`s — subscribe via `.$`,
|
* `index` (map) and `all` (values) are `Projection`s — subscribe via `.$`,
|
||||||
* snapshot via `.get()`. Per-key access is `one(key)`, a plain on-demand store.
|
* snapshot via `.get()`. Per-key access is `one(key)`, a plain on-demand store.
|
||||||
*/
|
*/
|
||||||
export abstract class DerivedPlugin<T> implements ReadableMap<T>, Loadable<T>, Derivable<T> {
|
export abstract class DerivedPlugin<T> {
|
||||||
index: Projection<ItemsByKey<T>>
|
index: Projection<ItemsByKey<T>>
|
||||||
all: Projection<T[]>
|
all: Projection<T[]>
|
||||||
one: (key?: string, ...args: any[]) => Readable<Maybe<T>>
|
one: (key?: string, ...args: any[]) => Readable<Maybe<T>>
|
||||||
|
|||||||
Reference in New Issue
Block a user