@@ -10,8 +10,9 @@ import {
1010 AfterContentInit ,
1111 Injector ,
1212} from '@angular/core' ;
13-
14- import { debounce } from '../util/debounce' ;
13+ import { Observable } from 'rxjs/Observable' ;
14+ import { Subject } from 'rxjs/Subject' ;
15+ import 'rxjs/add/operator/debounceTime' ;
1516
1617/**
1718 * Directive that triggers a callback whenever the content of
@@ -29,30 +30,21 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
2930 /** Event emitted for each change in the element's content. */
3031 @Output ( 'cdkObserveContent' ) event = new EventEmitter < MutationRecord [ ] > ( ) ;
3132
33+ /** Used for debouncing the emitted values to the observeContent event. */
34+ private _debouncer = new Subject < MutationRecord [ ] > ( ) ;
35+
3236 /** Debounce interval for emitting the changes. */
3337 @Input ( ) debounce : number ;
3438
3539 constructor ( private _elementRef : ElementRef , private _injector : Injector ) { }
3640
3741 ngAfterContentInit ( ) {
38- let callback : MutationCallback ;
39-
40- // If a debounce interval is specified, keep track of the mutations and debounce the emit.
41- if ( this . debounce > 0 ) {
42- let debouncedEmit = debounce ( ( mutations : MutationRecord [ ] ) => {
43- this . event . emit ( this . _pendingRecords ) ;
44- this . _pendingRecords = [ ] ;
45- } , this . debounce ) ;
46-
47- callback = ( mutations : MutationRecord [ ] ) => {
48- this . _pendingRecords . push . apply ( this . _pendingRecords , mutations ) ;
49- debouncedEmit ( ) ;
50- } ;
51- } else {
52- callback = ( mutations : MutationRecord [ ] ) => this . event . emit ( mutations ) ;
53- }
42+ this . _debouncer
43+ . debounceTime ( this . debounce )
44+ . subscribe ( mutations => this . event . emit ( mutations ) ) ;
5445
55- this . _observer = new ( this . _injector . get ( MutationObserver ) ) ( callback ) ;
46+ this . _observer = new ( this . _injector . get ( MutationObserver ) as any ) (
47+ ( mutations : MutationRecord [ ] ) => this . _debouncer . next ( mutations ) ) ;
5648
5749 this . _observer . observe ( this . _elementRef . nativeElement , {
5850 characterData : true ,
0 commit comments