Created
October 8, 2019 11:19
-
-
Save mohamedelshorbagy/60ab7e943b0ee200e5719be253306e00 to your computer and use it in GitHub Desktop.
Debounce Event using EventManagerPlugin
This file contains hidden or 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 { Component } from '@angular/core'; | |
@Component({ | |
selector: 'app-root', | |
template: ` | |
<div class="ui container"> | |
<hr> | |
<div class="ui input"> | |
<input type="text" (input.debounce.1000)="search($event)"> | |
</div> | |
<p>{{ inputSearch }}</p> | |
</div> | |
`, | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
inputSearch: string; | |
search(event: InputEvent) { | |
this.inputSearch = (<any>event.target).value; | |
} | |
} |
This file contains hidden or 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 { BrowserModule, EVENT_MANAGER_PLUGINS } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { AppComponent } from './app.component'; | |
import { DebouceEvent } from './debounce.event'; | |
@NgModule({ | |
declarations: [ | |
AppComponent | |
], | |
imports: [ | |
BrowserModule | |
], | |
providers: [ | |
{ | |
multi: true, | |
provide: EVENT_MANAGER_PLUGINS, | |
useClass: DebouceEvent | |
} | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
This file contains hidden or 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 { EventManager } from '@angular/platform-browser'; | |
// Event Manager in Angular Source Code | |
//https://github.com/angular/angular/blob/master/packages/platform-browser/src/dom/events/event_manager.ts | |
// Event Manager in Angular Docs. | |
// https://angular.io/api/platform-browser/EventManager | |
// https://bestofjs.org/projects/30-seconds-of-code | |
function debounce(fn, ms = 0) { | |
let timeoutId; | |
return function (...args) { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => fn.apply(this, args), ms); | |
}; | |
} | |
export class DebouceEvent { | |
manager: EventManager; | |
supports(eventName: string): boolean { | |
return /debounce/.test(eventName); | |
} | |
addEventListener( | |
element: HTMLElement, | |
eventName: string, | |
handler: Function | |
) { | |
// input.debounce.1000 | |
// ['input', 'debounce', '10000'] | |
// I'm interested in first and third elements | |
let [domEventName, , time = 300] = eventName.split('.'); | |
const guardedHandler = event => this.manager.getZone().runGuarded(() => handler(event)); | |
let debounceHandler = debounce(guardedHandler, +time); | |
// Register Event in ELement | |
this.manager.getZone().runOutsideAngular(() => { | |
element.addEventListener(domEventName, debounceHandler); | |
}) | |
// disposal function | |
// angular calls it when removes the element | |
// For avoiding memory leaks | |
return () => { | |
element.removeEventListener(domEventName, debounceHandler); | |
debounceHandler = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment