Writable stores are created using writable() from svelte/store. Example: const count = writable(0). They provide set() and update() methods to modify values, and subscribe() to react to changes.
Derived stores are created using derived() and compute values based on other stores. Example: const doubled = derived(count, $count => $count * 2). Update automatically when source stores change.
Auto-subscription happens when using $ prefix with stores in components. Svelte automatically handles subscribe and unsubscribe. No manual cleanup needed. Works in template and script.
Stores are initialized with initial value in creation. Example: writable(initialValue). Can be undefined. Support synchronous and asynchronous initialization.
Store middleware intercepts store operations. Implement custom behaviors. Handle side effects. Example: Create wrapper store with logging or validation.
Store monitoring tracks store usage. Implement metrics collection. Handle debugging. Example: Create monitoring wrapper for stores, track operations.
Stores in Svelte are reactive data containers that can be shared across components. They provide a way to manage global state and notify subscribers when data changes. Created using writable(), readable(), or derived().
Store values can be updated using set() or update() methods. Example: count.set(10) or count.update(n => n + 1). Updates notify all subscribers automatically.
Store error handling catches and processes errors. Implement error states. Support error recovery. Example: Try-catch in store operations, maintain error state.
Advanced patterns include state machines, event sourcing, command pattern. Handle complex state management. Example: Implement store as state machine.
Store time travel tracks state history. Implement undo/redo. Handle state snapshots. Example: Maintain history of states, implement restore functionality.
Store performance optimization includes selective updates, memoization, lazy loading. Handle large datasets. Example: Implement selective update mechanism.
The store contract requires an object with subscribe method that takes a callback and returns unsubscribe function. Any object meeting this contract can be used as a store with $ prefix.
Store changes can be subscribed to using subscribe method or $ prefix in components. Example: count.subscribe(value => console.log(value)) or use $count directly in template.
Readable stores are created using readable() and are read-only. They can only be updated through their start function. Useful for external data sources that components shouldn't modify.
Async stores handle asynchronous data. Support loading states. Handle errors. Example: const data = writable(null); async function fetchData() { const response = await fetch(url); data.set(await response.json()); }
Store dependencies use derived stores or reactive statements. Handle update order. Manage circular dependencies. Support dependency tracking.
Store optimization includes batching updates, debouncing, throttling. Handle performance bottlenecks. Example: Implement update batching or debounced updates.
Store migrations handle version changes. Implement data transforms. Support backwards compatibility. Example: Define migration strategies between versions.
Stores integrate with Svelte's reactivity system. Changes trigger reactive updates. Work with reactive declarations ($:). Support reactive dependencies tracking.
Custom stores implement subscribe method. Can add custom methods. Handle internal state. Example: function createCustomStore() { const { subscribe, set } = writable(0); return { subscribe, increment: () => update(n => n + 1) }; }
Store persistence saves state to storage. Implement auto-save. Handle state rehydration. Example: Subscribe to changes and save to localStorage. Load initial state from storage.
Store validation ensures valid state. Implement validation rules. Handle validation errors. Example: Create wrapper store that validates updates.
Store composition combines multiple stores. Create higher-order stores. Handle store relationships. Example: Combine multiple stores into single interface.
Store lifecycle management handles initialization, updates, cleanup. Support store creation/destruction. Example: Implement cleanup in onDestroy, handle store initialization.
Store synchronization handles multiple instances. Implement cross-tab sync. Handle conflicts. Example: Use broadcast channel for cross-tab communication.
Store encryption secures sensitive data. Implement encryption/decryption. Handle key management. Example: Encrypt data before storage, decrypt on retrieval.
Store testing verifies store behavior. Implement test utilities. Handle async testing. Example: Create test helpers, mock store functionality.
Store documentation describes store usage. Generate documentation automatically. Support examples. Example: Create documentation generator for stores.
Store security prevents unauthorized access. Implement access control. Handle sensitive data. Example: Create secure store wrapper with access checks.