Skip to content

Commit 1be05d1

Browse files
committed
Merge branch 'main' into use-sync-external-store
2 parents 748caeb + 34bf82b commit 1be05d1

3 files changed

Lines changed: 8 additions & 88 deletions

File tree

src/middleware/persist.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ type DeepPartial<T> = {
77
export type StateStorage = {
88
getItem: (name: string) => string | null | Promise<string | null>
99
setItem: (name: string, value: string) => void | Promise<void>
10-
// Note: This will be required in v4
11-
removeItem?: (name: string) => void | Promise<void>
10+
removeItem: (name: string) => void | Promise<void>
1211
}
1312

1413
type StorageValue<S> = { state: DeepPartial<S>; version?: number }
@@ -44,18 +43,6 @@ export type PersistOptions<
4443
deserialize?: (
4544
str: string
4645
) => StorageValue<PersistedState> | Promise<StorageValue<PersistedState>>
47-
/**
48-
* Prevent some items from being stored.
49-
*
50-
* @deprecated This options is deprecated and will be removed in the next version. Please use the `partialize` option instead.
51-
*/
52-
blacklist?: (keyof S)[]
53-
/**
54-
* Only store the listed properties.
55-
*
56-
* @deprecated This options is deprecated and will be removed in the next version. Please use the `partialize` option instead.
57-
*/
58-
whitelist?: (keyof S)[]
5946
/**
6047
* Filter the persisted value.
6148
*
@@ -189,14 +176,6 @@ export const persist =
189176
...baseOptions,
190177
}
191178

192-
if (options.blacklist || options.whitelist) {
193-
console.warn(
194-
`The ${
195-
options.blacklist ? 'blacklist' : 'whitelist'
196-
} option is deprecated and will be removed in the next version. Please use the 'partialize' option instead.`
197-
)
198-
}
199-
200179
let hasHydrated = false
201180
const hydrationListeners = new Set<PersistListener<S>>()
202181
const finishHydrationListeners = new Set<PersistListener<S>>()
@@ -219,26 +198,13 @@ export const persist =
219198
get,
220199
api
221200
)
222-
} else if (!storage.removeItem) {
223-
console.warn(
224-
`[zustand persist middleware] The given storage for item '${options.name}' does not contain a 'removeItem' method, which will be required in v4.`
225-
)
226201
}
227202

228203
const thenableSerialize = toThenable(options.serialize)
229204

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

233-
if (options.whitelist) {
234-
;(Object.keys(state) as (keyof S)[]).forEach((key) => {
235-
!options.whitelist?.includes(key) && delete state[key]
236-
})
237-
}
238-
if (options.blacklist) {
239-
options.blacklist.forEach((key) => delete state[key])
240-
}
241-
242208
let errorInSync: Error | undefined
243209
const thenable = thenableSerialize({ state, version: options.version })
244210
.then((serializedValue) =>
@@ -342,7 +308,7 @@ export const persist =
342308
}
343309
},
344310
clearStorage: () => {
345-
storage?.removeItem?.(options.name)
311+
storage?.removeItem(options.name)
346312
},
347313
rehydrate: () => hydrate() as Promise<void>,
348314
hasHydrated: () => hasHydrated,

src/middleware/subscribeWithSelector.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ interface StoreSubscribeWithSelector<T extends State> {
3939
* @deprecated Use `Mutate<StoreApi<T>, [["zustand/subscribeWithSelector", never]]>`.
4040
* See tests/middlewaresTypes.test.tsx for usage with multiple middlewares.
4141
*/
42-
export type StoreApiWithSubscribeWithSelector<T extends State> = Omit<
43-
StoreApi<T>,
44-
'subscribe' // FIXME remove omit in v4
45-
> & {
42+
export type StoreApiWithSubscribeWithSelector<T extends State> = StoreApi<T> & {
4643
subscribe: {
4744
(listener: StateListener<T>): () => void
4845
<StateSlice>(
@@ -68,8 +65,7 @@ export const subscribeWithSelector =
6865
(
6966
set: CustomSetState,
7067
get: CustomGetState,
71-
api: Omit<CustomStoreApi, 'subscribe'> & // FIXME remove omit in v4
72-
StoreApiWithSubscribeWithSelector<S>
68+
api: CustomStoreApi & StoreApiWithSubscribeWithSelector<S>
7369
): S => {
7470
const origSubscribe = api.subscribe as Subscribe<S>
7571
api.subscribe = ((selector: any, optListener: any, options: any) => {
@@ -90,10 +86,6 @@ export const subscribeWithSelector =
9086
}
9187
return origSubscribe(listener)
9288
}) as any
93-
const initialState = fn(
94-
set,
95-
get,
96-
api as CustomStoreApi // FIXME can remove in v4?
97-
)
89+
const initialState = fn(set, get, api)
9890
return initialState
9991
}

src/vanilla.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ export type StateListener<T> = (state: T, previousState: T) => void
2525
export type StateSliceListener<T> = (slice: T, previousSlice: T) => void
2626
export type Subscribe<T extends State> = {
2727
(listener: StateListener<T>): () => void
28-
/**
29-
* @deprecated Please use `subscribeWithSelector` middleware
30-
*/
31-
<StateSlice>(
32-
listener: StateSliceListener<StateSlice>,
33-
selector?: StateSelector<T, StateSlice>,
34-
equalityFn?: EqualityChecker<StateSlice>
35-
): () => void
3628
}
3729

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

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

116-
const subscribeWithSelector = <StateSlice>(
117-
listener: StateSliceListener<StateSlice>,
118-
selector: StateSelector<TState, StateSlice> = getState as any,
119-
equalityFn: EqualityChecker<StateSlice> = Object.is
120-
) => {
121-
console.warn('[DEPRECATED] Please use `subscribeWithSelector` middleware')
122-
let currentSlice: StateSlice = selector(state)
123-
function listenerToAdd() {
124-
const nextSlice = selector(state)
125-
if (!equalityFn(currentSlice, nextSlice)) {
126-
const previousSlice = currentSlice
127-
listener((currentSlice = nextSlice), previousSlice)
128-
}
129-
}
130-
listeners.add(listenerToAdd)
131-
// Unsubscribe
132-
return () => listeners.delete(listenerToAdd)
133-
}
134-
135-
const subscribe: Subscribe<TState> = <StateSlice>(
136-
listener: StateListener<TState> | StateSliceListener<StateSlice>,
137-
selector?: StateSelector<TState, StateSlice>,
138-
equalityFn?: EqualityChecker<StateSlice>
139-
) => {
140-
if (selector || equalityFn) {
141-
return subscribeWithSelector(
142-
listener as StateSliceListener<StateSlice>,
143-
selector,
144-
equalityFn
145-
)
146-
}
147-
listeners.add(listener as StateListener<TState>)
108+
const subscribe: Subscribe<TState> = (listener: StateListener<TState>) => {
109+
listeners.add(listener)
148110
// Unsubscribe
149-
return () => listeners.delete(listener as StateListener<TState>)
111+
return () => listeners.delete(listener)
150112
}
151113

152114
const destroy: Destroy = () => listeners.clear()

0 commit comments

Comments
 (0)