Skip to content

Commit 899369f

Browse files
feat: Add option in useInterval to run immediately (#15)
* Add option to run immediately on mount in useInterval * Add a test * One more test * Fix accidental changing of other file.
1 parent 83bcca3 commit 899369f

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ npm install @restart/hooks
1313
```js
1414
import useInterval from '@restart/hooks/useInterval'
1515

16-
useInterval(() => loop(), false, 300)
16+
useInterval(() => loop(), 300, false)
1717
```
1818

1919
[npm-badge]: https://img.shields.io/npm/v/@restart/hooks.svg

src/useInterval.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,32 @@ import useCommittedRef from './useCommittedRef'
88
* @param ms The milliseconds duration of the interval
99
*/
1010
function useInterval(fn: () => void, ms: number): void
11+
1112
/**
12-
* Creates a pasuable `setInterval` that is properly cleaned up when a component unmounted
13+
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
1314
*
1415
* @param fn an function run on each interval
1516
* @param ms The milliseconds duration of the interval
1617
* @param paused Whether or not the interval is currently running
1718
*/
1819
function useInterval(fn: () => void, ms: number, paused: boolean): void
20+
21+
/**
22+
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted
23+
*
24+
* @param fn an function run on each interval
25+
* @param ms The milliseconds duration of the interval
26+
* @param paused Whether or not the interval is currently running
27+
* @param runImmediately Whether to run the function immediately on mount or unpause
28+
* rather than waiting for the first interval to elapse
29+
*/
30+
function useInterval(fn: () => void, ms: number, paused: boolean, runImmediately: boolean): void
31+
1932
function useInterval(
2033
fn: () => void,
2134
ms: number,
2235
paused: boolean = false,
36+
runImmediately: boolean = false,
2337
): void {
2438
let handle: number
2539
const fnRef = useCommittedRef(fn)
@@ -38,9 +52,13 @@ function useInterval(
3852
}
3953

4054
useEffect(() => {
41-
schedule()
55+
if (runImmediately) {
56+
tick()
57+
} else {
58+
schedule()
59+
}
4260
return () => clearTimeout(handle)
43-
}, [paused])
61+
}, [paused, runImmediately])
4462
}
4563

4664
export default useInterval

test/useInterval.test.tsx

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React, { useEffect } from 'react'
2+
3+
import { mount } from 'enzyme'
4+
5+
import useInterval from '../src/useInterval'
6+
7+
describe('useTimeout', () => {
8+
it('should set an interval', () => {
9+
jest.useFakeTimers()
10+
11+
let spy = jest.fn()
12+
13+
function Wrapper() {
14+
useInterval(spy, 100);
15+
16+
return <span />
17+
}
18+
19+
mount(<Wrapper />)
20+
21+
expect(spy).not.toHaveBeenCalled()
22+
jest.runOnlyPendingTimers()
23+
expect(spy).toHaveBeenCalledTimes(1)
24+
jest.runOnlyPendingTimers()
25+
expect(spy).toHaveBeenCalledTimes(2)
26+
})
27+
28+
it('should run immediately when argument is set', () => {
29+
jest.useFakeTimers()
30+
let spy = jest.fn()
31+
32+
function Wrapper() {
33+
useInterval(spy, 100, false, true);
34+
35+
return <span />
36+
}
37+
38+
mount(<Wrapper />)
39+
40+
expect(spy).toHaveBeenCalledTimes(1)
41+
})
42+
43+
it('should not run when paused', () => {
44+
jest.useFakeTimers()
45+
let spy = jest.fn()
46+
47+
function Wrapper() {
48+
useInterval(spy, 100, true);
49+
50+
return <span />
51+
}
52+
53+
mount(<Wrapper />)
54+
55+
jest.runOnlyPendingTimers()
56+
expect(spy).not.toHaveBeenCalled()
57+
})
58+
59+
it('should stop running on unmount', () => {
60+
jest.useFakeTimers()
61+
let spy = jest.fn()
62+
63+
function Wrapper() {
64+
useInterval(spy, 100);
65+
66+
return <span />
67+
}
68+
69+
const wrapper = mount(<Wrapper />)
70+
wrapper.unmount()
71+
72+
jest.runOnlyPendingTimers()
73+
expect(spy).not.toHaveBeenCalled()
74+
})
75+
})

0 commit comments

Comments
 (0)