From 689ff3cddb39750f65a01621171c7df374636b4d Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Fri, 30 Apr 2021 19:11:28 +0700 Subject: [PATCH] Fixed timeout and dists for build and publish --- dist/index.d.ts | 1 + dist/index.js | 8 +++++++ dist/useCountdown.d.ts | 12 ++++++++++ dist/useCountdown.js | 54 ++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/useCountdown.ts | 2 +- 6 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 dist/useCountdown.d.ts create mode 100644 dist/useCountdown.js diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..0174f99 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1 @@ +export { default as useCountdown } from './useCountdown'; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..03a1634 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,8 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.useCountdown = void 0; +var useCountdown_1 = require("./useCountdown"); +Object.defineProperty(exports, "useCountdown", { enumerable: true, get: function () { return __importDefault(useCountdown_1).default; } }); diff --git a/dist/useCountdown.d.ts b/dist/useCountdown.d.ts new file mode 100644 index 0000000..d1c80e9 --- /dev/null +++ b/dist/useCountdown.d.ts @@ -0,0 +1,12 @@ +interface useCountdownOptions { + now?: () => Date; + onEnd?: () => void; + onCount?: (timeLeft: number) => void; + step?: number; +} +interface CountdownHookResult { + timeleft: number; + start: (timeLeft: number) => void; +} +declare function useCountdown(options?: useCountdownOptions): CountdownHookResult; +export default useCountdown; diff --git a/dist/useCountdown.js b/dist/useCountdown.js new file mode 100644 index 0000000..e1c8f47 --- /dev/null +++ b/dist/useCountdown.js @@ -0,0 +1,54 @@ +"use strict"; +var __assign = (this && this.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var react_1 = __importDefault(require("react")); +var DEFAULT_OPTIONS = { + step: 1000, +}; +function useCountdown(options) { + var _a = react_1.default.useState(), timeleft = _a[0], setTimeleft = _a[1]; + var _b = react_1.default.useState(), targetDatetime = _b[0], setTargetDatetime = _b[1]; + var _c = __assign(__assign({}, DEFAULT_OPTIONS), options), step = _c.step, onCount = _c.onCount, onEnd = _c.onEnd; + var start = function (countAsM) { + setTimeleft(countAsM); + }; + var varTimeout = null; + react_1.default.useEffect(function () { + return function () { return clearTimeout(varTimeout); }; + }, []); + react_1.default.useEffect(function () { + if (timeleft === undefined) { + return; + } + var newTimeleft = timeleft - step; + if (newTimeleft < 0) { + setTimeleft(0); + onEnd === null || onEnd === void 0 ? void 0 : onEnd(); + } + else { + onCount === null || onCount === void 0 ? void 0 : onCount(timeleft); + varTimeout = setTimeout(function () { + setTimeleft(newTimeleft); + }, step); + } + }, [timeleft]); + var result = { + timeleft: timeleft || 0, + start: start, + }; + return result; +} +exports.default = useCountdown; diff --git a/package.json b/package.json index c85c724..edf3b36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cubetiq/react-use-countdown", - "version": "1.11.0", + "version": "1.11.1", "description": "React Countdown with trigger", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/useCountdown.ts b/src/useCountdown.ts index cb94cf9..a42ad1b 100644 --- a/src/useCountdown.ts +++ b/src/useCountdown.ts @@ -30,7 +30,7 @@ function useCountdown(options?: useCountdownOptions): CountdownHookResult { const start = (countAsM: number) => { setTimeleft(countAsM); }; - let varTimeout: number | null = null; + let varTimeout: Timeout | null = null; React.useEffect(() => { return () => clearTimeout(varTimeout!);