create-packer 1.17.2 → 1.17.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { create } from 'zustand'
|
|
2
|
+
import { produce } from 'immer'
|
|
3
|
+
|
|
4
|
+
export type setStateType<S> = (updater: (state: S) => void) => void
|
|
5
|
+
export interface modelActionsType {
|
|
6
|
+
reset: () => void
|
|
7
|
+
}
|
|
8
|
+
export interface modelOptionsType<S extends Record<string, any>, A extends Record<string, any>> {
|
|
9
|
+
state: () => S
|
|
10
|
+
actions: (setState: setStateType<S>, getState: () => S, actions: modelActionsType) => A
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function defineModel<
|
|
14
|
+
S extends Record<string, any>,
|
|
15
|
+
A extends Record<string, any> = Record<string, any>
|
|
16
|
+
>(options: modelOptionsType<S, A>) {
|
|
17
|
+
return create<{ state: S; actions: A }>()((setState, getState, store) => {
|
|
18
|
+
const _setState: setStateType<S> = updater => {
|
|
19
|
+
setState(
|
|
20
|
+
produce((store: { state: S }) => {
|
|
21
|
+
updater(store.state)
|
|
22
|
+
}) as any
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
const _getState = () => getState().state
|
|
26
|
+
const reset: modelActionsType['reset'] = () => {
|
|
27
|
+
setState({ state: options.state() })
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
state: options.state(),
|
|
31
|
+
actions: {
|
|
32
|
+
reset,
|
|
33
|
+
setState: _setState,
|
|
34
|
+
subscribe: store.subscribe,
|
|
35
|
+
...options.actions(_setState, _getState, { reset })
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './defineModel'
|
|
2
2
|
export { default as request } from './request'
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { create } from 'zustand'
|
|
2
|
-
import { combine } from 'zustand/middleware'
|
|
3
|
-
import { produce } from 'immer'
|
|
4
|
-
|
|
5
|
-
type setStateType<S> = (updater: (state: S) => void) => void
|
|
6
|
-
export interface defineStoreOptionsType<S extends object, A extends object> {
|
|
7
|
-
state: () => S
|
|
8
|
-
actions: (
|
|
9
|
-
setState: setStateType<S>,
|
|
10
|
-
getState: () => S,
|
|
11
|
-
getActions: () => Record<string, any>
|
|
12
|
-
) => A
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function defineStore<S extends object, A extends object>(
|
|
16
|
-
options: defineStoreOptionsType<S, A>
|
|
17
|
-
) {
|
|
18
|
-
return create(
|
|
19
|
-
combine({ state: options.state() }, (setState, getState, store) => {
|
|
20
|
-
const _setState: setStateType<S> = updater => {
|
|
21
|
-
setState(
|
|
22
|
-
produce((store: { state: S }) => {
|
|
23
|
-
updater(store.state)
|
|
24
|
-
}) as any
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
|
-
const _getState = () => getState().state
|
|
28
|
-
const _getActions = () => (getState() as any).actions
|
|
29
|
-
return {
|
|
30
|
-
actions: {
|
|
31
|
-
reset: () => {
|
|
32
|
-
setState({ state: options.state() })
|
|
33
|
-
},
|
|
34
|
-
setState: _setState,
|
|
35
|
-
destroy: store.destroy,
|
|
36
|
-
subscribe: store.subscribe,
|
|
37
|
-
...options.actions(_setState, _getState, _getActions)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
)
|
|
42
|
-
}
|