extra-react-store 0.1.2 → 0.1.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/README.md +7 -4
- package/lib/types.d.ts +2 -0
- package/lib/use-partial-updater.d.ts +1 -2
- package/lib/use-partial-updater.js +7 -6
- package/lib/use-partial-updater.js.map +1 -1
- package/lib/use-updater.d.ts +1 -2
- package/lib/use-updater.js.map +1 -1
- package/package.json +2 -3
- package/src/types.ts +7 -0
- package/src/use-partial-updater.ts +13 -9
- package/src/use-updater.ts +1 -2
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 | State]
|
|
67
|
+
) => void
|
|
61
68
|
```
|
|
62
69
|
|
|
63
70
|
### Store
|
|
@@ -82,15 +89,11 @@ 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
|
```
|
|
89
94
|
|
|
90
95
|
### usePartialUpdater
|
|
91
96
|
```ts
|
|
92
|
-
import { Updater } from 'use-immer'
|
|
93
|
-
|
|
94
97
|
function usePartialUpdater<State, PartialState>(
|
|
95
98
|
context: StoreContext<State>
|
|
96
99
|
, extractPartialState: (state: State) => PartialState
|
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 | State]) => void;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import { Updater } from '
|
|
2
|
-
import { StoreContext } from './types.js';
|
|
1
|
+
import { StoreContext, Updater } from './types.js';
|
|
3
2
|
export declare function usePartialUpdater<State, PartialState>(context: StoreContext<State>, extractPartialState: (state: State) => PartialState, mergePartialState: (state: State, partialState: PartialState) => State): Updater<PartialState>;
|
|
@@ -3,16 +3,17 @@ import { produce } from 'immer';
|
|
|
3
3
|
import { go, isFunction } from '@blackglory/prelude';
|
|
4
4
|
export function usePartialUpdater(context, extractPartialState, mergePartialState) {
|
|
5
5
|
const store = useContext(context);
|
|
6
|
-
const updater = useCallback(
|
|
6
|
+
const updater = useCallback(newPartialStateOrFn => {
|
|
7
7
|
const oldState = store.getState();
|
|
8
8
|
const newPartialState = go(() => {
|
|
9
|
-
if (isFunction(
|
|
10
|
-
const fn =
|
|
11
|
-
|
|
9
|
+
if (isFunction(newPartialStateOrFn)) {
|
|
10
|
+
const fn = newPartialStateOrFn;
|
|
11
|
+
const newPartialState = produce(extractPartialState(oldState), fn);
|
|
12
|
+
return newPartialState;
|
|
12
13
|
}
|
|
13
14
|
else {
|
|
14
|
-
const
|
|
15
|
-
return
|
|
15
|
+
const newPartialState = newPartialStateOrFn;
|
|
16
|
+
return newPartialState;
|
|
16
17
|
}
|
|
17
18
|
});
|
|
18
19
|
store.setState(mergePartialState(oldState, newPartialState));
|
|
@@ -1 +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;
|
|
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,EAAS,OAAO,EAAE,MAAM,OAAO,CAAA;AACtC,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,mBAAmB,CAAC,EAAE;QACvE,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAA;QAEjC,MAAM,eAAe,GAAiB,EAAE,CAAC,GAAG,EAAE;YAC5C,IAAI,UAAU,CAAC,mBAAmB,CAAC,EAAE;gBACnC,MAAM,EAAE,GAAG,mBAAmB,CAAA;gBAC9B,MAAM,eAAe,GAAG,OAAO,CAC7B,mBAAmB,CAAC,QAAQ,CAAC,EAE7B,EAAE,CACH,CAAA;gBACD,OAAO,eAAe,CAAA;aACvB;iBAAM;gBACL,MAAM,eAAe,GAAG,mBAAmB,CAAA;gBAC3C,OAAO,eAAe,CAAA;aACvB;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"}
|
package/lib/use-updater.d.ts
CHANGED
package/lib/use-updater.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-updater.js","sourceRoot":"","sources":["../src/use-updater.ts"],"names":[],"mappings":"
|
|
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.
|
|
3
|
+
"version": "0.1.4",
|
|
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/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 | State]
|
|
14
|
+
) => void
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { useCallback, useContext } from 'react'
|
|
2
|
-
import {
|
|
3
|
-
import { produce } from 'immer'
|
|
2
|
+
import { Draft, produce } from 'immer'
|
|
4
3
|
import { go, isFunction } from '@blackglory/prelude'
|
|
5
|
-
import { StoreContext } from './types.js'
|
|
4
|
+
import { StoreContext, Updater } from './types.js'
|
|
6
5
|
|
|
7
6
|
export function usePartialUpdater<State, PartialState>(
|
|
8
7
|
context: StoreContext<State>
|
|
@@ -11,16 +10,21 @@ export function usePartialUpdater<State, PartialState>(
|
|
|
11
10
|
): Updater<PartialState> {
|
|
12
11
|
const store = useContext(context)
|
|
13
12
|
|
|
14
|
-
const updater: Updater<PartialState> = useCallback(
|
|
13
|
+
const updater: Updater<PartialState> = useCallback(newPartialStateOrFn => {
|
|
15
14
|
const oldState = store.getState()
|
|
16
15
|
|
|
17
16
|
const newPartialState: PartialState = go(() => {
|
|
18
|
-
if (isFunction(
|
|
19
|
-
const fn =
|
|
20
|
-
|
|
17
|
+
if (isFunction(newPartialStateOrFn)) {
|
|
18
|
+
const fn = newPartialStateOrFn
|
|
19
|
+
const newPartialState = produce<PartialState, Draft<PartialState>>(
|
|
20
|
+
extractPartialState(oldState)
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
, fn
|
|
23
|
+
)
|
|
24
|
+
return newPartialState
|
|
21
25
|
} else {
|
|
22
|
-
const
|
|
23
|
-
return
|
|
26
|
+
const newPartialState = newPartialStateOrFn
|
|
27
|
+
return newPartialState
|
|
24
28
|
}
|
|
25
29
|
})
|
|
26
30
|
|
package/src/use-updater.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Updater } from '
|
|
2
|
-
import { StoreContext } from './types.js'
|
|
1
|
+
import { StoreContext, Updater } from './types.js'
|
|
3
2
|
import { usePartialUpdater } from './use-partial-updater.js'
|
|
4
3
|
|
|
5
4
|
export function useUpdater<State>(context: StoreContext<State>): Updater<State> {
|