Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger an optional function after any counter, stopwatch or timer finishes #22

Merged
merged 5 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ It takes three arguments:
- `min` (number): the initial value of the counter.
- `max`(number): the final value of the counter. It has to be greater than `min`.
- `startPaused` (optional boolean): a boolean flag that determines whether the counter should start in a paused state. Defaults to false.
- `onFinish` (optional function): a function that will be called when the counter reaches the final value.

It returns an object with the following props:

Expand Down Expand Up @@ -83,6 +84,7 @@ It takes three arguments:
- `min` (number): the initial value of the counter.
- `max`(number): the final value of the counter. It has to be greater than `min`.
- `startPaused` (optional boolean): a boolean flag that determines whether the counter should start in a paused state. Defaults to false.
- `onFinish` (optional function): a function that will be called when the counter reaches the final value.

It returns an object with the following props:

Expand Down Expand Up @@ -134,6 +136,7 @@ It takes six arguments:
- `seconds` (number): the final value of the seconds. The value must be between 0 (inclusive) and 60 (exclusive).
- `startPaused` (optional boolean): a boolean flag that determines whether the stopwatch should start in a paused state. Defaults to false.
- `separator` (optional string): a string that specifies the separator to be used between days, hours, minutes, and seconds when the time is represented as a string. By default, colon (:) is used as a separator.
- `onFinish` (optional function): a function that will be called when the stopwatch reaches the final value.

It returns an object with the following props:

Expand Down Expand Up @@ -282,6 +285,7 @@ It takes six arguments:
- `seconds` (number): the initial value of the seconds. The value must be between 0 (inclusive) and 60 (exclusive).
- `startPaused` (optional boolean): a boolean flag that determines whether the timer should start in a paused state. Defaults to false.
- `separator` (optional string): a string that specifies the separator to be used between days, hours, minutes, and seconds when the time is represented as a string. By default, colon (:) is used as a separator.
- `onFinish` (optional function): a function that will be called when the timer reaches the final value.

It returns an object with the following props:

Expand Down
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCountDown } from "./hooks";
import { useTimer } from "./hooks";

const App = () => {
const counter = useCountDown(0, 10);
const counter = useTimer(0, 0, 0, 2);

return (
<div>
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/useCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const useCounter = (
min: number,
max: number,
isCountingUp: boolean,
startPaused?: boolean
startPaused?: boolean,
onFinish?: () => void
): BaseCounter => {
if (min >= max) {
throw new Error("The min parameter has to be less than the max parameter.");
Expand Down Expand Up @@ -36,6 +37,10 @@ export const useCounter = (
return () => clearInterval(interval);
}, [count, min, max, paused, isCountingUp]);

useEffect(() => {
isOver && onFinish && onFinish();
}, [isOver]);

return {
current: {
withLeadingZero: addLeadingZero(count),
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/useInternalStopwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const useInternalStopwatch = (
minutes: number,
seconds: number,
startPaused?: boolean,
separator?: string
separator?: string,
onFinish?: () => void
): InternalCounter => {
const [time, setTime] = useState({
days: 0,
Expand Down Expand Up @@ -67,6 +68,10 @@ export const useInternalStopwatch = (
return () => clearInterval(interval);
}, [days, hours, minutes, seconds, time, paused]);

useEffect(() => {
isOver && onFinish && onFinish();
}, [isOver]);

return {
current: {
withLeadingZero: `${addLeadingZero(time.days)}${divider}${addLeadingZero(
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/useInternalTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const useInternalTimer = (
minutes: number,
seconds: number,
startPaused?: boolean,
separator?: string
separator?: string,
onFinish?: () => void
): InternalCounter => {
const [time, setTime] = useState({ days, hours, minutes, seconds });
const [paused, setPaused] = useState(startPaused ?? false);
Expand Down Expand Up @@ -64,6 +65,10 @@ export const useInternalTimer = (
return () => clearInterval(interval);
}, [days, hours, minutes, seconds, time, paused]);

useEffect(() => {
isOver && onFinish && onFinish();
}, [isOver]);

return {
current: {
withLeadingZero: `${addLeadingZero(time.days)}${divider}${addLeadingZero(
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useCountDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { useCounter } from "../helpers";
export const useCountDown = (
min: number,
max: number,
startPaused?: boolean
): BaseCounter => useCounter(min, max, false, startPaused);
startPaused?: boolean,
onFinish?: () => void
): BaseCounter => useCounter(min, max, false, startPaused, onFinish);
5 changes: 3 additions & 2 deletions src/hooks/useCountUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { useCounter } from "../helpers";
export const useCountUp = (
min: number,
max: number,
startPaused?: boolean
): BaseCounter => useCounter(min, max, true, startPaused);
startPaused?: boolean,
onFinish?: () => void
): BaseCounter => useCounter(min, max, true, startPaused, onFinish);
6 changes: 4 additions & 2 deletions src/hooks/useStopwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const useStopwatch = (
minutes: number,
seconds: number,
startPaused?: boolean,
separator?: string
separator?: string,
onFinish?: () => void
): Stopwatch => {
handleTimerErrors(days, hours, minutes, seconds);

Expand All @@ -25,7 +26,8 @@ export const useStopwatch = (
minutes,
seconds,
startPaused,
separator
separator,
onFinish
);
const timer = useInternalTimer(
days,
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
handleTimerErrors,
} from "../helpers";

export interface Timer extends BaseCounter, BaseCounterStatus {
interface Timer extends BaseCounter, BaseCounterStatus {
elapsedTime: Zero;
}

Expand All @@ -15,7 +15,8 @@ export const useTimer = (
minutes: number,
seconds: number,
startPaused?: boolean,
separator?: string
separator?: string,
onFinish?: () => void
): Timer => {
handleTimerErrors(days, hours, minutes, seconds);

Expand All @@ -25,7 +26,8 @@ export const useTimer = (
minutes,
seconds,
startPaused,
separator
separator,
onFinish
);
const stopwatch = useInternalStopwatch(
days,
Expand Down