extra-react-store 0.1.1 → 0.1.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/README.md CHANGED
@@ -51,6 +51,8 @@ function increment(state: IState): void {
51
51
 
52
52
  ## API
53
53
  ```ts
54
+ import { Draft } from 'immer'
55
+
54
56
  type StoreContext<State> = React.Context<IStore<State>>
55
57
 
56
58
  interface IStore<State> {
@@ -58,6 +60,11 @@ interface IStore<State> {
58
60
  setState(newState: State): void
59
61
  subscribe(fn: (state: State) => void): () => void
60
62
  }
63
+
64
+ type Updater<State> = (...args:
65
+ | [newState: State]
66
+ | [fn: (draft: Draft<State>) => void]
67
+ ) => void
61
68
  ```
62
69
 
63
70
  ### Store
@@ -82,7 +89,14 @@ function useSelector<State, Value>(
82
89
 
83
90
  ### useUpdater
84
91
  ```ts
85
- import { Updater } from 'use-immer'
86
-
87
92
  function useUpdater<State>(context: StoreContext<State>): Updater<State>
88
93
  ```
94
+
95
+ ### usePartialUpdater
96
+ ```ts
97
+ function usePartialUpdater<State, PartialState>(
98
+ context: StoreContext<State>
99
+ , extractPartialState: (state: State) => PartialState
100
+ , mergePartialState: (state: State, partialState: PartialState) => State
101
+ ): Updater<PartialState>
102
+ ```
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"}
package/lib/types.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  /// <reference types="react" />
2
+ import { Draft } from 'immer';
2
3
  export type StoreContext<State> = React.Context<IStore<State>>;
3
4
  export interface IStore<State> {
4
5
  getState(): State;
5
6
  setState(newState: State): void;
6
7
  subscribe(fn: (state: State) => void): () => void;
7
8
  }
9
+ export type Updater<State> = (...args: [newState: State] | [fn: (draft: Draft<State>) => void]) => void;
@@ -0,0 +1,2 @@
1
+ import { StoreContext, Updater } from './types.js';
2
+ 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;AAC/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"}
@@ -1,3 +1,2 @@
1
- import { Updater } from 'use-immer';
2
- import { StoreContext } from './types.js';
1
+ import { StoreContext, Updater } from './types.js';
3
2
  export declare function useUpdater<State>(context: StoreContext<State>): Updater<State>;
@@ -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":"AACA,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.1",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
@@ -62,8 +62,7 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "@blackglory/prelude": "^0.3.3",
65
- "@blackglory/structures": "^0.13.4",
66
- "use-immer": "^0.9.0"
65
+ "@blackglory/structures": "^0.13.4"
67
66
  },
68
67
  "peerDependencies": {
69
68
  "immer": "^10.0.2",
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'
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Draft } from 'immer'
2
+
1
3
  export type StoreContext<State> = React.Context<IStore<State>>
2
4
 
3
5
  export interface IStore<State> {
@@ -5,3 +7,8 @@ export interface IStore<State> {
5
7
  setState(newState: State): void
6
8
  subscribe(fn: (state: State) => void): () => void
7
9
  }
10
+
11
+ export type Updater<State> = (...args:
12
+ | [newState: State]
13
+ | [fn: (draft: Draft<State>) => void]
14
+ ) => void
@@ -0,0 +1,30 @@
1
+ import { useCallback, useContext } from 'react'
2
+ import { produce } from 'immer'
3
+ import { go, isFunction } from '@blackglory/prelude'
4
+ import { StoreContext, Updater } from './types.js'
5
+
6
+ export function usePartialUpdater<State, PartialState>(
7
+ context: StoreContext<State>
8
+ , extractPartialState: (state: State) => PartialState
9
+ , mergePartialState: (state: State, partialState: PartialState) => State
10
+ ): Updater<PartialState> {
11
+ const store = useContext(context)
12
+
13
+ const updater: Updater<PartialState> = useCallback(newStateOrFn => {
14
+ const oldState = store.getState()
15
+
16
+ const newPartialState: PartialState = go(() => {
17
+ if (isFunction(newStateOrFn)) {
18
+ const fn = newStateOrFn
19
+ return produce(extractPartialState(oldState), fn)
20
+ } else {
21
+ const newState = newStateOrFn
22
+ return newState
23
+ }
24
+ })
25
+
26
+ store.setState(mergePartialState(oldState, newPartialState))
27
+ }, [store, extractPartialState, mergePartialState])
28
+
29
+ return updater
30
+ }
@@ -1,25 +1,10 @@
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'
1
+ import { StoreContext, Updater } from './types.js'
2
+ import { usePartialUpdater } from './use-partial-updater.js'
6
3
 
7
4
  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
5
+ return usePartialUpdater(
6
+ context
7
+ , state => state
8
+ , (_, value) => value
9
+ )
25
10
  }