create-packer 1.19.1 → 1.19.3

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