create-packer 1.19.0 → 1.19.2
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
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { create, StoreApi
|
|
1
|
+
import { create, StoreApi } from 'zustand'
|
|
2
2
|
import { combine } from 'zustand/middleware'
|
|
3
3
|
import { produce } from 'immer'
|
|
4
|
-
import {
|
|
4
|
+
import { forEach, size } from 'lodash-es'
|
|
5
5
|
|
|
6
6
|
export interface insideActionsType<S> {
|
|
7
7
|
reset: () => void
|
|
@@ -12,16 +12,52 @@ export type actionsType<S, OptionAction, InsideActions = unknown> = (
|
|
|
12
12
|
actions: insideActionsType<S> & InsideActions,
|
|
13
13
|
store: StoreApi<S>
|
|
14
14
|
) => OptionAction
|
|
15
|
-
export interface optionsType<S, OptionAction> {
|
|
15
|
+
export interface optionsType<S, G, OptionAction> {
|
|
16
16
|
state: () => S
|
|
17
|
+
getter: () => G
|
|
17
18
|
actions: actionsType<S, OptionAction>
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
)
|
|
21
|
+
type GenGetterStateType<S, G extends Record<string, (state: S) => any>> = G extends Record<
|
|
22
|
+
infer K,
|
|
23
|
+
(state: S) => infer V
|
|
24
|
+
>
|
|
25
|
+
? Record<K, V>
|
|
26
|
+
: unknown
|
|
27
|
+
export function define<
|
|
28
|
+
S extends Record<string, any>,
|
|
29
|
+
G extends Record<string, (state: S) => any>,
|
|
30
|
+
OptionActions extends Record<string, any>
|
|
31
|
+
>(options: optionsType<S, G, OptionActions>) {
|
|
32
|
+
function createDefState() {
|
|
33
|
+
const state: any = options.state()
|
|
34
|
+
forEach(options.getter(), (getter, k) => {
|
|
35
|
+
state[k] = getter(state)
|
|
36
|
+
})
|
|
37
|
+
return state as S & GenGetterStateType<S, G>
|
|
38
|
+
}
|
|
23
39
|
return create(
|
|
24
|
-
combine(
|
|
40
|
+
combine(createDefState(), (set, get, store) => {
|
|
41
|
+
// getterListener
|
|
42
|
+
// ----------------------------------------------------------------------
|
|
43
|
+
store.subscribe(state => {
|
|
44
|
+
const oldGetterState: any = get()
|
|
45
|
+
let equalledLen = 0
|
|
46
|
+
const newGetterState: any = {}
|
|
47
|
+
forEach(options.getter(), (getter, k) => {
|
|
48
|
+
const value = getter(state)
|
|
49
|
+
if (value === oldGetterState[k]) {
|
|
50
|
+
equalledLen += 1
|
|
51
|
+
} else {
|
|
52
|
+
newGetterState[k] = getter(state)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
if (size(options.getter()) > equalledLen) {
|
|
56
|
+
set(newGetterState)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
// actions
|
|
60
|
+
// ----------------------------------------------------------------------
|
|
25
61
|
const setState: insideActionsType<S>['setState'] = updater => {
|
|
26
62
|
set(
|
|
27
63
|
produce((store: S) => {
|
|
@@ -33,7 +69,6 @@ export function define<S extends Record<string, any>, OptionActions extends Reco
|
|
|
33
69
|
set(() => options.state() as never)
|
|
34
70
|
}
|
|
35
71
|
return {
|
|
36
|
-
...options.state(),
|
|
37
72
|
reset,
|
|
38
73
|
setState,
|
|
39
74
|
subscribe: store.subscribe,
|
|
@@ -42,11 +77,3 @@ export function define<S extends Record<string, any>, OptionActions extends Reco
|
|
|
42
77
|
})
|
|
43
78
|
)
|
|
44
79
|
}
|
|
45
|
-
|
|
46
|
-
export function defineComputed<
|
|
47
|
-
Model extends UseBoundStore<StoreApi<any>>,
|
|
48
|
-
K extends string,
|
|
49
|
-
R = any
|
|
50
|
-
>(useStore: Model, computed: Record<K, (state: ExtractModelType<Model>) => R>) {
|
|
51
|
-
return (field: K) => useStore(computed[field])
|
|
52
|
-
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { create, StoreApi
|
|
1
|
+
import { create, StoreApi } from 'zustand'
|
|
2
2
|
import { combine } from 'zustand/middleware'
|
|
3
3
|
import { produce } from 'immer'
|
|
4
|
-
import {
|
|
4
|
+
import { forEach, size } from 'lodash-es'
|
|
5
5
|
|
|
6
6
|
export interface insideActionsType<S> {
|
|
7
7
|
reset: () => void
|
|
@@ -12,16 +12,52 @@ export type actionsType<S, OptionAction, InsideActions = unknown> = (
|
|
|
12
12
|
actions: insideActionsType<S> & InsideActions,
|
|
13
13
|
store: StoreApi<S>
|
|
14
14
|
) => OptionAction
|
|
15
|
-
export interface optionsType<S, OptionAction> {
|
|
15
|
+
export interface optionsType<S, G, OptionAction> {
|
|
16
16
|
state: () => S
|
|
17
|
+
getter: () => G
|
|
17
18
|
actions: actionsType<S, OptionAction>
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
)
|
|
21
|
+
type GenGetterStateType<S, G extends Record<string, (state: S) => any>> = G extends Record<
|
|
22
|
+
infer K,
|
|
23
|
+
(state: S) => infer V
|
|
24
|
+
>
|
|
25
|
+
? Record<K, V>
|
|
26
|
+
: unknown
|
|
27
|
+
export function define<
|
|
28
|
+
S extends Record<string, any>,
|
|
29
|
+
G extends Record<string, (state: S) => any>,
|
|
30
|
+
OptionActions extends Record<string, any>
|
|
31
|
+
>(options: optionsType<S, G, OptionActions>) {
|
|
32
|
+
function createDefState() {
|
|
33
|
+
const state: any = options.state()
|
|
34
|
+
forEach(options.getter(), (getter, k) => {
|
|
35
|
+
state[k] = getter(state)
|
|
36
|
+
})
|
|
37
|
+
return state as S & GenGetterStateType<S, G>
|
|
38
|
+
}
|
|
23
39
|
return create(
|
|
24
|
-
combine(
|
|
40
|
+
combine(createDefState(), (set, get, store) => {
|
|
41
|
+
// getterListener
|
|
42
|
+
// ----------------------------------------------------------------------
|
|
43
|
+
store.subscribe(state => {
|
|
44
|
+
const oldGetterState: any = get()
|
|
45
|
+
let equalledLen = 0
|
|
46
|
+
const newGetterState: any = {}
|
|
47
|
+
forEach(options.getter(), (getter, k) => {
|
|
48
|
+
const value = getter(state)
|
|
49
|
+
if (value === oldGetterState[k]) {
|
|
50
|
+
equalledLen += 1
|
|
51
|
+
} else {
|
|
52
|
+
newGetterState[k] = getter(state)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
if (size(options.getter()) > equalledLen) {
|
|
56
|
+
set(newGetterState)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
// actions
|
|
60
|
+
// ----------------------------------------------------------------------
|
|
25
61
|
const setState: insideActionsType<S>['setState'] = updater => {
|
|
26
62
|
set(
|
|
27
63
|
produce((store: S) => {
|
|
@@ -33,7 +69,6 @@ export function define<S extends Record<string, any>, OptionActions extends Reco
|
|
|
33
69
|
set(() => options.state() as never)
|
|
34
70
|
}
|
|
35
71
|
return {
|
|
36
|
-
...options.state(),
|
|
37
72
|
reset,
|
|
38
73
|
setState,
|
|
39
74
|
subscribe: store.subscribe,
|
|
@@ -42,11 +77,3 @@ export function define<S extends Record<string, any>, OptionActions extends Reco
|
|
|
42
77
|
})
|
|
43
78
|
)
|
|
44
79
|
}
|
|
45
|
-
|
|
46
|
-
export function defineComputed<
|
|
47
|
-
Model extends UseBoundStore<StoreApi<any>>,
|
|
48
|
-
K extends string,
|
|
49
|
-
R = any
|
|
50
|
-
>(useStore: Model, computed: Record<K, (state: ExtractModelType<Model>) => R>) {
|
|
51
|
-
return (field: K) => useStore(computed[field])
|
|
52
|
-
}
|