첫 커밋
This commit is contained in:
46
src/shared/model/store.ts
Normal file
46
src/shared/model/store.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { persistOptions, withLenses } from '@dhmk/zustand-lens';
|
||||
import { create } from 'zustand';
|
||||
import { devtools, persist } from 'zustand/middleware';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
import { createUtilEventSlice, UtilEventState } from '@/entities/redirect/model/store';
|
||||
import { createUserInfoStore, UserInfoState } from '@/entities/user/model/store';
|
||||
import { StorageKeys } from '@/shared/constants/local-storage';
|
||||
|
||||
export type RootStore = {
|
||||
UserStore: UserInfoState;
|
||||
};
|
||||
export const useStore = create<RootStore>()(
|
||||
devtools(
|
||||
persist(
|
||||
immer(
|
||||
withLenses(() => ({
|
||||
UserStore: createUserInfoStore,
|
||||
utilEventSlice: createUtilEventSlice,
|
||||
})),
|
||||
),
|
||||
{
|
||||
name: StorageKeys.RootStore,
|
||||
version: 1,
|
||||
...persistOptions,
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
type StoreWithPersist = typeof useStore;
|
||||
|
||||
export const withStorageDOMEvents = (store: StoreWithPersist) => {
|
||||
const storageEventCallback = (e: StorageEvent) => {
|
||||
if (e.key === store.persist.getOptions().name && e.newValue) {
|
||||
store.persist.rehydrate();
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('storage', storageEventCallback);
|
||||
return () => {
|
||||
window.removeEventListener('storage', storageEventCallback);
|
||||
};
|
||||
};
|
||||
|
||||
withStorageDOMEvents(useStore);
|
||||
window.store = useStore;
|
||||
Reference in New Issue
Block a user