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 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
- - [`Tick`](./src/utils/tick.ts):嘀嗒器,每帧都会执行
14
+ - [`tick`](./src/utils/tick.ts):嘀嗒器,每帧都会执行
15
15
  - [`uniqKey`](./src/utils/uniqKey.ts):生成一个全局唯一的 key
16
16
  - [`waitUntil`](./src/utils/wait.ts):执行某种检测,直接条件为真或者超时才返回
17
17
 
@@ -1,5 +1,5 @@
1
- import { Tick } from "../utils/tick";
2
- import { useEffect, useRef } from "react";
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 tick = new Tick(() => framer.current());
13
- tick.start();
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 { Tick } from './utils/tick';
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 { Tick } from './utils/tick';
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';
@@ -1,4 +1,4 @@
1
- export type CountdownValueIndex = "d" | "h" | "i" | "s";
1
+ export type CountdownValueIndex = 'd' | 'h' | 'i' | 's';
2
2
  export type CountdownValue = {
3
3
  [key in CountdownValueIndex]?: number;
4
4
  };
@@ -1,4 +1,4 @@
1
- import { Tick } from "./tick";
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 = ["d", "h", "i", "s"];
17
- if (typeof option.remain === "number" && option.remain >= 0) {
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 === "string") {
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 = ["h", "i", "s"];
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
- const tickInstance = new Tick(() => {
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 "d":
92
+ case 'd':
95
93
  result.d = Math.floor(remainTime / 86400);
96
94
  remainTime = remainTime - result.d * 86400;
97
95
  break;
98
- case "h":
96
+ case 'h':
99
97
  result.h = Math.floor(remainTime / 3600);
100
98
  remainTime = remainTime - result.h * 3600;
101
99
  break;
102
- case "i":
100
+ case 'i':
103
101
  result.i = Math.floor(remainTime / 60);
104
102
  remainTime = remainTime - result.i * 60;
105
103
  break;
106
- case "s":
104
+ case 's':
107
105
  result.s = remainTime;
108
106
  break;
109
107
  default:
@@ -1,13 +1,7 @@
1
+ export type StopTick = () => void;
1
2
  /**
2
- * tick类,初始化后使用start开始tick,使用destroy终止tick
3
- * @constructor
4
- * @param {function} callback start后每一帧都会调用的回调函数
3
+ * 逐帧执行的工具函数,返回一个方法,调用该方法,停止执行
4
+ * @param callback
5
+ * @param interval
5
6
  */
6
- export declare class Tick {
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;
@@ -1,32 +1,47 @@
1
1
  /**
2
- * tick类,初始化后使用start开始tick,使用destroy终止tick
3
- * @constructor
4
- * @param {function} callback start后每一帧都会调用的回调函数
2
+ * 逐帧执行的工具函数,返回一个方法,调用该方法,停止执行
3
+ * @param callback
4
+ * @param interval
5
5
  */
6
- export class Tick {
7
- constructor(callback) {
8
- this.callback = callback;
9
- this.frameId = undefined;
10
- this.isRun = true;
11
- }
12
- start() {
13
- // 每一帧执行的任务
14
- const frame = () => {
15
- var _a;
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
- this.frameId = requestAnimationFrame(frame);
20
- // 执行回调
21
- (_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this);
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
- destroy() {
27
- if (typeof this.frameId === "number") {
28
- cancelAnimationFrame(this.frameId);
29
- }
30
- this.isRun = false;
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
  }
@@ -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 { Tick } from "./tick";
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 !== "function") {
24
+ if (typeof condition !== 'function') {
25
25
  return !!condition;
26
26
  }
27
27
  // 设置默认检测时间的最大值,如果没有设置,则一直检测
28
- if (!maxTime || typeof maxTime !== "number") {
28
+ if (!maxTime || typeof maxTime !== 'number') {
29
29
  maxTime = Infinity;
30
30
  }
31
31
  return new Promise((resolve) => {
32
- const tick = new Tick(() => {
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
- tick.destroy();
37
+ stop();
38
38
  resolve(false);
39
39
  return;
40
40
  }
41
41
  // 处理结果
42
42
  const handle = (res) => {
43
43
  if (res) {
44
- tick.destroy();
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clxx",
3
- "version": "2.0.9",
3
+ "version": "2.1.2",
4
4
  "description": "Basic JS library for mobile devices",
5
5
  "main": "./build/index.js",
6
6
  "module": "./build/index.js",