-
Notifications
You must be signed in to change notification settings - Fork 15
/
decorators.ts
23 lines (21 loc) · 938 Bytes
/
decorators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import {debounce as db, throttle as th, ThrottleOptions} from './index'
export function throttle(wait = 0, opts: ThrottleOptions = {}): MethodDecorator {
return (proto: unknown, name: string | symbol, descriptor: PropertyDescriptor) => {
if (!descriptor || typeof descriptor.value !== 'function') {
throw new Error('debounce can only decorate functions')
}
const fn = descriptor.value
descriptor.value = th(fn, wait, opts)
Object.defineProperty(proto, name, descriptor)
}
}
export function debounce(wait = 0, opts: ThrottleOptions = {}): MethodDecorator {
return (proto: unknown, name: string | symbol, descriptor: PropertyDescriptor) => {
if (!descriptor || typeof descriptor.value !== 'function') {
throw new Error('debounce can only decorate functions')
}
const fn = descriptor.value
descriptor.value = db(fn, wait, opts)
Object.defineProperty(proto, name, descriptor)
}
}