ccstate-react 4.3.0 → 4.5.0
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/CHANGELOG.md +17 -0
- package/dist/index.cjs +7 -11
- package/dist/index.js +8 -12
- package/package.json +3 -3
- package/src/__tests__/get-and-set.test.tsx +32 -0
- package/src/useSet.ts +11 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# ccstate-react
|
|
2
2
|
|
|
3
|
+
## 4.5.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [9d65ed7]
|
|
8
|
+
- ccstate@4.5.0
|
|
9
|
+
|
|
10
|
+
## 4.4.0
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- de57ac4: fix: useSet should useCallback to keep stable
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- ccstate@4.4.0
|
|
19
|
+
|
|
3
20
|
## 4.3.0
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -30,18 +30,14 @@ function useGet(atom) {
|
|
|
30
30
|
|
|
31
31
|
function useSet(signal) {
|
|
32
32
|
var store = useStore();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return ret;
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return function (value) {
|
|
33
|
+
return react.useCallback('write' in signal ? function () {
|
|
34
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
35
|
+
args[_key] = arguments[_key];
|
|
36
|
+
}
|
|
37
|
+
return store.set.apply(store, [signal].concat(args));
|
|
38
|
+
} : function (value) {
|
|
43
39
|
store.set(signal, value);
|
|
44
|
-
};
|
|
40
|
+
}, [store, signal]);
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
function _arrayLikeToArray(r, a) {
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useContext, useSyncExternalStore, useState, useEffect } from 'react';
|
|
1
|
+
import { createContext, useContext, useSyncExternalStore, useCallback, useState, useEffect } from 'react';
|
|
2
2
|
import { getDefaultStore, command } from 'ccstate';
|
|
3
3
|
|
|
4
4
|
var StoreContext = createContext(null);
|
|
@@ -28,18 +28,14 @@ function useGet(atom) {
|
|
|
28
28
|
|
|
29
29
|
function useSet(signal) {
|
|
30
30
|
var store = useStore();
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return ret;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
return function (value) {
|
|
31
|
+
return useCallback('write' in signal ? function () {
|
|
32
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
33
|
+
args[_key] = arguments[_key];
|
|
34
|
+
}
|
|
35
|
+
return store.set.apply(store, [signal].concat(args));
|
|
36
|
+
} : function (value) {
|
|
41
37
|
store.set(signal, value);
|
|
42
|
-
};
|
|
38
|
+
}, [store, signal]);
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
function _arrayLikeToArray(r, a) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ccstate-react",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"description": "CCState React Hooks",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"react": ">=17.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"ccstate": "^4.
|
|
28
|
+
"ccstate": "^4.5.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependenciesMeta": {
|
|
31
31
|
"@types/react": {
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"shx": "^0.3.4",
|
|
56
56
|
"signal-timers": "^1.0.4",
|
|
57
57
|
"vitest": "^2.1.8",
|
|
58
|
-
"ccstate": "^4.
|
|
58
|
+
"ccstate": "^4.5.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "rollup -c",
|
|
@@ -260,3 +260,35 @@ describe('react', () => {
|
|
|
260
260
|
expect(store.getSubscribeGraph()).toHaveLength(0);
|
|
261
261
|
});
|
|
262
262
|
});
|
|
263
|
+
|
|
264
|
+
it('useSet should be stable', () => {
|
|
265
|
+
const count$ = state(0);
|
|
266
|
+
|
|
267
|
+
function Container() {
|
|
268
|
+
const count = useGet(count$);
|
|
269
|
+
return <Foo count={count} />;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function Foo({ count }: { count: number }) {
|
|
273
|
+
const setCount = useSet(count$);
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<>
|
|
277
|
+
count: {count}
|
|
278
|
+
<RenderCounter setCount={setCount} />
|
|
279
|
+
</>
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const trace = vi.fn();
|
|
284
|
+
function RenderCounter({ setCount }: { setCount: (val: number) => void }) {
|
|
285
|
+
trace(setCount);
|
|
286
|
+
setCount(1);
|
|
287
|
+
return <div>Render</div>;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
render(<Container />);
|
|
291
|
+
|
|
292
|
+
expect(trace).toHaveBeenCalledTimes(2);
|
|
293
|
+
expect(trace.mock.calls[0][0]).toBe(trace.mock.calls[1][0]);
|
|
294
|
+
});
|
package/src/useSet.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
1
2
|
import { useStore } from './provider';
|
|
2
3
|
import type { Command, State, StateArg } from 'ccstate';
|
|
3
4
|
|
|
@@ -13,15 +14,14 @@ export function useSet<T, CommandArgs extends unknown[]>(
|
|
|
13
14
|
): ValueSetter<T> | CommandInvoker<T, CommandArgs> {
|
|
14
15
|
const store = useStore();
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
17
|
+
return useCallback(
|
|
18
|
+
'write' in signal
|
|
19
|
+
? (...args: CommandArgs): T => {
|
|
20
|
+
return store.set(signal, ...args);
|
|
21
|
+
}
|
|
22
|
+
: (value: StateArg<T>) => {
|
|
23
|
+
store.set(signal, value);
|
|
24
|
+
},
|
|
25
|
+
[store, signal],
|
|
26
|
+
);
|
|
27
27
|
}
|