CodeVetter · Repo Unpack · Behavior traces
pmndrs/zustand
The source supports concrete walks through initialization, counter updates, selective subscriptions, persistence, and external debugging. Synchronous persistence can affect the returned initial state, whereas asynchronous hydration completes after initialization. Clearing storage invalidates pending hydration but preserves live state.
- Startup: create(initializer) → createImpl → createStore → createStoreImpl → initializer(setState,getState,api) → assign state/initialState → attach api to useBoundStore.
- Starter action: Counter selects count/inc → button onClick invokes inc → functional setState increments count → listeners notify React → useStore reads the selected snapshot.
- Selective subscription: subscribe(selector,listener,options) captures currentSlice → optional immediate callback → origSubscribe registers wrapper → update computes nextSlice → unequal slices trigger listener.
- Synchronous persistence startup: persistImpl configures storage → invokes config → hydrate reads/parses storage → merge and set(...,true) → completion callback → returns stateFromStorage rather than configResult.
- Asynchronous hydration: hydrate increments hydrationVersion → bound getItem resolves → version mismatch may await migrate → generation check precedes merge/set → another check precedes completion signals; stale errors are also suppressed.
- Storage clearing: persist.clearStorage increments hydrationVersion before removeItem → delayed hydration sees a stale generation and skips application; the operation does not reset already hydrated live state.
- DevTools time travel: extension subscriber receives JUMP_TO_STATE/JUMP_TO_ACTION → parseJsonThen parses message.state → setStateFromDevtools disables recording → underlying set updates subscribers → recording is restored.
- Snippet flow: SnippetLang change calls setLang → CodePreview reads lang and getCode → selected source is highlighted and passed to CopyButton → handleCopy calls clipboard.writeText → success displays Copied! for three seconds.