@restart/hooks
Advanced tools
Comparing version 0.3.18 to 0.3.19
@@ -9,3 +9,3 @@ /** | ||
/** | ||
* Creates a pasuable `setInterval` that is properly cleaned up when a component unmounted | ||
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted | ||
* | ||
@@ -17,2 +17,12 @@ * @param fn an function run on each interval | ||
declare function useInterval(fn: () => void, ms: number, paused: boolean): void; | ||
/** | ||
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted | ||
* | ||
* @param fn an function run on each interval | ||
* @param ms The milliseconds duration of the interval | ||
* @param paused Whether or not the interval is currently running | ||
* @param runImmediately Whether to run the function immediately on mount or unpause | ||
* rather than waiting for the first interval to elapse | ||
*/ | ||
declare function useInterval(fn: () => void, ms: number, paused: boolean, runImmediately: boolean): void; | ||
export default useInterval; |
@@ -12,3 +12,3 @@ "use strict"; | ||
function useInterval(fn, ms, paused) { | ||
function useInterval(fn, ms, paused, runImmediately) { | ||
if (paused === void 0) { | ||
@@ -18,2 +18,6 @@ paused = false; | ||
if (runImmediately === void 0) { | ||
runImmediately = false; | ||
} | ||
var handle; | ||
@@ -37,7 +41,12 @@ var fnRef = (0, _useCommittedRef.default)(fn); // this ref is necessary b/c useEffect will sometimes miss a paused toggle | ||
(0, _react.useEffect)(function () { | ||
schedule(); | ||
if (runImmediately) { | ||
tick(); | ||
} else { | ||
schedule(); | ||
} | ||
return function () { | ||
return clearTimeout(handle); | ||
}; | ||
}, [paused]); | ||
}, [paused, runImmediately]); | ||
} | ||
@@ -44,0 +53,0 @@ |
@@ -9,3 +9,3 @@ /** | ||
/** | ||
* Creates a pasuable `setInterval` that is properly cleaned up when a component unmounted | ||
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted | ||
* | ||
@@ -17,2 +17,12 @@ * @param fn an function run on each interval | ||
declare function useInterval(fn: () => void, ms: number, paused: boolean): void; | ||
/** | ||
* Creates a pausable `setInterval` that is properly cleaned up when a component unmounted | ||
* | ||
* @param fn an function run on each interval | ||
* @param ms The milliseconds duration of the interval | ||
* @param paused Whether or not the interval is currently running | ||
* @param runImmediately Whether to run the function immediately on mount or unpause | ||
* rather than waiting for the first interval to elapse | ||
*/ | ||
declare function useInterval(fn: () => void, ms: number, paused: boolean, runImmediately: boolean): void; | ||
export default useInterval; |
@@ -10,3 +10,3 @@ import { useEffect } from 'react'; | ||
function useInterval(fn, ms, paused) { | ||
function useInterval(fn, ms, paused, runImmediately) { | ||
if (paused === void 0) { | ||
@@ -16,2 +16,6 @@ paused = false; | ||
if (runImmediately === void 0) { | ||
runImmediately = false; | ||
} | ||
var handle; | ||
@@ -35,9 +39,14 @@ var fnRef = useCommittedRef(fn); // this ref is necessary b/c useEffect will sometimes miss a paused toggle | ||
useEffect(function () { | ||
schedule(); | ||
if (runImmediately) { | ||
tick(); | ||
} else { | ||
schedule(); | ||
} | ||
return function () { | ||
return clearTimeout(handle); | ||
}; | ||
}, [paused]); | ||
}, [paused, runImmediately]); | ||
} | ||
export default useInterval; |
{ | ||
"name": "@restart/hooks", | ||
"version": "0.3.18", | ||
"version": "0.3.19", | ||
"main": "cjs/index.js", | ||
@@ -42,3 +42,3 @@ "types": "cjs/index.d.ts", | ||
"readme": "ERROR: No README data found!", | ||
"_id": "@restart/hooks@0.3.17" | ||
"_id": "@restart/hooks@0.3.18" | ||
} |
@@ -16,3 +16,3 @@ # @restart/hooks [![npm][npm-badge]][npm] | ||
useInterval(() => loop(), false, 300) | ||
useInterval(() => loop(), 300, false) | ||
``` | ||
@@ -19,0 +19,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131220
3442