-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdebounceNext.js
executable file
·52 lines (46 loc) · 1.47 KB
/
debounceNext.js
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
/**
* @description 防抖动:(decorator)可装饰类内箭头函数
* @param {object} params - 配置
* @param {number} params.delay - 时间阀值(单位:ms),默认:delay=300
* @param {bool} params.immediate - 初始是否立刻执行,默认:immediate=false
* @returns {function} - 返回装饰器方法
*/
export const debounceNext = (params = {}) => {
// reference:http://es6.ruanyifeng.com/#docs/decorator#%E6%96%B9%E6%B3%95%E7%9A%84%E4%BF%AE%E9%A5%B0
return function (target, name, descriptor) {
let timer = null;
const { delay = 300, immediate = false } = params;
// high order function
if (!descriptor || (arguments.length === 1 && typeof target === 'function')) {
return createDebounce(target);
}
function createDebounce(fn) {
return function debounce() {
const [argumentsCopy, that] = [arguments, this];
if (immediate && !timer) {
fn.apply(this, arguments);
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
if (!immediate) {
fn.apply(that, argumentsCopy);
}
timer = null;
}, delay);
};
}
// 修饰类内的箭头函数
if (descriptor.initializer) {
return {
enumerable: false,
configurable: true,
get: function () {
return createDebounce(descriptor.initializer.call(this));
}
};
}
return descriptor;
};
};