MCPcopy
hub / github.com/pmndrs/zustand / persistImpl

Function persistImpl

src/middleware/persist.ts:187–375  ·  view source on GitHub ↗
(config, baseOptions)

Source from the content-addressed store, hash-verified

185 }
186
187const persistImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
188 type S = ReturnType<typeof config>
189 let options = {
190 storage: createJSONStorage<S, void>(() => window.localStorage),
191 partialize: (state: S) => state,
192 version: 0,
193 merge: (persistedState: unknown, currentState: S) => ({
194 ...currentState,
195 ...(persistedState as object),
196 }),
197 ...baseOptions,
198 }
199
200 let hasHydrated = false
201 // Counter to track hydration versions and prevent race conditions
202 // when multiple rehydrate() calls happen concurrently
203 let hydrationVersion = 0
204 const hydrationListeners = new Set<PersistListener<S>>()
205 const finishHydrationListeners = new Set<PersistListener<S>>()
206 let storage = options.storage
207
208 if (!storage) {
209 return config(
210 (...args) => {
211 console.warn(
212 `[zustand persist middleware] Unable to update item '${options.name}', the given storage is currently unavailable.`,
213 )
214 set(...(args as Parameters<typeof set>))
215 },
216 get,
217 api,
218 )
219 }
220
221 const setItem = () => {
222 const state = options.partialize({ ...get() })
223 return (storage as PersistStorage<S, unknown>).setItem(options.name, {
224 state,
225 version: options.version,
226 })
227 }
228
229 const savedSetState = api.setState
230
231 api.setState = (state, replace) => {
232 savedSetState(state, replace as any)
233 return setItem()
234 }
235
236 const configResult = config(
237 (...args) => {
238 set(...(args as Parameters<typeof set>))
239 return setItem()
240 },
241 get,
242 api,
243 )
244

Callers

nothing calls this directly

Calls 3

createJSONStorageFunction · 0.85
hydrateFunction · 0.85
setItemFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…