useVisibleStatus.js
2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
var StatusQueue = ['measure', 'align', null, 'motion'];
var _default = function _default(visible, doMeasure) {
var _useState = (0, _react.useState)(null),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
status = _useState2[0],
setStatus = _useState2[1];
var rafRef = (0, _react.useRef)();
function cancelRaf() {
_raf.default.cancel(rafRef.current);
}
function goNextStatus(callback) {
cancelRaf();
rafRef.current = (0, _raf.default)(function () {
// Only align should be manually trigger
setStatus(function (prev) {
switch (status) {
case 'align':
return 'motion';
case 'motion':
return 'stable';
}
return prev;
});
callback === null || callback === void 0 ? void 0 : callback();
});
} // Init status
(0, _react.useEffect)(function () {
setStatus('measure');
}, [visible]); // Go next status
(0, _react.useEffect)(function () {
switch (status) {
case 'measure':
doMeasure();
break;
}
if (status) {
rafRef.current = (0, _raf.default)( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
var index, nextStatus;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
index = StatusQueue.indexOf(status);
nextStatus = StatusQueue[index + 1];
if (nextStatus && index !== -1) {
setStatus(nextStatus);
}
case 3:
case "end":
return _context.stop();
}
}
}, _callee);
})));
}
}, [status]);
(0, _react.useEffect)(function () {
return function () {
cancelRaf();
};
}, []);
return [status, goNextStatus];
};
exports.default = _default;