-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(observe-content): add debounce option and other improvements
* Adds a reusable utility for debouncing a function. * Adds the ability to debounce the changes from the `cdkObserveContent` directive. * Makes the `cdkObserveContent` directive pass back the `MutationRecord` to the `EventEmitter`. * Fires the callback once per mutation event, instead of once per `MutationRecord`. Relates to #2372.
- Loading branch information
Showing
4 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {fakeAsync, tick} from '@angular/core/testing'; | ||
import {debounce} from './debounce'; | ||
|
||
describe('debounce', () => { | ||
let func: jasmine.Spy; | ||
|
||
beforeEach(() => func = jasmine.createSpy('test function')); | ||
|
||
it('should debounce calls to a function', fakeAsync(() => { | ||
let debounced = debounce(func, 100); | ||
|
||
debounced(); | ||
debounced(); | ||
debounced(); | ||
|
||
tick(100); | ||
|
||
expect(func).toHaveBeenCalledTimes(1); | ||
})); | ||
|
||
it('should pass the arguments to the debounced function', fakeAsync(() => { | ||
let debounced = debounce(func, 250); | ||
|
||
debounced(1, 2, 3); | ||
debounced(4, 5, 6); | ||
|
||
tick(250); | ||
|
||
expect(func).toHaveBeenCalledWith(4, 5, 6); | ||
})); | ||
|
||
it('should be able to invoke a function with a context', fakeAsync(() => { | ||
let context = { name: 'Bilbo' }; | ||
let debounced = debounce(func, 300, context); | ||
|
||
debounced(); | ||
tick(300); | ||
|
||
expect(func.calls.mostRecent().object).toBe(context); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Returns a function that won't be invoked, as long as it keeps being called. It will | ||
* be invoked after it hasn't been called for `delay` milliseconds. | ||
* | ||
* @param func Function to be debounced. | ||
* @param delay Amount of milliseconds to wait before calling the function. | ||
* @param context Context in which to call the function. | ||
*/ | ||
export function debounce(func: Function, delay: number, context?: any): Function { | ||
let timer: number; | ||
|
||
return function() { | ||
let args = arguments; | ||
|
||
clearTimeout(timer); | ||
|
||
timer = setTimeout(() => { | ||
timer = null; | ||
func.apply(context, args); | ||
}, delay); | ||
}; | ||
}; |