-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: 重构优化 ComponentAsset store 实现,收敛 AssetStore 的定义方式
- Loading branch information
Showing
9 changed files
with
105 additions
and
58 deletions.
There are no files selected for viewing
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
File renamed without changes.
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,2 +1,3 @@ | ||
export * from './ComponentAsset'; | ||
export { createUseAssetStore, type CreateAssetStore } from './store'; | ||
export * from './types'; |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { StateCreator, StoreApi, create } from 'zustand'; | ||
import { UseContextStore, createContext, optionalDevtools } from 'zustand-utils'; | ||
import { DevtoolsOptions } from 'zustand/middleware'; | ||
|
||
import { PublicProEditorStore } from '@/ProEditor/store'; | ||
|
||
export interface AssetStoreOptions<T = any> { | ||
initialState: T; | ||
devtools?: false | DevtoolsOptions; | ||
} | ||
|
||
export type CreateAssetStore<T> = StateCreator< | ||
T & PublicProEditorStore, | ||
[['zustand/devtools', never]], | ||
[], | ||
T | ||
>; | ||
|
||
const { Provider, useStore, useStoreApi } = createContext<StoreApi<any>>(); | ||
|
||
export const createAssetStore = <T>( | ||
createStore: StateCreator<T>, | ||
options?: AssetStoreOptions<T>, | ||
) => { | ||
const store = () => { | ||
const devtoolsOptions = options?.devtools; | ||
const devtools = optionalDevtools(!!devtoolsOptions); | ||
|
||
const initialState = options?.initialState || {}; | ||
|
||
return create<T>()( | ||
devtools( | ||
(...params) => ({ ...createStore(...params), ...initialState }), | ||
!devtoolsOptions ? {} : devtoolsOptions, | ||
), | ||
); | ||
}; | ||
|
||
return { Provider, createStore: store }; | ||
}; | ||
|
||
type WithoutCallSignature<T> = { | ||
[K in keyof T]: T[K]; | ||
}; | ||
|
||
export const createUseAssetStore = <T>(): { | ||
useStore: UseContextStore<StoreApi<T>>; | ||
useStoreApi: () => WithoutCallSignature<StoreApi<T>>; | ||
} => ({ useStore, useStoreApi }); |
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