/**
* 接口try 装饰器
* @param loading 是否有loading效果
* @param cb 异常处理函数d 函数名
* @constructor
*/
export function Try(loading?: boolean, cb?: string) {
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function (...args) {
try {
if (loading) {
Tips.showLoading();
}
return await method.call(this, ...args);
} catch (e) {
if (cb && target[cb]) {
target[cb](e);
} else {
Ehr.handleError(e);
}
} finally {
if (loading) {
Tips.hideLoading();
}
}
};
};
}
/**
* 节流装饰器
* @param delay
* @constructor
*/
export function Throttle(delay = 300) {
let previous = 0;
return function (target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function (...args) {
const now = Date.now();
if (now - previous > delay) {
previous = now;
return method.call(this, ...args);
}
};
};
}
/**
* 防抖装饰器
* @param delay
* @constructor
*/
export const Debounce = (delay: number = 200) => {
let timer: any = null;
return function (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = function (...args) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
return method.call(this, ...args);
}, delay);
};
};
};
@Debounce()
@Try()
async login() {
if (this.isValid) return;
await LoginService.login();
await Service.becomeVip(this.userId);
YNavigator.redirectTo("/shop/pages/shopIndex/ShopIndexPage");
}
js装饰器
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...