@@ -8,11 +8,22 @@ import {
88 EventEmitter ,
99 OnDestroy ,
1010 AfterContentInit ,
11- Injector ,
11+ Injectable ,
1212} from '@angular/core' ;
1313import { Subject } from 'rxjs/Subject' ;
1414import 'rxjs/add/operator/debounceTime' ;
1515
16+ /**
17+ * Factory that creates a new MutationObserver and allows us to stub it out in unit tests.
18+ * @docs -private
19+ */
20+ @Injectable ( )
21+ export class MdMutationObserverFactory {
22+ create ( callback ) : MutationObserver {
23+ return new MutationObserver ( callback ) ;
24+ }
25+ }
26+
1627/**
1728 * Directive that triggers a callback whenever the content of
1829 * its associated element has changed.
@@ -32,15 +43,18 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
3243 /** Debounce interval for emitting the changes. */
3344 @Input ( ) debounce : number ;
3445
35- constructor ( private _elementRef : ElementRef , private _injector : Injector ) { }
46+ constructor (
47+ private _mutationObserverFactory : MdMutationObserverFactory ,
48+ private _elementRef : ElementRef ) { }
3649
3750 ngAfterContentInit ( ) {
3851 this . _debouncer
3952 . debounceTime ( this . debounce )
4053 . subscribe ( mutations => this . event . emit ( mutations ) ) ;
4154
42- this . _observer = new ( this . _injector . get ( MutationObserver ) as any ) (
43- ( mutations : MutationRecord [ ] ) => this . _debouncer . next ( mutations ) ) ;
55+ this . _observer = this . _mutationObserverFactory . create ( ( mutations : MutationRecord [ ] ) => {
56+ this . _debouncer . next ( mutations ) ;
57+ } ) ;
4458
4559 this . _observer . observe ( this . _elementRef . nativeElement , {
4660 characterData : true ,
@@ -57,13 +71,11 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
5771 }
5872}
5973
74+
6075@NgModule ( {
6176 exports : [ ObserveContent ] ,
6277 declarations : [ ObserveContent ] ,
63- providers : [
64- // Pass the MutationObserver through DI so it can be stubbed when testing.
65- { provide : MutationObserver , useValue : MutationObserver }
66- ]
78+ providers : [ MdMutationObserverFactory ]
6779} )
6880export class ObserveContentModule {
6981 /** @deprecated */
0 commit comments