@@ -10,8 +10,9 @@ import {
10
10
AfterContentInit ,
11
11
Injector ,
12
12
} 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' ;
15
16
16
17
/**
17
18
* Directive that triggers a callback whenever the content of
@@ -23,36 +24,24 @@ import {debounce} from '../util/debounce';
23
24
export class ObserveContent implements AfterContentInit , OnDestroy {
24
25
private _observer : MutationObserver ;
25
26
26
- /** Collects any MutationRecords that haven't been emitted yet. */
27
- private _pendingRecords : MutationRecord [ ] = [ ] ;
28
-
29
27
/** Event emitted for each change in the element's content. */
30
28
@Output ( 'cdkObserveContent' ) event = new EventEmitter < MutationRecord [ ] > ( ) ;
31
29
30
+ /** Used for debouncing the emitted values to the observeContent event. */
31
+ private _debouncer = new Subject < MutationRecord [ ] > ( ) ;
32
+
32
33
/** Debounce interval for emitting the changes. */
33
34
@Input ( ) debounce : number ;
34
35
35
36
constructor ( private _elementRef : ElementRef , private _injector : Injector ) { }
36
37
37
38
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
- }
39
+ this . _debouncer
40
+ . debounceTime ( this . debounce )
41
+ . subscribe ( mutations => this . event . emit ( mutations ) ) ;
54
42
55
- this . _observer = new ( this . _injector . get ( MutationObserver ) ) ( callback ) ;
43
+ this . _observer = new ( this . _injector . get ( MutationObserver ) as any ) (
44
+ ( mutations : MutationRecord [ ] ) => this . _debouncer . next ( mutations ) ) ;
56
45
57
46
this . _observer . observe ( this . _elementRef . nativeElement , {
58
47
characterData : true ,
0 commit comments