extra-react-store 0.1.3 → 0.1.5

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
@@ -63,7 +63,7 @@ interface IStore<State> {
63
63
 
64
64
  type Updater<State> = (...args:
65
65
  | [newState: State]
66
- | [fn: (draft: Draft<State>) => void]
66
+ | [fn: (draft: Draft<State>) => void | State]
67
67
  ) => void
68
68
  ```
69
69
 
@@ -84,6 +84,7 @@ function createStoreContext<State>(): StoreContext<State>
84
84
  function useSelector<State, Value>(
85
85
  context: StoreContext<State>
86
86
  , selector: (state: State) => Value
87
+ , isEqual: (a: Value, b: Value) => boolean = (a, b) => a === b
87
88
  ): Value
88
89
  ```
89
90
 
package/lib/types.d.ts CHANGED
@@ -6,4 +6,4 @@ export interface IStore<State> {
6
6
  setState(newState: State): void;
7
7
  subscribe(fn: (state: State) => void): () => void;
8
8
  }
9
- export type Updater<State> = (...args: [newState: State] | [fn: (draft: Draft<State>) => void]) => void;
9
+ export type Updater<State> = (...args: [newState: State] | [fn: (draft: Draft<State>) => void | State]) => void;
@@ -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(newStateOrFn => {
6
+ const updater = useCallback(newPartialStateOrFn => {
7
7
  const oldState = store.getState();
8
8
  const newPartialState = go(() => {
9
- if (isFunction(newStateOrFn)) {
10
- const fn = newStateOrFn;
11
- return produce(extractPartialState(oldState), fn);
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 newState = newStateOrFn;
15
- return newState;
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;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
+ {"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"}
@@ -1,2 +1,2 @@
1
1
  import { StoreContext } from './types.js';
2
- export declare function useSelector<State, Value>(context: StoreContext<State>, selector: (state: State) => Value): Value;
2
+ export declare function useSelector<State, Value>(context: StoreContext<State>, selector: (state: State) => Value, isEqual?: (a: Value, b: Value) => boolean): Value;
@@ -1,13 +1,15 @@
1
1
  import { useState, useContext, useEffect } from 'react';
2
- export function useSelector(context, selector) {
2
+ export function useSelector(context, selector, isEqual = (a, b) => a === b) {
3
3
  const store = useContext(context);
4
- const [value, setValue] = useState(() => selector(store.getState()));
4
+ const [[value], setBox] = useState(() => [selector(store.getState())]);
5
5
  useEffect(() => {
6
6
  return store.subscribe(state => {
7
7
  const newValue = selector(state);
8
- setValue(newValue);
8
+ if (!isEqual(value, newValue)) {
9
+ setBox([newValue]);
10
+ }
9
11
  });
10
- }, [store, selector, value]);
12
+ }, [store, selector, value, setBox]);
11
13
  return value;
12
14
  }
13
15
  //# 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;AAGvD,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC;IAEjC,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IACjC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;IAEpE,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;YAC7B,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;YAGhC,QAAQ,CAAC,QAAQ,CAAC,CAAA;QACpB,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAA;IAE5B,OAAO,KAAK,CAAA;AACd,CAAC"}
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;AAGvD,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC,EACjC,UAA2C,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC;IAE5D,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IACjC,MAAM,CAAC,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAA;IAEtE,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;YAC7B,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;YAEhC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE;gBAC7B,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;aACnB;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;IAEpC,OAAO,KAAK,CAAA;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-react-store",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
@@ -33,15 +33,15 @@
33
33
  }
34
34
  },
35
35
  "devDependencies": {
36
- "@commitlint/cli": "^17.6.5",
37
- "@commitlint/config-conventional": "^17.6.5",
36
+ "@commitlint/cli": "^17.6.7",
37
+ "@commitlint/config-conventional": "^17.6.7",
38
38
  "@testing-library/react": "^14.0.0",
39
- "@types/react": "^18.2.11",
40
- "@typescript-eslint/eslint-plugin": "^5.59.9",
41
- "@typescript-eslint/parser": "^5.59.9",
39
+ "@types/react": "^18.2.15",
40
+ "@typescript-eslint/eslint-plugin": "^6.1.0",
41
+ "@typescript-eslint/parser": "^6.1.0",
42
42
  "@vitejs/plugin-react": "^4.0.3",
43
- "eslint": "^8.42.0",
44
- "eslint-plugin-react": "^7.32.2",
43
+ "eslint": "^8.45.0",
44
+ "eslint-plugin-react": "^7.33.0",
45
45
  "eslint-plugin-react-hooks": "^4.6.0",
46
46
  "extra-react-hooks": "^0.6.3",
47
47
  "husky": "4",
@@ -50,13 +50,13 @@
50
50
  "npm-run-all": "^4.1.5",
51
51
  "react": "^18.2.0",
52
52
  "react-dom": "^18.2.0",
53
- "rimraf": "^4.3.0",
53
+ "rimraf": "^5.0.1",
54
54
  "standard-version": "^9.3.2",
55
- "ts-patch": "^2.1.0",
56
- "tslib": "^2.5.3",
57
- "typescript": "4.9.5",
55
+ "ts-patch": "^3.0.2",
56
+ "tslib": "^2.6.0",
57
+ "typescript": "5.1.6",
58
58
  "typescript-transform-paths": "^3.4.6",
59
- "vite": "^4.4.3",
59
+ "vite": "^4.4.5",
60
60
  "vite-tsconfig-paths": "^4.2.0",
61
61
  "vitest": "^0.33.0"
62
62
  },
package/src/types.ts CHANGED
@@ -10,5 +10,5 @@ export interface IStore<State> {
10
10
 
11
11
  export type Updater<State> = (...args:
12
12
  | [newState: State]
13
- | [fn: (draft: Draft<State>) => void]
13
+ | [fn: (draft: Draft<State>) => void | State]
14
14
  ) => void
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useContext } from 'react'
2
- import { produce } from 'immer'
2
+ import { Draft, produce } from 'immer'
3
3
  import { go, isFunction } from '@blackglory/prelude'
4
4
  import { StoreContext, Updater } from './types.js'
5
5
 
@@ -10,16 +10,21 @@ export function usePartialUpdater<State, PartialState>(
10
10
  ): Updater<PartialState> {
11
11
  const store = useContext(context)
12
12
 
13
- const updater: Updater<PartialState> = useCallback(newStateOrFn => {
13
+ const updater: Updater<PartialState> = useCallback(newPartialStateOrFn => {
14
14
  const oldState = store.getState()
15
15
 
16
16
  const newPartialState: PartialState = go(() => {
17
- if (isFunction(newStateOrFn)) {
18
- const fn = newStateOrFn
19
- return produce(extractPartialState(oldState), fn)
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
20
25
  } else {
21
- const newState = newStateOrFn
22
- return newState
26
+ const newPartialState = newPartialStateOrFn
27
+ return newPartialState
23
28
  }
24
29
  })
25
30
 
@@ -4,18 +4,20 @@ import { StoreContext } from './types.js'
4
4
  export function useSelector<State, Value>(
5
5
  context: StoreContext<State>
6
6
  , selector: (state: State) => Value
7
+ , isEqual: (a: Value, b: Value) => boolean = (a, b) => a === b
7
8
  ): Value {
8
9
  const store = useContext(context)
9
- const [value, setValue] = useState(() => selector(store.getState()))
10
+ const [[value], setBox] = useState(() => [selector(store.getState())])
10
11
 
11
12
  useEffect(() => {
12
13
  return store.subscribe(state => {
13
14
  const newValue = selector(state)
14
15
 
15
- // 若新旧状态引用相等, 不会发生重新渲染.
16
- setValue(newValue)
16
+ if (!isEqual(value, newValue)) {
17
+ setBox([newValue])
18
+ }
17
19
  })
18
- }, [store, selector, value])
20
+ }, [store, selector, value, setBox])
19
21
 
20
22
  return value
21
23
  }