Debounce yapisi ile cok sik yapilan requestleri birlestirmek ve tek bir seferde
typescript, programming, debounce, project design, project pattern
Code
export class Debouncer<T> {
private requested = false
private onInittedCallbacks: ((data: T) => void)[] = []
constructor(
private readonly initData: () => Promise<T>,
private readonly debouncePeriod: number
) {}
request(onInitCallback?: (data: T) => void) {
if (onInitCallback) this.onInittedCallbacks.push(onInitCallback)
if (!this.requested) {
this.requested = true
setTimeout(async () => {
this.requested = false
await this.execute()
}, this.debouncePeriod * 1000)
}
}
private async execute() {
const data = await this.initData()
this.onInittedCallbacks.forEach(callback => callback(data))
this.onInittedCallbacks = []
}
}Debounce Nasıl Çalışır?
Kullanım Alanları
PreviousConverting Instagram saved_collections.json file to csv file and importing it tNextDeleting files, completely, from git history
Last updated
Was this helpful?