)
}
```
## Performance Tips
### 1. Use Appropriate Debounce Times
- **Fast UI updates**: 100-200ms (search, filters)
- **Standard forms**: 300ms (default, recommended)
- **Non-critical data**: 500-1000ms (preferences, settings)
### 2. Batch Operations
```typescript
// ❌ Bad - Multiple individual operations
files.forEach(file => dispatch(saveFile(file)))
// ✅ Good - Single batch operation
dispatch(setFiles(files))
```
### 3. Disable During Bulk Operations
```typescript
import { disablePersistence, enablePersistence, flushPersistence } from '@/store/middleware'
async function importData(largeDataset) {
disablePersistence('files')
// Perform bulk operations without persistence overhead
largeDataset.forEach(item => dispatch(addItem(item)))
enablePersistence('files')
// Flush once after all operations
await flushPersistence()
}
```
## Common Patterns
### Create with Auto-Save
```typescript
const handleCreate = () => {
dispatch(saveFile({
id: generateId(),
name: 'new-file.js',
content: '',
language: 'javascript',
path: '/src/new-file.js',
updatedAt: Date.now()
}))
// Automatically persisted and synced!
}
```
### Update with Auto-Save
```typescript
const handleUpdate = (fileId, newContent) => {
dispatch(updateFile({
id: fileId,
content: newContent,
updatedAt: Date.now()
}))
// Automatically persisted and synced!
}
```
### Delete with Auto-Save
```typescript
const handleDelete = (fileId) => {
dispatch(deleteFile(fileId))
// Automatically removed from storage!
}
```
## Troubleshooting
### Data not persisting?
1. Check that the slice is configured in `persistenceMiddleware.ts`
2. Verify the action matches supported patterns
3. Check console for errors
### High failure rate?
1. Check Flask API is running
2. Verify network connection
3. Check IndexedDB quota not exceeded
### Slow performance?
1. Increase debounce time
2. Reduce sync frequency
3. Disable Flask sync for non-critical data
## Example: Complete CRUD Component
```typescript
import { useAppDispatch, useAppSelector } from '@/store'
import { saveFile, deleteFile } from '@/store/slices/filesSlice'
import { usePersistence } from '@/hooks/use-persistence'
function FileManager() {
const dispatch = useAppDispatch()
const files = useAppSelector((state) => state.files.files)
const { status } = usePersistence()
const create = (name, content) => {
dispatch(saveFile({
id: `file-${Date.now()}`,
name,
content,
language: 'javascript',
path: `/src/${name}`,
updatedAt: Date.now()
}))
}
const update = (id, content) => {
const file = files.find(f => f.id === id)
if (file) {
dispatch(saveFile({
...file,
content,
updatedAt: Date.now()
}))
}
}
const remove = (id) => {
dispatch(deleteFile(id))
}
return (
Status: {status.syncStatus}
{files.map(file => (
{file.name}
))}
)
}
```
## Learn More
- Full documentation: `PERSISTENCE_MIDDLEWARE_DOCS.md`
- View live dashboard: Navigate to **Persistence** page
- Try the demo: Navigate to **Persistence Demo** page