extra-react-store 0.2.1 → 0.2.3

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/store.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { Emitter } from '@blackglory/structures';
2
2
  export class Store {
3
- state;
4
- emitter = new Emitter();
5
3
  constructor(initialState) {
4
+ this.emitter = new Emitter();
6
5
  this.state = initialState;
7
6
  }
8
7
  getState() {
package/lib/store.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAGhD,MAAM,OAAO,KAAK;IACR,KAAK,CAAO;IACZ,OAAO,GAAG,IAAI,OAAO,EAAoC,CAAA;IAEjE,YAAY,YAAmB;QAC7B,IAAI,CAAC,KAAK,GAAG,YAAY,CAAA;IAC3B,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,QAAQ,CAAC,QAAe;QACtB,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAA;QACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAA;IAC7C,CAAC;IAED,SAAS,CAAC,EAA0B;QAClC,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,EAAE,CAAC,CAAA;IAC5C,CAAC;CACF"}
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAGhD,MAAM,OAAO,KAAK;IAIhB,YAAY,YAAmB;QAFvB,YAAO,GAAG,IAAI,OAAO,EAAoC,CAAA;QAG/D,IAAI,CAAC,KAAK,GAAG,YAAY,CAAA;IAC3B,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,QAAQ,CAAC,QAAe;QACtB,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAA;QACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAA;IAC7C,CAAC;IAED,SAAS,CAAC,EAA0B;QAClC,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,cAAc,EAAE,EAAE,CAAC,CAAA;IAC5C,CAAC;CACF"}
@@ -1,19 +1,35 @@
1
- import { useState, useContext } from 'react';
2
- import { useIIFE } 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
+ 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
+ }
7
18
  useIIFE(() => {
8
- let oldValue = value;
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,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAE9C,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC,EACjC,UAA2C,gBAAgB;IAE3D,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,OAAO,CAAC,GAAG,EAAE;QACX,IAAI,QAAQ,GAAG,KAAK,CAAA;QAEpB,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;YAC7B,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;YAEhC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAE,CAAC;gBACjC,QAAQ,GAAG,QAAQ,CAAA;gBACnB,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAA;YACpB,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;IAE7C,OAAO,KAAK,CAAA;AACd,CAAC"}
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,EA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AEvE,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.1",
3
+ "version": "0.2.3",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
@@ -35,13 +35,13 @@
35
35
  "devDependencies": {
36
36
  "@commitlint/cli": "^19.8.0",
37
37
  "@commitlint/config-conventional": "^19.8.0",
38
- "@eslint/js": "^9.23.0",
38
+ "@eslint/js": "^9.24.0",
39
39
  "@testing-library/dom": "^10.4.0",
40
- "@testing-library/react": "^16.2.0",
40
+ "@testing-library/react": "^16.3.0",
41
41
  "@types/react": "^19.1.0",
42
42
  "@vitejs/plugin-react": "^4.3.4",
43
- "eslint": "^9.23.0",
44
- "eslint-plugin-react": "^7.37.4",
43
+ "eslint": "^9.24.0",
44
+ "eslint-plugin-react": "^7.37.5",
45
45
  "eslint-plugin-react-hooks": "^5.2.0",
46
46
  "husky": "4",
47
47
  "immer": "^10.1.1",
@@ -53,17 +53,17 @@
53
53
  "standard-version": "^9.3.2",
54
54
  "ts-patch": "^3.3.0",
55
55
  "tslib": "^2.8.1",
56
- "typescript": "5.8.2",
56
+ "typescript": "5.8.3",
57
57
  "typescript-eslint": "^8.29.0",
58
58
  "typescript-transform-paths": "^3.5.5",
59
- "vite": "^6.2.4",
59
+ "vite": "^6.2.5",
60
60
  "vite-tsconfig-paths": "^5.1.4",
61
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.4",
66
+ "extra-react-hooks": "^0.8.6",
67
67
  "extra-utils": "^5.19.0"
68
68
  },
69
69
  "peerDependencies": {
@@ -1,5 +1,5 @@
1
- import { useState, useContext } from 'react'
2
- import { useIIFE } 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,21 +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
+ const updateRequireRef = useRef<boolean>(true)
14
15
  useIIFE(() => {
15
- let oldValue = value
16
+ return () => {
17
+ updateRequireRef.current = true
18
+ }
19
+ }, [selector])
16
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(() => {
17
28
  return store.subscribe(state => {
18
- const newValue = selector(state)
29
+ try {
30
+ const newValue = selector(state)
19
31
 
20
- if (!isEqual(oldValue, newValue)) {
21
- oldValue = newValue
22
- setBox([newValue])
32
+ if (!isEqual(valueRef.current!, newValue)) {
33
+ valueRef.current = newValue
34
+ forceUpdate()
35
+ }
36
+ } catch {
37
+ updateRequireRef.current = true
38
+ forceUpdate()
23
39
  }
24
40
  })
25
- }, [store, selector, isEqual, value, setBox])
41
+ }, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef])
26
42
 
27
- return value
43
+ return valueRef.current!
28
44
  }