Skip to content

Commit a7338bf

Browse files
committed
update
1 parent 7861432 commit a7338bf

File tree

14 files changed

+266
-15
lines changed

14 files changed

+266
-15
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ internal enhancers:
4949

5050
other enhancers:
5151

52-
- requestCache()
53-
- rx(...operators)
52+
- [immer()](https://frontlich.github.io/nice-store/#/zh-cn/internal-enhancer?id=immer)
53+
- [requestCache()](https://frontlich.github.io/nice-store/#/zh-cn/internal-enhancer?id=requestCache)
54+
- [rx(...operators)](https://frontlich.github.io/nice-store/#/zh-cn/internal-enhancer?id=rx)

build-enhancers.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function build() {
2525
write: true,
2626
bundle: true,
2727
treeShaking: true,
28-
external: ['rxjs'],
28+
external: ['rxjs', 'immer'],
2929
define: {
3030
'process.env.NODE_ENV': 'process.env.NODE_ENV',
3131
},

docs/zh-cn/custom-enhancer.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
11
# 自定义增强器
2+
3+
如果内置的一些增强器不满足需求,我们还可以自定义增强器
4+
5+
自定义增强器既可以是定义新的增强器,也可以是组合现有的增强器
6+
7+
## 定义新的增强器
8+
9+
```ts
10+
export type EnhancedStore<State, Ext> = Store<State> & Ext;
11+
12+
export type CreateStore<S, Ext> = (initialState?: S) => EnhancedStore<S, Ext>;
13+
14+
export type NextEnhancer<S, Ext1, Ext2 = Ext1> = (
15+
createStore: CreateStore<S, Ext1>
16+
) => CreateStore<S, Ext1 & Ext2>;
17+
```
18+
19+
一个 Enhancer 是一个形如`(createStore) => (initialState) => store`的函数
20+
21+
举例:
22+
23+
```js
24+
export const logger = (createStore) => (initialState) => {
25+
const store = createStore();
26+
27+
const setState = (update) => {
28+
if (typeof update === "function") {
29+
store.setState((state) => {
30+
const newState = update(state);
31+
console.log("log from logger enhancer:", newState);
32+
return newState;
33+
});
34+
} else {
35+
console.log("log from logger enhancer:", update);
36+
store.setState(update);
37+
}
38+
};
39+
40+
return {
41+
...store,
42+
setState,
43+
};
44+
};
45+
```
46+
47+
## 组合现有的增强器
48+
49+
```js
50+
import { pipe, reset, thunk } from "nice-store";
51+
export const myEnhancer = pipe(reset(), thunk());
52+
```

docs/zh-cn/internal-enhancer.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,74 @@ const Counter = () => {
261261
);
262262
};
263263
```
264+
265+
# 可选的增强器
266+
267+
## immer
268+
269+
`immer()`
270+
271+
immer 增强器可以更方便地更新 state,它会给 store 增加一个 produce 方法,可以通过直接修改值来更新数据
272+
273+
用法
274+
275+
```js
276+
import { createStore } from "nice-store";
277+
import { immer } from "nice-store/enhancers";
278+
279+
const store = createStore({ a: 1, b: 2 }, immer());
280+
281+
// 订阅state更新事件
282+
store.subscribe(console.log);
283+
284+
store.produce((s) => {
285+
s.a = 3;
286+
});
287+
```
288+
289+
## requestCache
290+
291+
`requestCache()`
292+
293+
requestCache 增强器会给 store 添加一个 withCache 方法,调用 withCache 会返回一个 options 对象,在 ahooks 的 useRequest 配置项中使用该对象,可以将请求的数据缓存到 store 中
294+
295+
用法
296+
297+
```js
298+
import { createStore } from "nice-store";
299+
import { requestCache } from "nice-store/enhancers";
300+
import { useRequest } from "ahooks";
301+
302+
const store = createStore(0, requestCache());
303+
304+
// 订阅state更新事件
305+
store.subscribe(console.log);
306+
307+
useRequest(() => Promise.resolve(1), store.withCache());
308+
```
309+
310+
## rx
311+
312+
`rx(...operators)`
313+
314+
rx 增强器会给 store 添加 next 方法,调用 next 方法会走所有的 rxjs 的操作符,最终更新 store 的状态
315+
316+
```js
317+
import { createStore } from "nice-store";
318+
import { rx } from "nice-store/enhancers";
319+
import { map, scan } from "rxjs/operators";
320+
321+
const store = createStore(
322+
0,
323+
rx(
324+
map((v: string) => Number(v)),
325+
scan((acc, v) => acc + v)
326+
)
327+
);
328+
329+
// 订阅state更新事件
330+
store.subscribe(console.log);
331+
332+
store.next("1"); // log 1
333+
store.next("2"); // log 3
334+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nice-store",
33
"description": "lite & flexible & extensible state management for react",
4-
"version": "1.0.2",
4+
"version": "1.1.1",
55
"author": "frontlich",
66
"keywords": [
77
"state management",

src/createStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export function createStore<
123123
enhancer7: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6, Ext7>,
124124
enhancer8: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7, Ext8>,
125125
enhancer9: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7 & Ext8, Ext9>,
126+
...enhancers: NextEnhancer<State, any>[]
126127
): EnhancedStore<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7 & Ext8 & Ext9>;
127128

128129
export function createStore<State>(initialState?: State, ...enhancers: Enhancer<State, any>[]) {

src/enhancers/immer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ export function immer<State, PreExt>(): NextEnhancer<State, PreExt, Ext<State>>
1717

1818
return {
1919
...store,
20-
produce: (recipe) => {
21-
store.setState((state) => immerProduce(state, (draft) => void recipe(draft)));
22-
},
20+
produce: (recipe) => store.setState(immerProduce(recipe)),
2321
};
2422
};
2523
}

src/enhancers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { requestCache } from './requestCache';
22
export { rx } from './rx';
3+
export { immer } from './immer';

src/enhancers/rx.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ export function rx<State, PreExt, T1, T2, T3, T4, T5>(
4040
operator5: OperatorFunction<T5, State>,
4141
): NextEnhancer<State, PreExt, Ext<T1>>;
4242

43+
export function rx<State, PreExt, T1, T2, T3, T4, T5, T6>(
44+
operator1: OperatorFunction<T1, T2>,
45+
operator2: OperatorFunction<T2, T3>,
46+
operator3: OperatorFunction<T3, T4>,
47+
operator4: OperatorFunction<T4, T5>,
48+
operator5: OperatorFunction<T5, T6>,
49+
operator6: OperatorFunction<T6, State>,
50+
): NextEnhancer<State, PreExt, Ext<T1>>;
51+
52+
export function rx<State, PreExt, T1, T2, T3, T4, T5, T6, T7>(
53+
operator1: OperatorFunction<T1, T2>,
54+
operator2: OperatorFunction<T2, T3>,
55+
operator3: OperatorFunction<T3, T4>,
56+
operator4: OperatorFunction<T4, T5>,
57+
operator5: OperatorFunction<T5, T6>,
58+
operator6: OperatorFunction<T6, T7>,
59+
operator7: OperatorFunction<T7, State>,
60+
): NextEnhancer<State, PreExt, Ext<T1>>;
61+
4362
export function rx<State, PreExt, Next>(
4463
...operators: any[]
4564
): NextEnhancer<State, PreExt, Ext<Next>> {

src/react.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ export function create<
104104
enhancer8: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7, Ext8>,
105105
): Result<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7 & Ext8>;
106106

107+
export function create<
108+
State,
109+
Ext1 = {},
110+
Ext2 = {},
111+
Ext3 = {},
112+
Ext4 = {},
113+
Ext5 = {},
114+
Ext6 = {},
115+
Ext7 = {},
116+
Ext8 = {},
117+
Ext9 = {},
118+
>(
119+
initialState: State,
120+
enhancer1: Enhancer<State, Ext1>,
121+
enhancer2: NextEnhancer<State, Ext1, Ext2>,
122+
enhancer3: NextEnhancer<State, Ext1 & Ext2, Ext3>,
123+
enhancer4: NextEnhancer<State, Ext1 & Ext2 & Ext3, Ext4>,
124+
enhancer5: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4, Ext5>,
125+
enhancer6: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5, Ext6>,
126+
enhancer7: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6, Ext7>,
127+
enhancer8: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7, Ext8>,
128+
enhancer9: NextEnhancer<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7 & Ext8, Ext9>,
129+
...enhancers: NextEnhancer<State, any>[]
130+
): Result<State, Ext1 & Ext2 & Ext3 & Ext4 & Ext5 & Ext6 & Ext7 & Ext8>;
131+
107132
export function create<State>(initialState?: State, ...enhancers: Enhancer<State, any>[]) {
108133
const store = (createStore as any)(initialState, ...enhancers, selectorHook());
109134

0 commit comments

Comments
 (0)