parsec-hooks 1.0.16 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,10 @@
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: () => boolean;
5
+ clearCountdownTimer: () => void;
4
6
  stopCountdownTimer: () => void;
5
- setCountdown: (s: number) => Promise<unknown>;
7
+ setCountdown: (s: number, onChange?: (count: number) => void) => Promise<unknown>;
6
8
  countdownText: string;
7
9
  };
8
10
  export default _default;
@@ -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,60 +13,85 @@ var _react = require("react");
13
13
 
14
14
  var _refStateHooks = _interopRequireDefault(require("../refStateHooks"));
15
15
 
16
+ var _requestAnimationFramePolyfill = require("request-animation-frame-polyfill");
17
+
18
+ var stops = {};
19
+
20
+ var setTimeInterval = function setTimeInterval(fn, ms) {
21
+ var id = Math.random();
22
+ var startTime = +new Date();
23
+
24
+ var start = function start(prev) {
25
+ if (stops[id] === false) {
26
+ return;
27
+ }
28
+
29
+ var now = +new Date();
30
+
31
+ if (now - prev > ms) {
32
+ fn();
33
+ (0, _requestAnimationFramePolyfill.requestAnimationFrame)(function () {
34
+ return start(now);
35
+ });
36
+ } else {
37
+ (0, _requestAnimationFramePolyfill.requestAnimationFrame)(function () {
38
+ return start(prev);
39
+ });
40
+ }
41
+ };
42
+
43
+ (0, _requestAnimationFramePolyfill.requestAnimationFrame)(function () {
44
+ return start(startTime);
45
+ });
46
+ return id;
47
+ };
48
+
49
+ exports.setTimeInterval = setTimeInterval;
50
+
51
+ var clearTimeInterval = function clearTimeInterval(id) {
52
+ stops[id] = false;
53
+ };
54
+
55
+ exports.clearTimeInterval = clearTimeInterval;
56
+
16
57
  var _default = function _default() {
17
58
  var _useRefState = (0, _refStateHooks.default)(0),
18
59
  _useRefState2 = (0, _slicedToArray2.default)(_useRefState, 2),
19
60
  countdown = _useRefState2[0],
20
61
  setCountdown = _useRefState2[1];
21
62
 
22
- var stopRef = (0, _react.useRef)(false);
63
+ var timer = (0, _react.useRef)();
23
64
  var clearCountdownTimer = (0, _react.useCallback)(function () {
24
- return stopRef.current = true;
65
+ return timer.current && clearTimeInterval(timer.current);
25
66
  }, []);
26
67
  var stopCountdownTimer = (0, _react.useCallback)(function () {
27
68
  clearCountdownTimer();
28
69
  setCountdown(0);
29
70
  }, [clearCountdownTimer, setCountdown]);
71
+ (0, _react.useEffect)(function () {
72
+ return clearCountdownTimer;
73
+ }, [clearCountdownTimer]);
30
74
  return {
31
75
  countdown: countdown,
32
76
  clearCountdownTimer: clearCountdownTimer,
33
77
  stopCountdownTimer: stopCountdownTimer,
34
- setCountdown: (0, _react.useCallback)(function (s) {
78
+ setCountdown: (0, _react.useCallback)(function (s, onChange) {
35
79
  return new Promise(function (resolve) {
36
- stopRef.current = false;
37
- setCountdown(s);
38
- var startTime = +new Date();
80
+ clearCountdownTimer();
81
+ var countdown = s;
82
+ setCountdown(countdown);
83
+ timer.current = setTimeInterval(function () {
84
+ countdown--;
85
+ onChange === null || onChange === void 0 ? void 0 : onChange(countdown);
86
+ setCountdown(countdown);
39
87
 
40
- var start = function start(prev) {
41
- var now = +new Date();
42
-
43
- if (stopRef.current) {
44
- return;
45
- }
46
-
47
- if (now - prev > 1000) {
48
- var count = s - Math.round((now - startTime) / 1000);
49
- setCountdown(count);
50
-
51
- if (count <= 0) {
52
- resolve();
53
- } else {
54
- requestAnimationFrame(function () {
55
- return start(now);
56
- });
57
- }
58
- } else {
59
- requestAnimationFrame(function () {
60
- return start(prev);
61
- });
88
+ if (!countdown) {
89
+ clearCountdownTimer();
90
+ resolve();
62
91
  }
63
- };
64
-
65
- requestAnimationFrame(function () {
66
- return start(startTime);
67
- });
92
+ }, 1000);
68
93
  });
69
- }, [setCountdown]),
94
+ }, [clearCountdownTimer, setCountdown]),
70
95
  countdownText: countdown ? "(".concat(countdown, " s)") : ''
71
96
  };
72
97
  };
@@ -70,6 +70,7 @@ function _default(getListFn, _ref) {
70
70
  var preVisible = (0, _previousHooks.default)(loadMoreVisible);
71
71
 
72
72
  var _usePromise = (0, _promiseHooks.default)((0, _react.useCallback)(function (retainList) {
73
+ console.log(2222, dataRef.current);
73
74
  var _dataRef$current = dataRef.current,
74
75
  page = _dataRef$current.page,
75
76
  limit = _dataRef$current.limit,
@@ -88,6 +89,14 @@ function _default(getListFn, _ref) {
88
89
  if (count === countRef.current) {
89
90
  var list = pageNum === defaultPage || retainList ? newList : [].concat((0, _toConsumableArray2.default)(resList), (0, _toConsumableArray2.default)(newList));
90
91
  setIsEnd(list.length >= total);
92
+ console.log(1111, {
93
+ 'list.length': list.length,
94
+ defaultLimit: defaultLimit
95
+ }, {
96
+ page: Math.ceil(list.length < defaultLimit ? 1 : list.length / defaultLimit + 1),
97
+ limit: defaultLimit,
98
+ list: list
99
+ });
91
100
  setData({
92
101
  page: Math.ceil(list.length < defaultLimit ? 1 : list.length / defaultLimit + 1),
93
102
  limit: defaultLimit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "parsec-hooks",
3
- "version": "1.0.16",
3
+ "version": "1.0.19",
4
4
  "description": "秒差距前端常用的hooks工具",
5
5
  "author": "mushan0x0",
6
6
  "license": "MIT",
@@ -39,6 +39,7 @@
39
39
  "import-cdn-js": "^0.0.2",
40
40
  "intersection-observer": "^0.8.0",
41
41
  "qs": "^6.9.4",
42
+ "request-animation-frame-polyfill": "^1.1.2",
42
43
  "resize-observer-polyfill": "^1.5.1",
43
44
  "unstated-next": "^1.1.0"
44
45
  },