Skip to content

Commit

Permalink
refactor: remove arg from usePollingValue
Browse files Browse the repository at this point in the history
  • Loading branch information
emilianobovetti committed Dec 12, 2019
1 parent d117fc2 commit 4939ba7
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
15 changes: 0 additions & 15 deletions __tests__/usePollingValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,3 @@ test('usePollingValue returns an update callback', () => {
const [value] = result.current;
expect(value).toBe(1);
});

test('usePollingValue calls its function with given argument', () => {
const fn = jest.fn();

const { result } = renderHook(() => usePollingValue(fn, 1));
const [, update] = result.current;

expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0]).toEqual([1]);

act(() => update(2));

expect(fn.mock.calls.length).toBe(2);
expect(fn.mock.calls[1]).toEqual([2]);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fpc/hooks",
"description": "Useful hooks for react apps",
"version": "0.0.9",
"version": "0.0.10",
"author": "Emiliano Bovetti <emiliano.bovetti@gmail.com>",
"license": "GPL-3.0",
"keywords": [
Expand Down
8 changes: 4 additions & 4 deletions src/usePollingValue.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useRef, useReducer } from 'react';
import { expectFunction } from '@fpc/types';

const poll = ({ ref, arg }) => ({ value: ref.current.call(null, arg), ref });
const reducer = ({ ref }, arg) => poll({ ref, arg });
const evaluate = ref => ({ value: ref.current.call(null), ref });
const reducer = ({ ref }) => evaluate(ref);

export const usePollingValue = (fn, arg) => {
export const usePollingValue = fn => {
const ref = useRef();
ref.current = expectFunction(fn);

const [state, update] = useReducer(reducer, { ref, arg }, poll);
const [state, update] = useReducer(reducer, ref, evaluate);

return [state.value, update];
};

0 comments on commit 4939ba7

Please sign in to comment.