Skip to content

Commit c5ba74c

Browse files
committed
refactor: use debounce logic from rxjs
1 parent 7bf7fe6 commit c5ba74c

File tree

5 files changed

+12
-94
lines changed

5 files changed

+12
-94
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,6 @@ describe('Observe content', () => {
8989
tick(500);
9090
expect(fixture.componentInstance.spy).toHaveBeenCalledTimes(1);
9191
}));
92-
93-
it('should should keep track of and merge all of the mutation records', fakeAsync(() => {
94-
invokeCallbacks([1]);
95-
invokeCallbacks([2]);
96-
invokeCallbacks([3]);
97-
98-
tick(500);
99-
expect(fixture.componentInstance.spy).toHaveBeenCalledWith([1, 2, 3]);
100-
}));
10192
});
10293
});
10394

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -23,36 +24,24 @@ import {debounce} from '../util/debounce';
2324
export class ObserveContent implements AfterContentInit, OnDestroy {
2425
private _observer: MutationObserver;
2526

26-
/** Collects any MutationRecords that haven't been emitted yet. */
27-
private _pendingRecords: MutationRecord[] = [];
28-
2927
/** Event emitted for each change in the element's content. */
3028
@Output('cdkObserveContent') event = new EventEmitter<MutationRecord[]>();
3129

30+
/** Used for debouncing the emitted values to the observeContent event. */
31+
private _debouncer = new Subject<MutationRecord[]>();
32+
3233
/** Debounce interval for emitting the changes. */
3334
@Input() debounce: number;
3435

3536
constructor(private _elementRef: ElementRef, private _injector: Injector) { }
3637

3738
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));
5442

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));
5645

5746
this._observer.observe(this._elementRef.nativeElement, {
5847
characterData: true,

src/lib/core/util/debounce.spec.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/lib/core/util/debounce.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

tools/gulp/util/rollup-helper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const ROLLUP_GLOBALS = {
3232
'rxjs/add/operator/first': 'Rx.Observable.prototype',
3333
'rxjs/add/operator/startWith': 'Rx.Observable.prototype',
3434
'rxjs/add/operator/switchMap': 'Rx.Observable.prototype',
35+
'rxjs/add/operator/debounceTime': 'Rx.Observable.prototype',
3536
'rxjs/Observable': 'Rx'
3637
};
3738

0 commit comments

Comments
 (0)