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.
- package/lib/use-selector.js +27 -11
- package/lib/use-selector.js.map +1 -1
- package/package.json +18 -18
- package/src/use-selector.ts +27 -12
package/lib/use-selector.js
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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,
|
|
17
|
-
return
|
|
32
|
+
}, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef]);
|
|
33
|
+
return valueRef.current;
|
|
18
34
|
}
|
|
19
35
|
//# sourceMappingURL=use-selector.js.map
|
package/lib/use-selector.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAE3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAE9C,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC,EACjC,UAA2C,gBAAgB;IAE3D,MAAM,WAAW,GAAG,cAAc,EAAE,CAAA;IACpC,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAA;IAEjC,MAAM,gBAAgB,GAAG,MAAM,CAAU,IAAI,CAAC,CAAA;IAC9C,OAAO,CAAC,GAAG,EAAE;QACX,OAAO,GAAG,EAAE;YACV,gBAAgB,CAAC,OAAO,GAAG,IAAI,CAAA;QACjC,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEd,MAAM,QAAQ,GAAG,MAAM,CAAQ,SAAS,CAAC,CAAA;IACzC,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC7B,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC7C,gBAAgB,CAAC,OAAO,GAAG,KAAK,CAAA;IAClC,CAAC;IAED,OAAO,CAAC,GAAG,EAAE;QACX,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;YAC7B,IAAI,CAAC;gBACH,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;gBAEhC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAQ,EAAE,QAAQ,CAAC,EAAE,CAAC;oBAC1C,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAA;oBAC3B,WAAW,EAAE,CAAA;gBACf,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,gBAAgB,CAAC,OAAO,GAAG,IAAI,CAAA;gBAC/B,WAAW,EAAE,CAAA;YACf,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAA;IAEvE,OAAO,QAAQ,CAAC,OAAQ,CAAA;AAC1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-react-store",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "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.
|
|
37
|
-
"@commitlint/config-conventional": "^19.
|
|
38
|
-
"@eslint/js": "^9.
|
|
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.
|
|
41
|
-
"@types/react": "^19.0
|
|
40
|
+
"@testing-library/react": "^16.3.0",
|
|
41
|
+
"@types/react": "^19.1.0",
|
|
42
42
|
"@vitejs/plugin-react": "^4.3.4",
|
|
43
|
-
"eslint": "^9.
|
|
44
|
-
"eslint-plugin-react": "^7.37.
|
|
45
|
-
"eslint-plugin-react-hooks": "^5.
|
|
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.
|
|
51
|
-
"react-dom": "^19.
|
|
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.
|
|
57
|
-
"typescript-eslint": "^8.
|
|
58
|
-
"typescript-transform-paths": "^3.5.
|
|
59
|
-
"vite": "^6.
|
|
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": "^
|
|
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.
|
|
67
|
-
"extra-utils": "^5.
|
|
66
|
+
"extra-react-hooks": "^0.8.6",
|
|
67
|
+
"extra-utils": "^5.19.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"immer": "^10.0.2",
|
package/src/use-selector.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
29
|
+
try {
|
|
30
|
+
const newValue = selector(state)
|
|
20
31
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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,
|
|
41
|
+
}, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef])
|
|
27
42
|
|
|
28
|
-
return
|
|
43
|
+
return valueRef.current!
|
|
29
44
|
}
|