-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: switch to zustand official persistence middleware
BREAKING CHANGE: The persistence feature will now be bound the the zustand persistence middleware API itself. Zfy's solution has been built when zustand did not provide that ability. Therefore, it has no reason to still exist now. This change also lead to the removal of `initZfy()` method as its not required anymore.
- Loading branch information
1 parent
55e6a96
commit 3b29dd6
Showing
14 changed files
with
194 additions
and
262 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { default as initZfy } from './init-zfy' | ||
export { default as PersistGate } from './PersistGate' | ||
export { default as createStore } from './create-store' | ||
export { default as useRehydrate } from './use-rehydrate' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import type { CreateStoreConfigType, StoreType } from '../../types' | ||
import { getConfig } from '../config' | ||
import type { StoreApi } from 'zustand' | ||
import { persist, StoreApiWithPersist } from 'zustand/middleware' | ||
|
||
const persist = | ||
< | ||
StoresDataType extends Record<string, any>, | ||
StoreNameType extends keyof StoresDataType | ||
>( | ||
store: StoreNameType, | ||
config: CreateStoreConfigType<StoresDataType[StoreNameType]> | ||
): CreateStoreConfigType<StoresDataType[StoreNameType]> => | ||
(set, get, api): StoreType<StoresDataType[StoreNameType]> => | ||
config( | ||
async (args) => { | ||
set(args) | ||
import type { | ||
StoreType, | ||
CreateStoreConfigType, | ||
CreateStoreOptionsType, | ||
} from '../../types' | ||
|
||
const { persistKey, serialize, storage } = getConfig() | ||
const middleware = < | ||
StoresDataType extends Record<string, any>, | ||
StoreNameType extends keyof StoresDataType | ||
>( | ||
storeName: StoreNameType, | ||
config: CreateStoreConfigType<StoresDataType[StoreNameType]>, | ||
options?: CreateStoreOptionsType<StoresDataType, StoreNameType> | ||
): CreateStoreConfigType< | ||
StoresDataType[StoreNameType], | ||
StoreApi<StoreType<StoresDataType[StoreNameType]>> & | ||
StoreApiWithPersist<StoreType<StoresDataType[StoreNameType]>> | ||
> => { | ||
const { | ||
name = storeName as Exclude<StoreNameType, number | symbol>, | ||
...rest | ||
} = options?.persist ?? {} | ||
|
||
if (typeof serialize === 'function') { | ||
Promise.resolve(serialize({ data: get().data })).then((data) => { | ||
storage?.setItem(`@${persistKey}Store:${store}`, data) | ||
}) | ||
} else { | ||
storage?.setItem(`@${persistKey}Store:${store}`, { | ||
data: get().data, | ||
}) | ||
} | ||
}, | ||
get, | ||
api | ||
) | ||
return persist((set, get, api) => config(set, get, api), { | ||
name, | ||
...(rest ? rest : {}), | ||
}) | ||
} | ||
|
||
export default persist | ||
export default middleware |
Oops, something went wrong.