Update docs, tweak url based event derivation
This commit is contained in:
+10
-49
@@ -1,55 +1,16 @@
|
||||
# Getter
|
||||
|
||||
Utilities for adding synchronous `get()` methods to Svelte stores, allowing immediate value access without subscribing. Note that this has performance implications, since it will activate a subscription that will never get unsubscribed. Do not use this on stores that require complex calculations, or which are created and destroyed.
|
||||
|
||||
## Functions
|
||||
|
||||
### getter(store)
|
||||
|
||||
Creates a getter function that returns the current value of a store.
|
||||
|
||||
**Parameters:**
|
||||
- `store` - Any readable Svelte store
|
||||
|
||||
**Returns:** Function that returns the current store value
|
||||
|
||||
### withGetter(store)
|
||||
|
||||
Enhances a store by adding a synchronous `get()` method.
|
||||
|
||||
**Parameters:**
|
||||
- `store` - Readable or writable Svelte store
|
||||
|
||||
**Returns:** Store with added `get()` method
|
||||
|
||||
## Types
|
||||
|
||||
- `ReadableWithGetter<T>` - Readable store with `get()` method
|
||||
- `WritableWithGetter<T>` - Writable store with `get()` method
|
||||
|
||||
## Example
|
||||
Utility for creating optimized getter functions that adapt based on access patterns.
|
||||
|
||||
```typescript
|
||||
import {writable, derived} from "svelte/store"
|
||||
import {withGetter, getter} from "@welshman/store"
|
||||
// Create optimized getter that switches to subscription when hot
|
||||
getter<T>(store: Readable<T>, options?: {
|
||||
threshold?: number // Calls per second before switching to subscription (default: 10)
|
||||
}): () => T
|
||||
|
||||
// Create enhanced stores with getter methods
|
||||
const count = withGetter(writable(0))
|
||||
const doubled = withGetter(derived(count, $count => $count * 2))
|
||||
|
||||
// Access values synchronously without subscribing
|
||||
console.log(count.get()) // 0
|
||||
console.log(doubled.get()) // 0
|
||||
|
||||
// Update the store
|
||||
count.set(5)
|
||||
|
||||
// Get updated values immediately
|
||||
console.log(count.get()) // 5
|
||||
console.log(doubled.get()) // 10
|
||||
|
||||
// Alternative: create getter function separately
|
||||
const regularStore = writable(42)
|
||||
const getValue = getter(regularStore)
|
||||
console.log(getValue()) // 42
|
||||
// Add .get() method to a store
|
||||
withGetter<T>(store: Readable<T>): ReadableWithGetter<T>
|
||||
withGetter<T>(store: Writable<T>): WritableWithGetter<T>
|
||||
```
|
||||
|
||||
The `getter` function automatically switches between `get()` and subscription based on call frequency, optimizing performance for hot code paths.
|
||||
|
||||
Reference in New Issue
Block a user