parsec-hooks 1.0.17 → 1.0.18
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
|
|
1
|
+
export declare const setTimeInterval: (fn: () => void, ms: number) => number;
|
2
|
+
export declare const clearTimeInterval: (id: number) => void;
|
1
3
|
declare const _default: () => {
|
2
4
|
countdown: number;
|
3
|
-
clearCountdownTimer: () =>
|
5
|
+
clearCountdownTimer: () => void;
|
4
6
|
stopCountdownTimer: () => void;
|
5
7
|
setCountdown: (s: number, onChange?: (count: number) => void) => Promise<unknown>;
|
6
8
|
countdownText: string;
|
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
6
6
|
value: true
|
7
7
|
});
|
8
|
-
exports.default = void 0;
|
8
|
+
exports.default = exports.clearTimeInterval = exports.setTimeInterval = void 0;
|
9
9
|
|
10
10
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
11
11
|
|
@@ -13,61 +13,83 @@ var _react = require("react");
|
|
13
13
|
|
14
14
|
var _refStateHooks = _interopRequireDefault(require("../refStateHooks"));
|
15
15
|
|
16
|
+
var stops = {};
|
17
|
+
|
18
|
+
var setTimeInterval = function setTimeInterval(fn, ms) {
|
19
|
+
var id = Math.random();
|
20
|
+
var startTime = +new Date();
|
21
|
+
|
22
|
+
var start = function start(prev) {
|
23
|
+
if (stops[id] === false) {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
|
27
|
+
var now = +new Date();
|
28
|
+
|
29
|
+
if (now - prev > ms) {
|
30
|
+
fn();
|
31
|
+
requestAnimationFrame(function () {
|
32
|
+
return start(now);
|
33
|
+
});
|
34
|
+
} else {
|
35
|
+
requestAnimationFrame(function () {
|
36
|
+
return start(prev);
|
37
|
+
});
|
38
|
+
}
|
39
|
+
};
|
40
|
+
|
41
|
+
requestAnimationFrame(function () {
|
42
|
+
return start(startTime);
|
43
|
+
});
|
44
|
+
return id;
|
45
|
+
};
|
46
|
+
|
47
|
+
exports.setTimeInterval = setTimeInterval;
|
48
|
+
|
49
|
+
var clearTimeInterval = function clearTimeInterval(id) {
|
50
|
+
stops[id] = false;
|
51
|
+
};
|
52
|
+
|
53
|
+
exports.clearTimeInterval = clearTimeInterval;
|
54
|
+
|
16
55
|
var _default = function _default() {
|
17
56
|
var _useRefState = (0, _refStateHooks.default)(0),
|
18
57
|
_useRefState2 = (0, _slicedToArray2.default)(_useRefState, 2),
|
19
58
|
countdown = _useRefState2[0],
|
20
59
|
setCountdown = _useRefState2[1];
|
21
60
|
|
22
|
-
var
|
61
|
+
var timer = (0, _react.useRef)();
|
23
62
|
var clearCountdownTimer = (0, _react.useCallback)(function () {
|
24
|
-
return
|
63
|
+
return timer.current && clearTimeInterval(timer.current);
|
25
64
|
}, []);
|
26
65
|
var stopCountdownTimer = (0, _react.useCallback)(function () {
|
27
66
|
clearCountdownTimer();
|
28
67
|
setCountdown(0);
|
29
68
|
}, [clearCountdownTimer, setCountdown]);
|
69
|
+
(0, _react.useEffect)(function () {
|
70
|
+
return clearCountdownTimer;
|
71
|
+
}, [clearCountdownTimer]);
|
30
72
|
return {
|
31
73
|
countdown: countdown,
|
32
74
|
clearCountdownTimer: clearCountdownTimer,
|
33
75
|
stopCountdownTimer: stopCountdownTimer,
|
34
76
|
setCountdown: (0, _react.useCallback)(function (s, onChange) {
|
35
77
|
return new Promise(function (resolve) {
|
36
|
-
|
37
|
-
|
38
|
-
|
78
|
+
clearCountdownTimer();
|
79
|
+
var countdown = s;
|
80
|
+
setCountdown(countdown);
|
81
|
+
timer.current = setTimeInterval(function () {
|
82
|
+
countdown--;
|
83
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(countdown);
|
84
|
+
setCountdown(countdown);
|
39
85
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
if (stopRef.current) {
|
44
|
-
return;
|
45
|
-
}
|
46
|
-
|
47
|
-
if (now - prev > 1000) {
|
48
|
-
var count = s - Math.round((now - startTime) / 1000);
|
49
|
-
onChange === null || onChange === void 0 ? void 0 : onChange(count);
|
50
|
-
setCountdown(count);
|
51
|
-
|
52
|
-
if (count <= 0) {
|
53
|
-
resolve();
|
54
|
-
} else {
|
55
|
-
requestAnimationFrame(function () {
|
56
|
-
return start(now);
|
57
|
-
});
|
58
|
-
}
|
59
|
-
} else {
|
60
|
-
requestAnimationFrame(function () {
|
61
|
-
return start(prev);
|
62
|
-
});
|
86
|
+
if (!countdown) {
|
87
|
+
clearCountdownTimer();
|
88
|
+
resolve();
|
63
89
|
}
|
64
|
-
};
|
65
|
-
|
66
|
-
requestAnimationFrame(function () {
|
67
|
-
return start(startTime);
|
68
|
-
});
|
90
|
+
}, 1000);
|
69
91
|
});
|
70
|
-
}, [setCountdown]),
|
92
|
+
}, [clearCountdownTimer, setCountdown]),
|
71
93
|
countdownText: countdown ? "(".concat(countdown, " s)") : ''
|
72
94
|
};
|
73
95
|
};
|