Skip to content

Commit 19b936a

Browse files
committed
fix: refactor the MutationObserver token to not break AoT
1 parent 9b21868 commit 19b936a

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
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
// TODO(elad): `ProxyZone` doesn't seem to capture the events raised by
66
// `MutationObserver` and needs to be investigated
@@ -61,14 +61,16 @@ describe('Observe content', () => {
6161
imports: [ObserveContentModule],
6262
declarations: [ComponentWithDebouncedListener],
6363
providers: [{
64-
provide: MutationObserver,
65-
useValue: function(callback: Function) {
66-
callbacks.push(callback);
67-
68-
return {
69-
observe: () => {},
70-
disconnect: () => {}
71-
};
64+
provide: MdMutationObserverFactory,
65+
useValue: {
66+
create: function(callback: Function) {
67+
callbacks.push(callback);
68+
69+
return {
70+
observe: () => {},
71+
disconnect: () => {}
72+
};
73+
}
7274
}
7375
}]
7476
});

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ import {
77
EventEmitter,
88
OnDestroy,
99
AfterContentInit,
10-
Injector,
10+
Injectable,
1111
} from '@angular/core';
1212
import {Subject} from 'rxjs/Subject';
1313
import 'rxjs/add/operator/debounceTime';
1414

15+
/**
16+
* Factory that creates a new MutationObserver and allows us to stub it out in unit tests.
17+
* @docs-private
18+
*/
19+
@Injectable()
20+
export class MdMutationObserverFactory {
21+
create(callback): MutationObserver {
22+
return new MutationObserver(callback);
23+
}
24+
}
25+
1526
/**
1627
* Directive that triggers a callback whenever the content of
1728
* its associated element has changed.
@@ -31,15 +42,22 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
3142
/** Debounce interval for emitting the changes. */
3243
@Input() debounce: number;
3344

34-
constructor(private _elementRef: ElementRef, private _injector: Injector) { }
45+
constructor(
46+
private _mutationObserverFactory: MdMutationObserverFactory,
47+
private _elementRef: ElementRef) { }
3548

3649
ngAfterContentInit() {
37-
this._debouncer
38-
.debounceTime(this.debounce)
39-
.subscribe(mutations => this.event.emit(mutations));
50+
if (this.debounce > 0) {
51+
this._debouncer
52+
.debounceTime(this.debounce)
53+
.subscribe(mutations => this.event.emit(mutations));
54+
} else {
55+
this._debouncer.subscribe(mutations => this.event.emit(mutations));
56+
}
4057

41-
this._observer = new (this._injector.get(MutationObserver) as any)(
42-
(mutations: MutationRecord[]) => this._debouncer.next(mutations));
58+
this._observer = this._mutationObserverFactory.create((mutations: MutationRecord[]) => {
59+
this._debouncer.next(mutations);
60+
});
4361

4462
this._observer.observe(this._elementRef.nativeElement, {
4563
characterData: true,
@@ -51,17 +69,16 @@ export class ObserveContent implements AfterContentInit, OnDestroy {
5169
ngOnDestroy() {
5270
if (this._observer) {
5371
this._observer.disconnect();
54-
this._observer = null;
72+
this._debouncer.complete();
73+
this._debouncer = this._observer = null;
5574
}
5675
}
5776
}
5877

78+
5979
@NgModule({
6080
exports: [ObserveContent],
6181
declarations: [ObserveContent],
62-
providers: [
63-
// Pass the MutationObserver through DI so it can be stubbed when testing.
64-
{ provide: MutationObserver, useValue: MutationObserver }
65-
]
82+
providers: [MdMutationObserverFactory]
6683
})
6784
export class ObserveContentModule {}

0 commit comments

Comments
 (0)