clxx 2.0.9 → 2.1.2
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.
- package/README.md +1 -1
- package/build/Effect/useTick.js +4 -5
- package/build/index.d.ts +2 -1
- package/build/index.js +2 -1
- package/build/utils/Countdown.d.ts +1 -1
- package/build/utils/Countdown.js +11 -13
- package/build/utils/tick.d.ts +5 -11
- package/build/utils/tick.js +40 -25
- package/build/utils/wait.js +6 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
- [`is`](./src/utils/is.ts):一些简单的环境判断
|
|
12
12
|
- [`jsonp`](./src/utils/jsonp.ts):发送一个 jsonp 请求
|
|
13
13
|
- [`GET,POST`](./src/utils/request.ts):ajax 请求的简单封装
|
|
14
|
-
- [`
|
|
14
|
+
- [`tick`](./src/utils/tick.ts):嘀嗒器,每帧都会执行
|
|
15
15
|
- [`uniqKey`](./src/utils/uniqKey.ts):生成一个全局唯一的 key
|
|
16
16
|
- [`waitUntil`](./src/utils/wait.ts):执行某种检测,直接条件为真或者超时才返回
|
|
17
17
|
|
package/build/Effect/useTick.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useEffect, useRef } from
|
|
1
|
+
import { tick } from '../utils/tick';
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
3
3
|
/**
|
|
4
4
|
* 逐帧执行的ticker
|
|
5
5
|
* @param frame 帧函数
|
|
@@ -9,8 +9,7 @@ export function useTick(frame) {
|
|
|
9
9
|
const framer = useRef(frame);
|
|
10
10
|
framer.current = frame;
|
|
11
11
|
useEffect(() => {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
return () => tick.destroy();
|
|
12
|
+
const stop = tick(() => framer.current());
|
|
13
|
+
return () => stop();
|
|
15
14
|
}, []);
|
|
16
15
|
}
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { tick } from './utils/tick';
|
|
2
2
|
export { setContextValue, getContextValue } from './context';
|
|
3
3
|
export { jsonp } from './utils/jsonp';
|
|
4
4
|
export { uniqKey } from './utils/uniqKey';
|
|
@@ -11,6 +11,7 @@ export { is } from './utils/is';
|
|
|
11
11
|
export { waitFor, waitUntil } from './utils/wait';
|
|
12
12
|
export { normalizeUnit, splitValue, adaptive } from './utils/cssUtil';
|
|
13
13
|
export { createApp, history, getHistory } from './utils/createApp';
|
|
14
|
+
export { createPortalDOM } from './utils/dom';
|
|
14
15
|
export { useInterval } from './Effect/useInterval';
|
|
15
16
|
export { useTick } from './Effect/useTick';
|
|
16
17
|
export { Ago } from './Ago';
|
package/build/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { tick } from './utils/tick';
|
|
2
2
|
export { setContextValue, getContextValue } from './context';
|
|
3
3
|
export { jsonp } from './utils/jsonp';
|
|
4
4
|
export { uniqKey } from './utils/uniqKey';
|
|
@@ -11,6 +11,7 @@ export { is } from './utils/is';
|
|
|
11
11
|
export { waitFor, waitUntil } from './utils/wait';
|
|
12
12
|
export { normalizeUnit, splitValue, adaptive } from './utils/cssUtil';
|
|
13
13
|
export { createApp, history, getHistory } from './utils/createApp';
|
|
14
|
+
export { createPortalDOM } from './utils/dom';
|
|
14
15
|
export { useInterval } from './Effect/useInterval';
|
|
15
16
|
export { useTick } from './Effect/useTick';
|
|
16
17
|
export { Ago } from './Ago';
|
package/build/utils/Countdown.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { tick } from './tick';
|
|
2
2
|
export class Countdown {
|
|
3
3
|
constructor(option) {
|
|
4
4
|
/**
|
|
@@ -13,13 +13,13 @@ export class Countdown {
|
|
|
13
13
|
* i:分
|
|
14
14
|
* s:秒
|
|
15
15
|
*/
|
|
16
|
-
this.format = [
|
|
17
|
-
if (typeof option.remain ===
|
|
16
|
+
this.format = ['d', 'h', 'i', 's'];
|
|
17
|
+
if (typeof option.remain === 'number' && option.remain >= 0) {
|
|
18
18
|
this.total = this.remain = option.remain;
|
|
19
19
|
}
|
|
20
20
|
// 倒计时需要展示的时间格式
|
|
21
|
-
if (typeof option.format ===
|
|
22
|
-
const parts = option.format.split(
|
|
21
|
+
if (typeof option.format === 'string') {
|
|
22
|
+
const parts = option.format.split('');
|
|
23
23
|
const output = [];
|
|
24
24
|
this.format.forEach((item) => {
|
|
25
25
|
if (parts.includes(item)) {
|
|
@@ -30,7 +30,7 @@ export class Countdown {
|
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
32
32
|
// 设置默认的倒计时格式
|
|
33
|
-
this.format = [
|
|
33
|
+
this.format = ['h', 'i', 's'];
|
|
34
34
|
}
|
|
35
35
|
this._onUpdate = option.onUpdate;
|
|
36
36
|
this._onEnd = option.onEnd;
|
|
@@ -54,7 +54,7 @@ export class Countdown {
|
|
|
54
54
|
(_d = this._onUpdate) === null || _d === void 0 ? void 0 : _d.call(this, this.formatValue());
|
|
55
55
|
// 记录倒计时开启时的时间
|
|
56
56
|
const start = Date.now();
|
|
57
|
-
|
|
57
|
+
this._stopTick = tick(() => {
|
|
58
58
|
var _a, _b, _c, _d;
|
|
59
59
|
// 获取倒计时已经持续的时间
|
|
60
60
|
const duration = Math.floor((Date.now() - start) / 1000);
|
|
@@ -73,8 +73,6 @@ export class Countdown {
|
|
|
73
73
|
(_d = this._onUpdate) === null || _d === void 0 ? void 0 : _d.call(this, this.formatValue());
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
|
-
tickInstance.start();
|
|
77
|
-
this._stopTick = () => tickInstance.destroy();
|
|
78
76
|
}
|
|
79
77
|
// 停止倒计时
|
|
80
78
|
stop() {
|
|
@@ -91,19 +89,19 @@ export class Countdown {
|
|
|
91
89
|
const result = {};
|
|
92
90
|
this.format.forEach((key) => {
|
|
93
91
|
switch (key) {
|
|
94
|
-
case
|
|
92
|
+
case 'd':
|
|
95
93
|
result.d = Math.floor(remainTime / 86400);
|
|
96
94
|
remainTime = remainTime - result.d * 86400;
|
|
97
95
|
break;
|
|
98
|
-
case
|
|
96
|
+
case 'h':
|
|
99
97
|
result.h = Math.floor(remainTime / 3600);
|
|
100
98
|
remainTime = remainTime - result.h * 3600;
|
|
101
99
|
break;
|
|
102
|
-
case
|
|
100
|
+
case 'i':
|
|
103
101
|
result.i = Math.floor(remainTime / 60);
|
|
104
102
|
remainTime = remainTime - result.i * 60;
|
|
105
103
|
break;
|
|
106
|
-
case
|
|
104
|
+
case 's':
|
|
107
105
|
result.s = remainTime;
|
|
108
106
|
break;
|
|
109
107
|
default:
|
package/build/utils/tick.d.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
+
export type StopTick = () => void;
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @
|
|
4
|
-
* @param
|
|
3
|
+
* 逐帧执行的工具函数,返回一个方法,调用该方法,停止执行
|
|
4
|
+
* @param callback
|
|
5
|
+
* @param interval
|
|
5
6
|
*/
|
|
6
|
-
export declare
|
|
7
|
-
callback?: (() => void) | undefined;
|
|
8
|
-
frameId: number | undefined;
|
|
9
|
-
isRun: boolean;
|
|
10
|
-
constructor(callback?: (() => void) | undefined);
|
|
11
|
-
start(): void;
|
|
12
|
-
destroy(): void;
|
|
13
|
-
}
|
|
7
|
+
export declare function tick(callback: () => void, interval?: number): StopTick;
|
package/build/utils/tick.js
CHANGED
|
@@ -1,32 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @
|
|
4
|
-
* @param
|
|
2
|
+
* 逐帧执行的工具函数,返回一个方法,调用该方法,停止执行
|
|
3
|
+
* @param callback
|
|
4
|
+
* @param interval
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (!this.isRun)
|
|
6
|
+
export function tick(callback, interval) {
|
|
7
|
+
// 执行状态,是否正在执行
|
|
8
|
+
let isRunning;
|
|
9
|
+
let frame;
|
|
10
|
+
let frameId;
|
|
11
|
+
// 设置了tick的间隔
|
|
12
|
+
if (interval && typeof interval === 'number') {
|
|
13
|
+
let lastTick = Date.now();
|
|
14
|
+
frame = () => {
|
|
15
|
+
if (!isRunning) {
|
|
17
16
|
return;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
}
|
|
18
|
+
frameId = requestAnimationFrame(frame);
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
// 每次间隔频率逻辑上保持一致,即使帧频不一致
|
|
21
|
+
if (now - lastTick >= interval) {
|
|
22
|
+
// 本次tick的时间为上次的时间加上频率间隔
|
|
23
|
+
lastTick = lastTick + interval;
|
|
24
|
+
callback();
|
|
25
|
+
}
|
|
22
26
|
};
|
|
23
|
-
// 执行起始帧
|
|
24
|
-
frame();
|
|
25
27
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
// 没有设置tick的间隔
|
|
29
|
+
else {
|
|
30
|
+
frame = () => {
|
|
31
|
+
if (!isRunning) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
frameId = requestAnimationFrame(frame);
|
|
35
|
+
// 没有设置interval时,每帧都执行
|
|
36
|
+
callback();
|
|
37
|
+
};
|
|
31
38
|
}
|
|
39
|
+
// 开始执行
|
|
40
|
+
isRunning = true;
|
|
41
|
+
frameId = requestAnimationFrame(frame);
|
|
42
|
+
// 返回一个可以立即停止的函数
|
|
43
|
+
return () => {
|
|
44
|
+
isRunning = false;
|
|
45
|
+
cancelAnimationFrame(frameId);
|
|
46
|
+
};
|
|
32
47
|
}
|
package/build/utils/wait.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
10
|
+
import { tick } from './tick';
|
|
11
11
|
/**
|
|
12
12
|
* 直接条件为真或者超时才返回结果
|
|
13
13
|
*
|
|
@@ -21,27 +21,27 @@ export function waitUntil(condition, maxTime) {
|
|
|
21
21
|
// 记录检测开始时间
|
|
22
22
|
const start = Date.now();
|
|
23
23
|
// 如果检测条件不为函数,直接返回结果
|
|
24
|
-
if (typeof condition !==
|
|
24
|
+
if (typeof condition !== 'function') {
|
|
25
25
|
return !!condition;
|
|
26
26
|
}
|
|
27
27
|
// 设置默认检测时间的最大值,如果没有设置,则一直检测
|
|
28
|
-
if (!maxTime || typeof maxTime !==
|
|
28
|
+
if (!maxTime || typeof maxTime !== 'number') {
|
|
29
29
|
maxTime = Infinity;
|
|
30
30
|
}
|
|
31
31
|
return new Promise((resolve) => {
|
|
32
|
-
const
|
|
32
|
+
const stop = tick(() => {
|
|
33
33
|
const now = Date.now();
|
|
34
34
|
const result = condition();
|
|
35
35
|
// 超时返回false
|
|
36
36
|
if (now - start >= maxTime) {
|
|
37
|
-
|
|
37
|
+
stop();
|
|
38
38
|
resolve(false);
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
41
|
// 处理结果
|
|
42
42
|
const handle = (res) => {
|
|
43
43
|
if (res) {
|
|
44
|
-
|
|
44
|
+
stop();
|
|
45
45
|
resolve(true);
|
|
46
46
|
}
|
|
47
47
|
};
|
|
@@ -53,7 +53,6 @@ export function waitUntil(condition, maxTime) {
|
|
|
53
53
|
// 普通一般的结果
|
|
54
54
|
handle(result);
|
|
55
55
|
});
|
|
56
|
-
tick.start();
|
|
57
56
|
});
|
|
58
57
|
});
|
|
59
58
|
}
|