Collection
Utilities for creating reactive collections with automatic loading, caching, and staleness management using Svelte stores.
Functions
collection(options)
Creates a reactive collection that automatically loads missing items and manages freshness.
Options:
name
- Collection name for freshness trackingstore
- Readable store containing array of itemsgetKey
- Function to extract unique key from itemsload
- Async function to load missing items
Returns:
indexStore
- Derived store with items indexed by keyderiveItem(key, relays)
- Creates a derived store for a specific itemloadItem(key, relays)
- Manually loads an itemonItem(callback)
- Subscribe to individual item updates
makeCachedLoader(options)
Creates a cached loader function with staleness checking and exponential backoff.
Options:
name
- Loader name for freshness trackingindexStore
- Store containing indexed itemsload
- Async function to load itemssubscribers
- Array of item update subscribers
Freshness Management
getFreshness(ns, key)
- Get last update timestamp for an itemsetFreshnessImmediate(update)
- Immediately update freshnesssetFreshnessThrottled(update)
- Throttled freshness updates
Example
typescript
import {writable} from 'svelte/store'
import {derived, readable} from "svelte/store"
import {readProfile, PROFILE, PublishedProfile} from "@welshman/util"
import {Repository} from "@welshman/relay"
import {deriveEventsMapped, collection, withGetter} from "@welshman/store"
const repository = new Repository()
export const profiles = writable([])
export const {
indexStore: profilesByPubkey,
deriveItem: deriveProfile,
loadItem: loadProfile,
} = collection({
name: "profiles",
store: profiles,
getKey: profile => profile.event.pubkey,
load: (pubkey: string) => // Load the user's profile
})
// Get a reactive store for a specific profile
const hints = [/* optional relay hints to load from */]
const userProfile = deriveProfile("user-pubkey", hints)
// Subscribe to profile updates
userProfile.subscribe(profile => {
console.log("Profile updated:", profile)
})