@tmsfe/tms-core 0.0.64 → 0.0.65
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/package.json +1 -1
- package/src/funcUtils.ts +87 -0
- package/src/index-proxy.js +2 -0
- package/src/index.js +3 -0
package/package.json
CHANGED
package/src/funcUtils.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 函数节流
|
|
3
|
+
* 用途:如果func触发频繁,限制一段时间内只能执行一次
|
|
4
|
+
* @param func 执行函数
|
|
5
|
+
* @param wait 要延迟的时间,单位:毫秒
|
|
6
|
+
* @param immediate 首次调用或者超过wait时间间隔后调用时,是否先立即执行func
|
|
7
|
+
*/
|
|
8
|
+
function throttle(func: Function, wait: number, immediate = false): Function {
|
|
9
|
+
let context: object | null = null;
|
|
10
|
+
let args: IArguments | null = null;
|
|
11
|
+
let timer = 0;
|
|
12
|
+
let last = 0;
|
|
13
|
+
|
|
14
|
+
const run = (): void => {
|
|
15
|
+
func.apply(context, args);
|
|
16
|
+
timer = 0;
|
|
17
|
+
context = null;
|
|
18
|
+
args = null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return function () {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
context = this;
|
|
24
|
+
// eslint-disable-next-line
|
|
25
|
+
args = arguments;
|
|
26
|
+
if (timer) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const t = last;
|
|
30
|
+
last = Date.now();
|
|
31
|
+
if (immediate && last - t >= wait) {
|
|
32
|
+
run();
|
|
33
|
+
} else {
|
|
34
|
+
timer = setTimeout(run, wait);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 函数防抖
|
|
41
|
+
* 用途:如果func触发频繁,则在一段时间后没有再触发就执行一次
|
|
42
|
+
* @param func 执行函数
|
|
43
|
+
* @param wait 要延迟的时间,单位:毫秒
|
|
44
|
+
* @param immediate 首次调用或者超过wait时间间隔后调用时,是否先立即执行func
|
|
45
|
+
*/
|
|
46
|
+
function debounce(func: Function, wait: number, immediate = false): Function {
|
|
47
|
+
let args: IArguments | null = null;
|
|
48
|
+
let context: object | null = null;
|
|
49
|
+
let timer = 0;
|
|
50
|
+
let triggerTime = 0;
|
|
51
|
+
|
|
52
|
+
const run = (): void => {
|
|
53
|
+
const t = Date.now() - triggerTime - wait;
|
|
54
|
+
if (t < 0) {
|
|
55
|
+
timer = setTimeout(run, -t);
|
|
56
|
+
} else {
|
|
57
|
+
func.apply(context, args);
|
|
58
|
+
timer = 0;
|
|
59
|
+
args = null;
|
|
60
|
+
context = null;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return function () {
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
context = this;
|
|
67
|
+
// eslint-disable-next-line
|
|
68
|
+
args = arguments;
|
|
69
|
+
const t = triggerTime;
|
|
70
|
+
triggerTime = Date.now();
|
|
71
|
+
if (timer || (immediate && triggerTime - t < wait)) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (immediate) {
|
|
75
|
+
func.apply(context, args);
|
|
76
|
+
args = null;
|
|
77
|
+
context = null;
|
|
78
|
+
} else {
|
|
79
|
+
timer = setTimeout(run, wait);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export {
|
|
85
|
+
throttle,
|
|
86
|
+
debounce,
|
|
87
|
+
};
|
package/src/index-proxy.js
CHANGED
|
@@ -15,6 +15,7 @@ import * as stringUtils from './stringUtils';
|
|
|
15
15
|
import * as timeUtils from './timeUtils';
|
|
16
16
|
import * as ipxHelper from './ipxHelper';
|
|
17
17
|
import * as storage from './storage';
|
|
18
|
+
import * as funcUtils from './funcUtils';
|
|
18
19
|
|
|
19
20
|
let app = null;
|
|
20
21
|
let initOptions = null;
|
|
@@ -224,6 +225,7 @@ const api = {
|
|
|
224
225
|
...stringUtils,
|
|
225
226
|
...timeUtils,
|
|
226
227
|
...ipxHelper,
|
|
228
|
+
...funcUtils,
|
|
227
229
|
};
|
|
228
230
|
|
|
229
231
|
export default api;
|
package/src/index.js
CHANGED
|
@@ -44,6 +44,7 @@ import getLocInstance from './location/index';
|
|
|
44
44
|
import LocationBase from './location/base';
|
|
45
45
|
import { getMpOpenId, getOuterOpenId, getEncryptUserInfo } from './mpInfo';
|
|
46
46
|
import * as storage from './storage';
|
|
47
|
+
import { throttle, debounce } from './funcUtils';
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
50
|
* @public
|
|
@@ -195,6 +196,8 @@ const api = {
|
|
|
195
196
|
storage,
|
|
196
197
|
|
|
197
198
|
...syncApi,
|
|
199
|
+
throttle,
|
|
200
|
+
debounce,
|
|
198
201
|
};
|
|
199
202
|
|
|
200
203
|
|