-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (53 loc) · 1.46 KB
/
index.js
File metadata and controls
53 lines (53 loc) · 1.46 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
var react_1 = require("react");
// fork from koa-compose
function compose(middleware) {
/**
* @param {Object} context
* @return {Promise}
* @api public
*/
return function (context, next) {
// last called middleware #
var index = -1;
function dispatch(i) {
if (i <= index)
return;
index = i;
var fn = middleware[i];
if (i === middleware.length)
fn = next;
if (!fn)
return;
try {
return fn(context, dispatch.bind(null, i + 1));
}
catch (err) {
return;
}
}
return dispatch(0);
};
}
function useTimeLineTask(middleware, delay) {
var savedCallback = react_1.useRef();
// 保存新回调
react_1.useEffect(function () {
savedCallback.current = compose(middleware);
});
// 建立 interval
react_1.useEffect(function () {
function tick() {
savedCallback.current({});
}
if (delay !== null) {
var id_1 = setInterval(tick, delay);
return function () {
clearInterval(id_1);
};
}
}, [delay]);
}
exports.default = useTimeLineTask;