Skip to content

Commit

Permalink
feat(state-wire): add useSubscribe hook (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
smmoosavi authored Jul 15, 2020
1 parent 562ad25 commit 6190f18
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ connect react components with wire
- [Global wire](#global-wire)
- [`useSelector` hook](#useselector-hook)
- [`createSelector` function](#createselector-function)
- [`useSubscribe` hook](#usesubscribe-hook)
- [Subscribe to the wire](#subscribe-to-the-wire)
- [`useInterceptor` hook](#useinterceptor-hook)
- [Notes](#notes)
Expand Down Expand Up @@ -277,9 +278,22 @@ function SomeComponent() {
}
```

### `useSubscribe` hook

Every time the wire value changes, the callback function would be called

```tsx
// subscribe
useSubscribe(
useCallback(value => {
/* ... */
}, []),
);
```

### Subscribe to the wire

Every time wire value changed the callback function will be called
Every time the wire value changes, the callback function would be called

<details>
<summary>more detail</summary>
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export declare function useSelector<V, Fns = {}>(
options: ReadOnlySelectorOptions<V>,
): ReadonlyWire<V, Fns>;

export declare function useSubscribe<V>(
wire: ReadonlyStateWire<V>,
callback: (value: Defined<V>) => void,
): void;

export declare function useWire<V, Fns = {}>(
upLink: Wire<V, Fns>,
): Wire<V, Fns>;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export { useFn } from './fn-wire/use-fn';
export { createWire } from './wire/create-wire';
export { createSelector } from './selector/create-selector';
export { useSelector } from './selector/use-selector';
export { useSubscribe } from './state-wire/use-subscribe';
66 changes: 66 additions & 0 deletions src/state-wire/use-subscribe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { renderHook } from '@testing-library/react-hooks';
import { useCallback } from 'react';
import { act } from 'react-dom/test-utils';
import { useStateWire } from './use-state-wire';
import { useSubscribe } from './use-subscribe';

describe('useSubscribe', () => {
it('should not call the callback function on subscribing', () => {
const fn = jest.fn();

renderHook(() => {
const wire = useStateWire(null, 5);
useSubscribe(
wire,
useCallback(value => {
fn(value);
}, []),
);
return { wire };
});
expect(fn).not.toBeCalled();
});

it('should call the callback function when the wire value updates', () => {
const fn = jest.fn();

const { result } = renderHook(() => {
const wire = useStateWire(null, 5);
useSubscribe(
wire,
useCallback(value => {
fn(value);
}, []),
);
return { wire };
});
act(() => {
result.current.wire.setValue(3);
});

expect(fn).toBeCalledWith(3);
});

it('should not call the callback function after unmount', () => {
const fn = jest.fn();

const { result, unmount } = renderHook(() => {
const wire = useStateWire(null, 5);
useSubscribe(
wire,
useCallback(value => {
fn(value);
}, []),
);
return { wire };
});
act(() => {
unmount();
});
act(() => {
result.current.wire.setValue(3);
});

expect(fn).not.toBeCalled();
});
});
14 changes: 14 additions & 0 deletions src/state-wire/use-subscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect } from 'react';
import { Defined } from '../utils/type-utils';
import { useStabilityGuard } from '../utils/use-stability-guard';
import { ReadonlyStateWire } from './readonly-state-wire';

export function useSubscribe<V>(
wire: ReadonlyStateWire<V>,
callback: (value: Defined<V>) => void,
): void {
useStabilityGuard(wire);
useEffect(() => {
return wire.subscribe(callback);
}, [wire, callback]);
}

0 comments on commit 6190f18

Please sign in to comment.