Removal of localStorage dependency v1
This commit is contained in:
+31
-16
@@ -1,34 +1,49 @@
|
||||
# Synced Store
|
||||
|
||||
Utility for creating Svelte stores that automatically persist to and restore from localStorage.
|
||||
Utility for creating Svelte stores that automatically persist to and restore from storage providers.
|
||||
|
||||
## Functions
|
||||
|
||||
### synced(key, defaultValue)
|
||||
### synced(config)
|
||||
|
||||
Creates a writable store that synchronizes with localStorage using JSON serialization.
|
||||
Creates a writable store that synchronizes with a storage provider using JSON serialization.
|
||||
|
||||
**Parameters:**
|
||||
- `key` - localStorage key to store the value under
|
||||
- `defaultValue` - Default value if nothing exists in localStorage
|
||||
- `config` - Configuration object containing:
|
||||
- `key` - Storage key to store the value under
|
||||
- `storage` - Storage provider implementing the StorageProvider interface
|
||||
- `defaultValue` - Default value if nothing exists in storage
|
||||
|
||||
**Returns:** Writable Svelte store that persists changes to localStorage
|
||||
**Returns:** Writable Svelte store that persists changes to storage
|
||||
|
||||
The store automatically:
|
||||
- Loads initial value from localStorage on creation
|
||||
- Saves any changes back to localStorage
|
||||
- Falls back to defaultValue if localStorage is empty or invalid
|
||||
- Loads initial value from storage on creation
|
||||
- Saves any changes back to storage
|
||||
- Falls back to defaultValue if storage is empty or invalid
|
||||
|
||||
## Storage Provider Interface
|
||||
|
||||
```typescript
|
||||
interface StorageProvider {
|
||||
get: (key: string) => Promise<any>
|
||||
set: (key: string, value: any) => Promise<void>
|
||||
}
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```typescript
|
||||
import {synced} from "@welshman/store"
|
||||
import {synced, localStorageProvider} from "@welshman/store"
|
||||
|
||||
// Create a store that persists user preferences
|
||||
const userPreferences = synced("user-prefs", {
|
||||
theme: "dark",
|
||||
notifications: true,
|
||||
language: "en"
|
||||
// Create a store that persists user preferences using localStorage
|
||||
const userPreferences = synced({
|
||||
key: "user-prefs",
|
||||
storage: localStorageProvider,
|
||||
defaultValue: {
|
||||
theme: "dark",
|
||||
notifications: true,
|
||||
language: "en"
|
||||
}
|
||||
})
|
||||
|
||||
// Use like any writable store
|
||||
@@ -36,7 +51,7 @@ userPreferences.subscribe(prefs => {
|
||||
console.log("Preferences:", prefs)
|
||||
})
|
||||
|
||||
// Update the store - automatically saves to localStorage
|
||||
// Update the store - automatically saves to storage
|
||||
userPreferences.update(prefs => ({
|
||||
...prefs,
|
||||
theme: "light"
|
||||
|
||||
Reference in New Issue
Block a user