Last active
May 3, 2023 20:46
-
-
Save otonielguajardo/430739a07bec5ba763bf2dcf913cbc2f to your computer and use it in GitHub Desktop.
Angular RxJS Debounce Decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Subject } from 'rxjs'; | |
import { debounceTime } from 'rxjs/operators'; | |
export function Debounce(time: number) { | |
return function ( | |
target: any, | |
propertyKey: string, | |
descriptor: PropertyDescriptor | |
) { | |
const originalMethod = descriptor.value; | |
const subject = new Subject<any[]>(); | |
descriptor.value = function (...args: any[]) { | |
subject.next(args); | |
}; | |
subject | |
.pipe(debounceTime(time)) | |
.subscribe((args: any[]) => originalMethod.apply(target, args)); | |
return descriptor; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment