Skip to content

Commit eb2e454

Browse files
committed
fix: refactor the MutationObserver token to not break AoT
1 parent 9122ebe commit eb2e454

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/lib/core/observe-content/observe-content.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component} from '@angular/core';
22
import {async, TestBed, ComponentFixture, fakeAsync, tick} from '@angular/core/testing';
3-
import {ObserveContentModule} from './observe-content';
3+
import {ObserveContentModule, MdMutationObserverFactory} from './observe-content';
44

55
/**
66
* TODO(elad): `ProxyZone` doesn't seem to capture the events raised by
@@ -63,14 +63,16 @@ describe('Observe content', () => {
6363
imports: [ObserveContentModule],
6464
declarations: [ComponentWithDebouncedListener],
6565
providers: [{
66-
provide: MutationObserver,
67-
useValue: function(callback: Function) {
68-
callbacks.push(callback);
69-
70-
return {
71-
observe: () => {},
72-
disconnect: () => {}
73-
};
66+
provide: MdMutationObserverFactory,
67+
useValue: {
68+
create: function(callback: Function) {
69+
callbacks.push(callback);
70+
71+
return {
72+
observe: () => {},
73+
disconnect: () => {}
74+
};
75+
}
7476
}
7577
}]
7678
});

src/lib/core/observe-content/observe-content.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ import {
88
EventEmitter,
99
OnDestroy,
1010
AfterContentInit,
11-
Injector,
11+
Injectable,
1212
} from '@angular/core';
1313
import {Subject} from 'rxjs/Subject';
1414
import '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
})
6880
export class ObserveContentModule {
6981
/** @deprecated */

0 commit comments

Comments
 (0)