ccstate-react 4.2.0 → 4.4.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 +9 -13
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +10 -14
- package/package.json +3 -3
- package/src/__tests__/get-and-set.test.tsx +32 -0
- package/src/useSet.ts +22 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# ccstate-react
|
|
2
2
|
|
|
3
|
+
## 4.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- de57ac4: fix: useSet should useCallback to keep stable
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- ccstate@4.4.0
|
|
12
|
+
|
|
13
|
+
## 4.3.0
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [89d98a2]
|
|
18
|
+
- ccstate@4.3.0
|
|
19
|
+
|
|
3
20
|
## 4.2.0
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -28,20 +28,16 @@ function useGet(atom) {
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
function useSet(
|
|
31
|
+
function useSet(signal) {
|
|
32
32
|
var store = useStore();
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
return function (value) {
|
|
43
|
-
store.set(atom, value);
|
|
44
|
-
};
|
|
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) {
|
|
39
|
+
store.set(signal, value);
|
|
40
|
+
}, [store, signal]);
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
function _arrayLikeToArray(r, a) {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { State, Computed,
|
|
1
|
+
import { State, Computed, Command, StateArg, Store } from 'ccstate';
|
|
2
2
|
import * as react from 'react';
|
|
3
3
|
|
|
4
4
|
declare function useGet<T>(atom: State<T> | Computed<T>): T;
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
type ValueSetter<T> = (val: StateArg<T>) => void;
|
|
7
|
+
type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) => T;
|
|
8
|
+
declare function useSet<T>(signal: State<T>): ValueSetter<T>;
|
|
9
|
+
declare function useSet<T, CommandArgs extends unknown[]>(signal: Command<T, CommandArgs>): CommandInvoker<T, CommandArgs>;
|
|
8
10
|
|
|
9
11
|
declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
|
10
12
|
declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { State, Computed,
|
|
1
|
+
import { State, Computed, Command, StateArg, Store } from 'ccstate';
|
|
2
2
|
import * as react from 'react';
|
|
3
3
|
|
|
4
4
|
declare function useGet<T>(atom: State<T> | Computed<T>): T;
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
type ValueSetter<T> = (val: StateArg<T>) => void;
|
|
7
|
+
type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) => T;
|
|
8
|
+
declare function useSet<T>(signal: State<T>): ValueSetter<T>;
|
|
9
|
+
declare function useSet<T, CommandArgs extends unknown[]>(signal: Command<T, CommandArgs>): CommandInvoker<T, CommandArgs>;
|
|
8
10
|
|
|
9
11
|
declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
|
10
12
|
declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
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);
|
|
@@ -26,20 +26,16 @@ function useGet(atom) {
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
function useSet(
|
|
29
|
+
function useSet(signal) {
|
|
30
30
|
var store = useStore();
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
return function (value) {
|
|
41
|
-
store.set(atom, value);
|
|
42
|
-
};
|
|
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) {
|
|
37
|
+
store.set(signal, value);
|
|
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.4.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.4.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.4.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,22 +1,27 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
1
2
|
import { useStore } from './provider';
|
|
2
|
-
import type { Command,
|
|
3
|
+
import type { Command, State, StateArg } from 'ccstate';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export function useSet<T, ARGS extends unknown[]>(
|
|
7
|
-
atom: State<T> | Command<T, ARGS>,
|
|
8
|
-
): ((value: T | Updater<T>) => void) | ((...args: ARGS) => T) {
|
|
9
|
-
const store = useStore();
|
|
10
|
-
|
|
11
|
-
if ('write' in atom) {
|
|
12
|
-
return (...args: ARGS): T => {
|
|
13
|
-
const ret = store.set(atom, ...args);
|
|
5
|
+
type ValueSetter<T> = (val: StateArg<T>) => void;
|
|
6
|
+
type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) => T;
|
|
14
7
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
export function useSet<T>(signal: State<T>): ValueSetter<T>;
|
|
9
|
+
export function useSet<T, CommandArgs extends unknown[]>(
|
|
10
|
+
signal: Command<T, CommandArgs>,
|
|
11
|
+
): CommandInvoker<T, CommandArgs>;
|
|
12
|
+
export function useSet<T, CommandArgs extends unknown[]>(
|
|
13
|
+
signal: State<T> | Command<T, CommandArgs>,
|
|
14
|
+
): ValueSetter<T> | CommandInvoker<T, CommandArgs> {
|
|
15
|
+
const store = useStore();
|
|
18
16
|
|
|
19
|
-
return (
|
|
20
|
-
|
|
21
|
-
|
|
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
|
+
);
|
|
22
27
|
}
|