UNPKG

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