extra-react-store 0.2.0 → 0.2.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.
@@ -1,19 +1,35 @@
1
- import { useState, useContext } from 'react';
2
- import { useIsomorphicLayoutEffect } from 'extra-react-hooks';
1
+ import { useContext, useRef } from 'react';
2
+ import { useIIFE, useForceUpdate } from 'extra-react-hooks';
3
3
  import { isReferenceEqual } from 'extra-utils';
4
4
  export function useSelector(context, selector, isEqual = isReferenceEqual) {
5
+ const forceUpdate = useForceUpdate();
5
6
  const store = useContext(context);
6
- const [[value], setBox] = useState(() => [selector(store.getState())]);
7
- useIsomorphicLayoutEffect(() => {
8
- let oldValue = value;
7
+ const updateRequireRef = useRef(true);
8
+ useIIFE(() => {
9
+ return () => {
10
+ updateRequireRef.current = true;
11
+ };
12
+ }, [selector]);
13
+ const valueRef = useRef(undefined);
14
+ if (updateRequireRef.current) {
15
+ valueRef.current = selector(store.getState());
16
+ updateRequireRef.current = false;
17
+ }
18
+ useIIFE(() => {
9
19
  return store.subscribe(state => {
10
- const newValue = selector(state);
11
- if (!isEqual(oldValue, newValue)) {
12
- oldValue = newValue;
13
- setBox([newValue]);
20
+ try {
21
+ const newValue = selector(state);
22
+ if (!isEqual(valueRef.current, newValue)) {
23
+ valueRef.current = newValue;
24
+ forceUpdate();
25
+ }
26
+ }
27
+ catch {
28
+ updateRequireRef.current = true;
29
+ forceUpdate();
14
30
  }
15
31
  });
16
- }, [store, selector, isEqual, value, setBox]);
17
- return value;
32
+ }, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef]);
33
+ return valueRef.current;
18
34
  }
19
35
  //# 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,MAAM,OAAO,CAAA;AAC5C,OAAO,EAAE,yBAAyB,EAAE,MA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": "0.2.2",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
@@ -33,38 +33,38 @@
33
33
  }
34
34
  },
35
35
  "devDependencies": {
36
- "@commitlint/cli": "^19.6.1",
37
- "@commitlint/config-conventional": "^19.6.0",
38
- "@eslint/js": "^9.18.0",
36
+ "@commitlint/cli": "^19.8.0",
37
+ "@commitlint/config-conventional": "^19.8.0",
38
+ "@eslint/js": "^9.24.0",
39
39
  "@testing-library/dom": "^10.4.0",
40
- "@testing-library/react": "^16.1.0",
41
- "@types/react": "^19.0.7",
40
+ "@testing-library/react": "^16.3.0",
41
+ "@types/react": "^19.1.0",
42
42
  "@vitejs/plugin-react": "^4.3.4",
43
- "eslint": "^9.18.0",
44
- "eslint-plugin-react": "^7.37.4",
45
- "eslint-plugin-react-hooks": "^5.1.0",
43
+ "eslint": "^9.24.0",
44
+ "eslint-plugin-react": "^7.37.5",
45
+ "eslint-plugin-react-hooks": "^5.2.0",
46
46
  "husky": "4",
47
47
  "immer": "^10.1.1",
48
48
  "jsdom": "^26.0.0",
49
49
  "npm-run-all": "^4.1.5",
50
- "react": "^19.0.0",
51
- "react-dom": "^19.0.0",
50
+ "react": "^19.1.0",
51
+ "react-dom": "^19.1.0",
52
52
  "rimraf": "^6.0.1",
53
53
  "standard-version": "^9.3.2",
54
54
  "ts-patch": "^3.3.0",
55
55
  "tslib": "^2.8.1",
56
- "typescript": "5.7.3",
57
- "typescript-eslint": "^8.20.0",
58
- "typescript-transform-paths": "^3.5.3",
59
- "vite": "^6.0.7",
56
+ "typescript": "5.8.3",
57
+ "typescript-eslint": "^8.29.0",
58
+ "typescript-transform-paths": "^3.5.5",
59
+ "vite": "^6.2.5",
60
60
  "vite-tsconfig-paths": "^5.1.4",
61
- "vitest": "^2.1.8"
61
+ "vitest": "^3.1.1"
62
62
  },
63
63
  "dependencies": {
64
64
  "@blackglory/prelude": "^0.4.0",
65
65
  "@blackglory/structures": "^0.14.10",
66
- "extra-react-hooks": "^0.8.0",
67
- "extra-utils": "^5.17.0"
66
+ "extra-react-hooks": "^0.8.6",
67
+ "extra-utils": "^5.19.0"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "immer": "^10.0.2",
@@ -1,5 +1,5 @@
1
- import { useState, useContext } from 'react'
2
- import { useIsomorphicLayoutEffect } from 'extra-react-hooks'
1
+ import { useContext, useRef } from 'react'
2
+ import { useIIFE, useForceUpdate } from 'extra-react-hooks'
3
3
  import { StoreContext } from './types.js'
4
4
  import { isReferenceEqual } from 'extra-utils'
5
5
 
@@ -8,22 +8,37 @@ export function useSelector<State, Value>(
8
8
  , selector: (state: State) => Value
9
9
  , isEqual: (a: Value, b: Value) => boolean = isReferenceEqual
10
10
  ): Value {
11
+ const forceUpdate = useForceUpdate()
11
12
  const store = useContext(context)
12
- const [[value], setBox] = useState(() => [selector(store.getState())])
13
13
 
14
- // `useEffect` will be called too late, resulting in not subscribing in time.
15
- useIsomorphicLayoutEffect(() => {
16
- let oldValue = value
14
+ const updateRequireRef = useRef<boolean>(true)
15
+ useIIFE(() => {
16
+ return () => {
17
+ updateRequireRef.current = true
18
+ }
19
+ }, [selector])
17
20
 
21
+ const valueRef = useRef<Value>(undefined)
22
+ if (updateRequireRef.current) {
23
+ valueRef.current = selector(store.getState())
24
+ updateRequireRef.current = false
25
+ }
26
+
27
+ useIIFE(() => {
18
28
  return store.subscribe(state => {
19
- const newValue = selector(state)
29
+ try {
30
+ const newValue = selector(state)
20
31
 
21
- if (!isEqual(oldValue, newValue)) {
22
- oldValue = newValue
23
- setBox([newValue])
32
+ if (!isEqual(valueRef.current!, newValue)) {
33
+ valueRef.current = newValue
34
+ forceUpdate()
35
+ }
36
+ } catch {
37
+ updateRequireRef.current = true
38
+ forceUpdate()
24
39
  }
25
40
  })
26
- }, [store, selector, isEqual, value, setBox])
41
+ }, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef])
27
42
 
28
- return value
43
+ return valueRef.current!
29
44
  }