Skip to content

Instantly share code, notes, and snippets.

@otonielguajardo
Last active May 3, 2023 20:46
Show Gist options
  • Save otonielguajardo/430739a07bec5ba763bf2dcf913cbc2f to your computer and use it in GitHub Desktop.
Save otonielguajardo/430739a07bec5ba763bf2dcf913cbc2f to your computer and use it in GitHub Desktop.
Angular RxJS Debounce Decorator
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