You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: PERSISTENCE_ARCHITECTURE.md
+140Lines changed: 140 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -746,6 +746,146 @@ Each module can persist two separate state objects:
746
746
747
747
Both use the same pattern: define a serialized state type, create a JTD schema, implement serialize/deserialize functions, and register with the module.
748
748
749
+
### Understanding persistableFixableAtom
750
+
751
+
The `persistableFixableAtom` utility provides state management for values that need validation and automatic correction when loaded from persistence or when dependencies change. It's essential for robust module state persistence.
752
+
753
+
#### Core Features
754
+
755
+
**1. Source Tracking**
756
+
757
+
Every value has a source that determines how invalid values are handled:
758
+
-`Source.USER`: Value set by direct user interaction → auto-fixes when invalid
759
+
-`Source.PERSISTENCE`: Value loaded from saved session → shows warnings when invalid
760
+
-`Source.TEMPLATE`: Value loaded from template → same behavior as PERSISTENCE
761
+
762
+
**2. Validation & Fixup**
763
+
764
+
```typescript
765
+
persistableFixableAtom({
766
+
initialValue: defaultValue,
767
+
768
+
// Check if value is valid in current context
769
+
isValidFunction: ({ value, get }) => {
770
+
// Return true if value is valid, false otherwise
771
+
// Can read other atoms via get()
772
+
},
773
+
774
+
// Provide fallback when USER-source value is invalid
775
+
fixupFunction: ({ value, get }) => {
776
+
// Return a valid replacement value
777
+
// Only called for USER source, never for PERSISTENCE
778
+
},
779
+
})
780
+
```
781
+
782
+
**3. Auto-Transition Behavior**
783
+
784
+
When a value with `Source.PERSISTENCE` or `Source.TEMPLATE` becomes valid, it automatically transitions to `Source.USER`:
785
+
786
+
- Transition happens in next microtask after validation passes
0 commit comments