create-packer 1.19.2 → 1.19.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-packer",
3
- "version": "1.19.2",
3
+ "version": "1.19.4",
4
4
  "main": "index.js",
5
5
  "repository": "https://github.com/kevily/create-packer",
6
6
  "author": "1k <bug_zero@163.com>",
@@ -1,32 +1,38 @@
1
- import { create, StoreApi } from 'zustand'
1
+ import { create, StoreApi as ZStoreApi } from 'zustand'
2
2
  import { combine } from 'zustand/middleware'
3
3
  import { produce } from 'immer'
4
4
  import { forEach, size } from 'lodash-es'
5
5
 
6
+ export type StoreApi<S> = Omit<ZStoreApi<S>, 'setState'> & {
7
+ setState: (updater: ((state: S) => void) | Partial<S>, replace?: boolean) => void
8
+ }
6
9
  export interface insideActionsType<S> {
7
10
  reset: () => void
8
- setState: (updater: (state: S) => void) => void
11
+ setState: StoreApi<S>['setState']
9
12
  }
10
13
  export type actionsType<S, OptionAction, InsideActions = unknown> = (
11
14
  getState: () => S,
12
15
  actions: insideActionsType<S> & InsideActions,
13
16
  store: StoreApi<S>
14
17
  ) => OptionAction
18
+
19
+ export type defGetterStateType<S> = Record<string, (state: S) => any>
15
20
  export interface optionsType<S, G, OptionAction> {
16
21
  state: () => S
17
22
  getter: () => G
18
23
  actions: actionsType<S, OptionAction>
19
24
  }
20
25
 
21
- type GenGetterStateType<S, G extends Record<string, (state: S) => any>> = G extends Record<
26
+ type GenGetterStateType<S, G extends defGetterStateType<S>> = G extends Record<
22
27
  infer K,
23
28
  (state: S) => infer V
24
29
  >
25
30
  ? Record<K, V>
26
31
  : unknown
32
+
27
33
  export function define<
28
34
  S extends Record<string, any>,
29
- G extends Record<string, (state: S) => any>,
35
+ G extends defGetterStateType<S>,
30
36
  OptionActions extends Record<string, any>
31
37
  >(options: optionsType<S, G, OptionActions>) {
32
38
  function createDefState() {
@@ -58,21 +64,23 @@ export function define<
58
64
  })
59
65
  // actions
60
66
  // ----------------------------------------------------------------------
61
- const setState: insideActionsType<S>['setState'] = updater => {
62
- set(
63
- produce((store: S) => {
64
- updater(store)
65
- }) as any
66
- )
67
+ store.setState = (updater, replace) => {
68
+ const nextState = typeof updater === 'function' ? produce(updater as any) : updater
69
+
70
+ return set(nextState as any, replace)
67
71
  }
68
72
  const reset: insideActionsType<S>['reset'] = () => {
69
73
  set(() => options.state() as never)
70
74
  }
71
75
  return {
72
76
  reset,
73
- setState,
77
+ setState: store.setState as StoreApi<S>['setState'],
74
78
  subscribe: store.subscribe,
75
- ...options.actions(get, { reset, setState }, store)
79
+ ...options.actions(
80
+ get,
81
+ { reset, setState: store.setState as StoreApi<S>['setState'] },
82
+ store as StoreApi<S>
83
+ )
76
84
  }
77
85
  })
78
86
  )
@@ -1,7 +1,9 @@
1
1
  import { modelUtils } from '@/providers'
2
2
 
3
3
  export const useApp = modelUtils.define({
4
- state: () => ({}),
4
+ state: () => ({
5
+ a: ''
6
+ }),
5
7
  getter: () => ({}),
6
8
  actions: () => ({})
7
9
  })
@@ -1,32 +1,38 @@
1
- import { create, StoreApi } from 'zustand'
1
+ import { create, StoreApi as ZStoreApi } from 'zustand'
2
2
  import { combine } from 'zustand/middleware'
3
3
  import { produce } from 'immer'
4
4
  import { forEach, size } from 'lodash-es'
5
5
 
6
+ export type StoreApi<S> = Omit<ZStoreApi<S>, 'setState'> & {
7
+ setState: (updater: ((state: S) => void) | Partial<S>, replace?: boolean) => void
8
+ }
6
9
  export interface insideActionsType<S> {
7
10
  reset: () => void
8
- setState: (updater: (state: S) => void) => void
11
+ setState: StoreApi<S>['setState']
9
12
  }
10
13
  export type actionsType<S, OptionAction, InsideActions = unknown> = (
11
14
  getState: () => S,
12
15
  actions: insideActionsType<S> & InsideActions,
13
16
  store: StoreApi<S>
14
17
  ) => OptionAction
18
+
19
+ export type defGetterStateType<S> = Record<string, (state: S) => any>
15
20
  export interface optionsType<S, G, OptionAction> {
16
21
  state: () => S
17
22
  getter: () => G
18
23
  actions: actionsType<S, OptionAction>
19
24
  }
20
25
 
21
- type GenGetterStateType<S, G extends Record<string, (state: S) => any>> = G extends Record<
26
+ type GenGetterStateType<S, G extends defGetterStateType<S>> = G extends Record<
22
27
  infer K,
23
28
  (state: S) => infer V
24
29
  >
25
30
  ? Record<K, V>
26
31
  : unknown
32
+
27
33
  export function define<
28
34
  S extends Record<string, any>,
29
- G extends Record<string, (state: S) => any>,
35
+ G extends defGetterStateType<S>,
30
36
  OptionActions extends Record<string, any>
31
37
  >(options: optionsType<S, G, OptionActions>) {
32
38
  function createDefState() {
@@ -58,21 +64,23 @@ export function define<
58
64
  })
59
65
  // actions
60
66
  // ----------------------------------------------------------------------
61
- const setState: insideActionsType<S>['setState'] = updater => {
62
- set(
63
- produce((store: S) => {
64
- updater(store)
65
- }) as any
66
- )
67
+ store.setState = (updater, replace) => {
68
+ const nextState = typeof updater === 'function' ? produce(updater as any) : updater
69
+
70
+ return set(nextState as any, replace)
67
71
  }
68
72
  const reset: insideActionsType<S>['reset'] = () => {
69
73
  set(() => options.state() as never)
70
74
  }
71
75
  return {
72
76
  reset,
73
- setState,
77
+ setState: store.setState as StoreApi<S>['setState'],
74
78
  subscribe: store.subscribe,
75
- ...options.actions(get, { reset, setState }, store)
79
+ ...options.actions(
80
+ get,
81
+ { reset, setState: store.setState as StoreApi<S>['setState'] },
82
+ store as StoreApi<S>
83
+ )
76
84
  }
77
85
  })
78
86
  )