extra-react-store 0.1.0 → 0.1.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/README.md CHANGED
@@ -15,21 +15,21 @@ interface IState {
15
15
  count: number
16
16
  }
17
17
 
18
- const context = createStoreContext<IState>()
18
+ const Context = createStoreContext<IState>()
19
19
 
20
20
  function App() {
21
21
  const store = useMemo<Store<IState>>(() => new Store({ count: 0 }), [])
22
22
 
23
23
  return (
24
- <context.Provider value={store}>
24
+ <Context.Provider value={store}>
25
25
  <Viewer />
26
26
  <Controller />
27
- </context.Provider>
27
+ </Context.Provider>
28
28
  )
29
29
  }
30
30
 
31
31
  function Viewer() {
32
- const count = useSelector(context, state => state.count)
32
+ const count = useSelector(Context, state => state.count)
33
33
 
34
34
  return (
35
35
  <span>{count}</span>
@@ -37,7 +37,7 @@ function Viewer() {
37
37
  }
38
38
 
39
39
  function Controller() {
40
- const update = useUpdater<IState>(context)
40
+ const update = useUpdater<IState>(Context)
41
41
 
42
42
  return (
43
43
  <button onClick={() => update(increment)}>Increment</button>
@@ -86,3 +86,14 @@ import { Updater } from 'use-immer'
86
86
 
87
87
  function useUpdater<State>(context: StoreContext<State>): Updater<State>
88
88
  ```
89
+
90
+ ### usePartialUpdater
91
+ ```ts
92
+ import { Updater } from 'use-immer'
93
+
94
+ function usePartialUpdater<State, PartialState>(
95
+ context: StoreContext<State>
96
+ , extractPartialState: (state: State) => PartialState
97
+ , mergePartialState: (state: State, partialState: PartialState) => State
98
+ ): Updater<PartialState>
99
+ ```
package/lib/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './store.js';
3
3
  export * from './create-store-context.js';
4
4
  export * from './use-selector.js';
5
5
  export * from './use-updater.js';
6
+ export * from './use-partial-updater.js';
package/lib/index.js CHANGED
@@ -3,4 +3,5 @@ export * from './store.js';
3
3
  export * from './create-store-context.js';
4
4
  export * from './use-selector.js';
5
5
  export * from './use-updater.js';
6
+ export * from './use-partial-updater.js';
6
7
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,2BAA2B,CAAA;AACzC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,2BAA2B,CAAA;AACzC,cAAc,mBAAmB,CAAA;AACjC,cAAc,kBAAkB,CAAA;AAChC,cAAc,0BAA0B,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { Updater } from 'use-immer';
2
+ import { StoreContext } from './types.js';
3
+ export declare function usePartialUpdater<State, PartialState>(context: StoreContext<State>, extractPartialState: (state: State) => PartialState, mergePartialState: (state: State, partialState: PartialState) => State): Updater<PartialState>;
@@ -0,0 +1,22 @@
1
+ import { useCallback, useContext } from 'react';
2
+ import { produce } from 'immer';
3
+ import { go, isFunction } from '@blackglory/prelude';
4
+ export function usePartialUpdater(context, extractPartialState, mergePartialState) {
5
+ const store = useContext(context);
6
+ const updater = useCallback(newStateOrFn => {
7
+ const oldState = store.getState();
8
+ const newPartialState = go(() => {
9
+ if (isFunction(newStateOrFn)) {
10
+ const fn = newStateOrFn;
11
+ return produce(extractPartialState(oldState), fn);
12
+ }
13
+ else {
14
+ const newState = newStateOrFn;
15
+ return newState;
16
+ }
17
+ });
18
+ store.setState(mergePartialState(oldState, newPartialState));
19
+ }, [store, extractPartialState, mergePartialState]);
20
+ return updater;
21
+ }
22
+ //# sourceMappingURL=use-partial-updater.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-partial-updater.js","sourceRoot":"","sources":["../src/use-partial-updater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAE/C,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAGpD,MAAM,UAAU,iBAAiB,CAC/B,OAA4B,EAC5B,mBAAmD,EACnD,iBAAsE;IAEtE,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IAEjC,MAAM,OAAO,GAA0B,WAAW,CAAC,YAAY,CAAC,EAAE;QAChE,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAA;QAEjC,MAAM,eAAe,GAAiB,EAAE,CAAC,GAAG,EAAE;YAC5C,IAAI,UAAU,CAAC,YAAY,CAAC,EAAE;gBAC5B,MAAM,EAAE,GAAG,YAAY,CAAA;gBACvB,OAAO,OAAO,CAAC,mBAAmB,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,CAAA;aAClD;iBAAM;gBACL,MAAM,QAAQ,GAAG,YAAY,CAAA;gBAC7B,OAAO,QAAQ,CAAA;aAChB;QACH,CAAC,CAAC,CAAA;QAEF,KAAK,CAAC,QAAQ,CAAC,iBAAiB,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAA;IAC9D,CAAC,EAAE,CAAC,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,CAAC,CAAC,CAAA;IAEnD,OAAO,OAAO,CAAA;AAChB,CAAC"}
@@ -7,7 +7,7 @@ export function useSelector(context, selector) {
7
7
  const newValue = selector(state);
8
8
  setValue(newValue);
9
9
  });
10
- }, [store, value]);
10
+ }, [store, selector, value]);
11
11
  return value;
12
12
  }
13
13
  //# sourceMappingURL=use-selector.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAG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AAA;AACd,CAAC"}
@@ -1,22 +1,5 @@
1
- import { useCallback, useContext } from 'react';
2
- import { produce } from 'immer';
3
- import { go, isFunction } from '@blackglory/prelude';
1
+ import { usePartialUpdater } from './use-partial-updater.js';
4
2
  export function useUpdater(context) {
5
- const store = useContext(context);
6
- const updater = useCallback(newStateOrFn => {
7
- const state = go(() => {
8
- if (isFunction(newStateOrFn)) {
9
- const fn = newStateOrFn;
10
- const newState = produce(store.getState(), fn);
11
- return newState;
12
- }
13
- else {
14
- const newState = newStateOrFn;
15
- return newState;
16
- }
17
- });
18
- store.setState(state);
19
- }, [store]);
20
- return updater;
3
+ return usePartialUpdater(context, state => state, (_, value) => value);
21
4
  }
22
5
  //# sourceMappingURL=use-updater.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-updater.js","sourceRoot":"","sources":["../src/use-updater.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAE/C,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAGpD,MAAM,UAAU,UAAU,CAAQ,OAA4B;IAC5D,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IAEjC,MAAM,OAAO,GAAmB,WAAW,CAAC,YAAY,CAAC,EAAE;QACzD,MAAM,KAAK,GAAG,EAAE,CAAC,GAAG,EAAE;YACpB,IAAI,UAAU,CAAC,YAAY,CAAC,EAAE;gBAC5B,MAAM,EAAE,GAAG,YAAY,CAAA;gBACvB,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAA;gBAC9C,OAAO,QAAQ,CAAA;aAChB;iBAAM;gBACL,MAAM,QAAQ,GAAG,YAAY,CAAA;gBAC7B,OAAO,QAAQ,CAAA;aAChB;QACH,CAAC,CAAC,CAAA;QACF,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;IACvB,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAA;IAEX,OAAO,OAAO,CAAA;AAChB,CAAC"}
1
+ {"version":3,"file":"use-updater.js","sourceRoot":"","sources":["../src/use-updater.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAE5D,MAAM,UAAU,UAAU,CAAQ,OAA4B;IAC5D,OAAO,iBAAiB,CACtB,OAAO,EACP,KAAK,CAAC,EAAE,CAAC,KAAK,EACd,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CACpB,CAAA;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-react-store",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
package/src/index.ts CHANGED
@@ -3,3 +3,4 @@ export * from './store.js'
3
3
  export * from './create-store-context.js'
4
4
  export * from './use-selector.js'
5
5
  export * from './use-updater.js'
6
+ export * from './use-partial-updater.js'
@@ -0,0 +1,31 @@
1
+ import { useCallback, useContext } from 'react'
2
+ import { Updater } from 'use-immer'
3
+ import { produce } from 'immer'
4
+ import { go, isFunction } from '@blackglory/prelude'
5
+ import { StoreContext } from './types.js'
6
+
7
+ export function usePartialUpdater<State, PartialState>(
8
+ context: StoreContext<State>
9
+ , extractPartialState: (state: State) => PartialState
10
+ , mergePartialState: (state: State, partialState: PartialState) => State
11
+ ): Updater<PartialState> {
12
+ const store = useContext(context)
13
+
14
+ const updater: Updater<PartialState> = useCallback(newStateOrFn => {
15
+ const oldState = store.getState()
16
+
17
+ const newPartialState: PartialState = go(() => {
18
+ if (isFunction(newStateOrFn)) {
19
+ const fn = newStateOrFn
20
+ return produce(extractPartialState(oldState), fn)
21
+ } else {
22
+ const newState = newStateOrFn
23
+ return newState
24
+ }
25
+ })
26
+
27
+ store.setState(mergePartialState(oldState, newPartialState))
28
+ }, [store, extractPartialState, mergePartialState])
29
+
30
+ return updater
31
+ }
@@ -15,7 +15,7 @@ export function useSelector<State, Value>(
15
15
  // 若新旧状态引用相等, 不会发生重新渲染.
16
16
  setValue(newValue)
17
17
  })
18
- }, [store, value])
18
+ }, [store, selector, value])
19
19
 
20
20
  return value
21
21
  }
@@ -1,25 +1,11 @@
1
- import { useCallback, useContext } from 'react'
2
1
  import { Updater } from 'use-immer'
3
- import { produce } from 'immer'
4
- import { go, isFunction } from '@blackglory/prelude'
5
2
  import { StoreContext } from './types.js'
3
+ import { usePartialUpdater } from './use-partial-updater.js'
6
4
 
7
5
  export function useUpdater<State>(context: StoreContext<State>): Updater<State> {
8
- const store = useContext(context)
9
-
10
- const updater: Updater<State> = useCallback(newStateOrFn => {
11
- const state = go(() => {
12
- if (isFunction(newStateOrFn)) {
13
- const fn = newStateOrFn
14
- const newState = produce(store.getState(), fn)
15
- return newState
16
- } else {
17
- const newState = newStateOrFn
18
- return newState
19
- }
20
- })
21
- store.setState(state)
22
- }, [store])
23
-
24
- return updater
6
+ return usePartialUpdater(
7
+ context
8
+ , state => state
9
+ , (_, value) => value
10
+ )
25
11
  }