Skip to content

Commit

Permalink
Merge branch 'main' into use-sync-external-store
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Apr 7, 2022
2 parents 748caeb + 34bf82b commit 1be05d1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 88 deletions.
38 changes: 2 additions & 36 deletions src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ type DeepPartial<T> = {
export type StateStorage = {
getItem: (name: string) => string | null | Promise<string | null>
setItem: (name: string, value: string) => void | Promise<void>
// Note: This will be required in v4
removeItem?: (name: string) => void | Promise<void>
removeItem: (name: string) => void | Promise<void>
}

type StorageValue<S> = { state: DeepPartial<S>; version?: number }
Expand Down Expand Up @@ -44,18 +43,6 @@ export type PersistOptions<
deserialize?: (
str: string
) => StorageValue<PersistedState> | Promise<StorageValue<PersistedState>>
/**
* Prevent some items from being stored.
*
* @deprecated This options is deprecated and will be removed in the next version. Please use the `partialize` option instead.
*/
blacklist?: (keyof S)[]
/**
* Only store the listed properties.
*
* @deprecated This options is deprecated and will be removed in the next version. Please use the `partialize` option instead.
*/
whitelist?: (keyof S)[]
/**
* Filter the persisted value.
*
Expand Down Expand Up @@ -189,14 +176,6 @@ export const persist =
...baseOptions,
}

if (options.blacklist || options.whitelist) {
console.warn(
`The ${
options.blacklist ? 'blacklist' : 'whitelist'
} option is deprecated and will be removed in the next version. Please use the 'partialize' option instead.`
)
}

let hasHydrated = false
const hydrationListeners = new Set<PersistListener<S>>()
const finishHydrationListeners = new Set<PersistListener<S>>()
Expand All @@ -219,26 +198,13 @@ export const persist =
get,
api
)
} else if (!storage.removeItem) {
console.warn(
`[zustand persist middleware] The given storage for item '${options.name}' does not contain a 'removeItem' method, which will be required in v4.`
)
}

const thenableSerialize = toThenable(options.serialize)

const setItem = (): Thenable<void> => {
const state = options.partialize({ ...get() })

if (options.whitelist) {
;(Object.keys(state) as (keyof S)[]).forEach((key) => {
!options.whitelist?.includes(key) && delete state[key]
})
}
if (options.blacklist) {
options.blacklist.forEach((key) => delete state[key])
}

let errorInSync: Error | undefined
const thenable = thenableSerialize({ state, version: options.version })
.then((serializedValue) =>
Expand Down Expand Up @@ -342,7 +308,7 @@ export const persist =
}
},
clearStorage: () => {
storage?.removeItem?.(options.name)
storage?.removeItem(options.name)
},
rehydrate: () => hydrate() as Promise<void>,
hasHydrated: () => hasHydrated,
Expand Down
14 changes: 3 additions & 11 deletions src/middleware/subscribeWithSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ interface StoreSubscribeWithSelector<T extends State> {
* @deprecated Use `Mutate<StoreApi<T>, [["zustand/subscribeWithSelector", never]]>`.
* See tests/middlewaresTypes.test.tsx for usage with multiple middlewares.
*/
export type StoreApiWithSubscribeWithSelector<T extends State> = Omit<
StoreApi<T>,
'subscribe' // FIXME remove omit in v4
> & {
export type StoreApiWithSubscribeWithSelector<T extends State> = StoreApi<T> & {
subscribe: {
(listener: StateListener<T>): () => void
<StateSlice>(
Expand All @@ -68,8 +65,7 @@ export const subscribeWithSelector =
(
set: CustomSetState,
get: CustomGetState,
api: Omit<CustomStoreApi, 'subscribe'> & // FIXME remove omit in v4
StoreApiWithSubscribeWithSelector<S>
api: CustomStoreApi & StoreApiWithSubscribeWithSelector<S>
): S => {
const origSubscribe = api.subscribe as Subscribe<S>
api.subscribe = ((selector: any, optListener: any, options: any) => {
Expand All @@ -90,10 +86,6 @@ export const subscribeWithSelector =
}
return origSubscribe(listener)
}) as any
const initialState = fn(
set,
get,
api as CustomStoreApi // FIXME can remove in v4?
)
const initialState = fn(set, get, api)
return initialState
}
44 changes: 3 additions & 41 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ export type StateListener<T> = (state: T, previousState: T) => void
export type StateSliceListener<T> = (slice: T, previousSlice: T) => void
export type Subscribe<T extends State> = {
(listener: StateListener<T>): () => void
/**
* @deprecated Please use `subscribeWithSelector` middleware
*/
<StateSlice>(
listener: StateSliceListener<StateSlice>,
selector?: StateSelector<T, StateSlice>,
equalityFn?: EqualityChecker<StateSlice>
): () => void
}

export type SetState<T extends State> = {
Expand Down Expand Up @@ -113,40 +105,10 @@ function createStore<

const getState: GetState<TState> = () => state

const subscribeWithSelector = <StateSlice>(
listener: StateSliceListener<StateSlice>,
selector: StateSelector<TState, StateSlice> = getState as any,
equalityFn: EqualityChecker<StateSlice> = Object.is
) => {
console.warn('[DEPRECATED] Please use `subscribeWithSelector` middleware')
let currentSlice: StateSlice = selector(state)
function listenerToAdd() {
const nextSlice = selector(state)
if (!equalityFn(currentSlice, nextSlice)) {
const previousSlice = currentSlice
listener((currentSlice = nextSlice), previousSlice)
}
}
listeners.add(listenerToAdd)
// Unsubscribe
return () => listeners.delete(listenerToAdd)
}

const subscribe: Subscribe<TState> = <StateSlice>(
listener: StateListener<TState> | StateSliceListener<StateSlice>,
selector?: StateSelector<TState, StateSlice>,
equalityFn?: EqualityChecker<StateSlice>
) => {
if (selector || equalityFn) {
return subscribeWithSelector(
listener as StateSliceListener<StateSlice>,
selector,
equalityFn
)
}
listeners.add(listener as StateListener<TState>)
const subscribe: Subscribe<TState> = (listener: StateListener<TState>) => {
listeners.add(listener)
// Unsubscribe
return () => listeners.delete(listener as StateListener<TState>)
return () => listeners.delete(listener)
}

const destroy: Destroy = () => listeners.clear()
Expand Down

0 comments on commit 1be05d1

Please sign in to comment.