Skip to content

Commit d9f21e3

Browse files
committed
feat: 🎸 add useHarmonicIntervalFn() hook
1 parent a851e90 commit d9f21e3

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"react-fast-compare": "^2.0.4",
5252
"react-wait": "^0.3.0",
5353
"screenfull": "^4.1.0",
54+
"set-harmonic-interval": "^1.0.0",
5455
"throttle-debounce": "^2.0.1",
5556
"ts-easing": "^0.2.0"
5657
},
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { storiesOf } from '@storybook/react';
2+
import * as React from 'react';
3+
import { useHarmonicIntervalFn, useInterval, useTimeoutFn } from '..';
4+
import ShowDocs from './util/ShowDocs';
5+
6+
const Clock: React.FC<{ useInt: typeof useHarmonicIntervalFn }> = ({ useInt }) => {
7+
const [count, setCount] = React.useState(0);
8+
useInt(() => {
9+
setCount(cnt => cnt + 1);
10+
}, 1000);
11+
12+
let m: number | string = Math.floor(count / 60);
13+
let s: number | string = count % 60;
14+
15+
m = m < 10 ? '0' + m : String(m);
16+
s = s < 10 ? '0' + s : String(s);
17+
18+
const style: React.CSSProperties = {
19+
padding: '20px',
20+
border: '1px solid #fafafa',
21+
float: 'left',
22+
fontFamily: 'monospace',
23+
};
24+
25+
return <div style={style}>{m + ':' + s}</div>;
26+
};
27+
28+
const Demo: React.FC<{ useInt: typeof useHarmonicIntervalFn }> = ({ useInt }) => {
29+
const [showSecondClock, setShowSecondClock] = React.useState(false);
30+
useTimeoutFn(() => {
31+
setShowSecondClock(true);
32+
}, 500);
33+
34+
const headingStyle: React.CSSProperties = {
35+
fontFamily: 'sans',
36+
fontSize: '20px',
37+
padding: '30px 0 0',
38+
};
39+
40+
const rowStyle: React.CSSProperties = {
41+
width: '100%',
42+
clear: 'both',
43+
};
44+
45+
return (
46+
<>
47+
<div style={rowStyle}>
48+
<h2 style={headingStyle}>{useInt.name}</h2>
49+
<Clock useInt={useInt} />
50+
{showSecondClock ? <Clock useInt={useInt} /> : null}
51+
</div>
52+
</>
53+
);
54+
};
55+
56+
storiesOf('Side effects|useHarmonicIntervalFn', module)
57+
.add('Docs', () => <ShowDocs md={require('../../docs/useInterval.md')} />)
58+
.add('Demo', () => (
59+
<>
60+
<Demo useInt={useInterval} />
61+
<Demo useInt={useHarmonicIntervalFn} />
62+
</>
63+
));

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export { default as useFullscreen } from './useFullscreen';
2323
export { default as useGeolocation } from './useGeolocation';
2424
export { default as useGetSet } from './useGetSet';
2525
export { default as useGetSetState } from './useGetSetState';
26+
export { default as useHarmonicIntervalFn } from './useHarmonicIntervalFn';
2627
export { default as useHover } from './useHover';
2728
export { default as useHoverDirty } from './useHoverDirty';
2829
export { default as useIdle } from './useIdle';

src/useHarmonicIntervalFn.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useEffect, useRef } from 'react';
2+
import { setHarmonicInterval, clearHarmonicInterval } from 'set-harmonic-interval';
3+
4+
const useHarmonicIntervalFn = (fn: Function, delay: number | null = 0) => {
5+
const latestCallback = useRef<Function>(() => {});
6+
7+
useEffect(() => {
8+
latestCallback.current = fn;
9+
});
10+
11+
useEffect(() => {
12+
if (delay !== null) {
13+
const interval = setHarmonicInterval(() => latestCallback.current(), delay);
14+
return () => clearHarmonicInterval(interval);
15+
}
16+
return undefined;
17+
}, [delay]);
18+
};
19+
20+
export default useHarmonicIntervalFn;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11062,6 +11062,11 @@ set-blocking@^2.0.0, set-blocking@~2.0.0:
1106211062
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
1106311063
integrity sha1-BF+XgtARrppoA93TgrJDkrPYkPc=
1106411064

11065+
set-harmonic-interval@^1.0.0:
11066+
version "1.0.0"
11067+
resolved "https://registry.yarnpkg.com/set-harmonic-interval/-/set-harmonic-interval-1.0.0.tgz#2759658395579fc336b9cd026eefe1ad9e742f9b"
11068+
integrity sha512-RJtrhB5G10e5A1auBv/jHGq0KWfHH8PAb4ln4+kjiHS46aAP12rSdarSj9GDlxQ3QULA2pefEDpm9Y1Xnz+eng==
11069+
1106511070
set-value@^0.4.3:
1106611071
version "0.4.3"
1106711072
resolved "https://registry.yarnpkg.com/set-value/-/set-value-0.4.3.tgz#7db08f9d3d22dc7f78e53af3c3bf4666ecdfccf1"

0 commit comments

Comments
 (0)