File tree 5 files changed +101
-1
lines changed
5 files changed +101
-1
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ connect react components with wire
20
20
- [ Global wire] ( #global-wire )
21
21
- [ ` useSelector ` hook] ( #useselector-hook )
22
22
- [ ` createSelector ` function] ( #createselector-function )
23
+ - [ ` useSubscribe ` hook] ( #usesubscribe-hook )
23
24
- [ Subscribe to the wire] ( #subscribe-to-the-wire )
24
25
- [ ` useInterceptor ` hook] ( #useinterceptor-hook )
25
26
- [ Notes] ( #notes )
@@ -277,9 +278,22 @@ function SomeComponent() {
277
278
}
278
279
```
279
280
281
+ ### ` useSubscribe ` hook
282
+
283
+ Every time the wire value changes, the callback function would be called
284
+
285
+ ``` tsx
286
+ // subscribe
287
+ useSubscribe (
288
+ useCallback (value => {
289
+ /* ... */
290
+ }, []),
291
+ );
292
+ ```
293
+
280
294
### Subscribe to the wire
281
295
282
- Every time wire value changed the callback function will be called
296
+ Every time the wire value changes, the callback function would be called
283
297
284
298
<details >
285
299
<summary >more detail</summary >
Original file line number Diff line number Diff line change @@ -184,6 +184,11 @@ export declare function useSelector<V, Fns = {}>(
184
184
options : ReadOnlySelectorOptions < V > ,
185
185
) : ReadonlyWire < V , Fns > ;
186
186
187
+ export declare function useSubscribe < V > (
188
+ wire : ReadonlyStateWire < V > ,
189
+ callback : ( value : Defined < V > ) => void ,
190
+ ) : void ;
191
+
187
192
export declare function useWire < V , Fns = { } > (
188
193
upLink : Wire < V , Fns > ,
189
194
) : Wire < V , Fns > ;
Original file line number Diff line number Diff line change @@ -14,3 +14,4 @@ export { useFn } from './fn-wire/use-fn';
14
14
export { createWire } from './wire/create-wire' ;
15
15
export { createSelector } from './selector/create-selector' ;
16
16
export { useSelector } from './selector/use-selector' ;
17
+ export { useSubscribe } from './state-wire/use-subscribe' ;
Original file line number Diff line number Diff line change
1
+ import { renderHook } from '@testing-library/react-hooks' ;
2
+ import { useCallback } from 'react' ;
3
+ import { act } from 'react-dom/test-utils' ;
4
+ import { useStateWire } from './use-state-wire' ;
5
+ import { useSubscribe } from './use-subscribe' ;
6
+
7
+ describe ( 'useSubscribe' , ( ) => {
8
+ it ( 'should not call the callback function on subscribing' , ( ) => {
9
+ const fn = jest . fn ( ) ;
10
+
11
+ renderHook ( ( ) => {
12
+ const wire = useStateWire ( null , 5 ) ;
13
+ useSubscribe (
14
+ wire ,
15
+ useCallback ( value => {
16
+ fn ( value ) ;
17
+ } , [ ] ) ,
18
+ ) ;
19
+ return { wire } ;
20
+ } ) ;
21
+ expect ( fn ) . not . toBeCalled ( ) ;
22
+ } ) ;
23
+
24
+ it ( 'should call the callback function when the wire value updates' , ( ) => {
25
+ const fn = jest . fn ( ) ;
26
+
27
+ const { result } = renderHook ( ( ) => {
28
+ const wire = useStateWire ( null , 5 ) ;
29
+ useSubscribe (
30
+ wire ,
31
+ useCallback ( value => {
32
+ fn ( value ) ;
33
+ } , [ ] ) ,
34
+ ) ;
35
+ return { wire } ;
36
+ } ) ;
37
+ act ( ( ) => {
38
+ result . current . wire . setValue ( 3 ) ;
39
+ } ) ;
40
+
41
+ expect ( fn ) . toBeCalledWith ( 3 ) ;
42
+ } ) ;
43
+
44
+ it ( 'should not call the callback function after unmount' , ( ) => {
45
+ const fn = jest . fn ( ) ;
46
+
47
+ const { result, unmount } = renderHook ( ( ) => {
48
+ const wire = useStateWire ( null , 5 ) ;
49
+ useSubscribe (
50
+ wire ,
51
+ useCallback ( value => {
52
+ fn ( value ) ;
53
+ } , [ ] ) ,
54
+ ) ;
55
+ return { wire } ;
56
+ } ) ;
57
+ act ( ( ) => {
58
+ unmount ( ) ;
59
+ } ) ;
60
+ act ( ( ) => {
61
+ result . current . wire . setValue ( 3 ) ;
62
+ } ) ;
63
+
64
+ expect ( fn ) . not . toBeCalled ( ) ;
65
+ } ) ;
66
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { useEffect } from 'react' ;
2
+ import { Defined } from '../utils/type-utils' ;
3
+ import { useStabilityGuard } from '../utils/use-stability-guard' ;
4
+ import { ReadonlyStateWire } from './readonly-state-wire' ;
5
+
6
+ export function useSubscribe < V > (
7
+ wire : ReadonlyStateWire < V > ,
8
+ callback : ( value : Defined < V > ) => void ,
9
+ ) : void {
10
+ useStabilityGuard ( wire ) ;
11
+ useEffect ( ( ) => {
12
+ return wire . subscribe ( callback ) ;
13
+ } , [ wire , callback ] ) ;
14
+ }
You can’t perform that action at this time.
0 commit comments