This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(debounce): create debounce helper
- Create internal debounce helper - Move debounce logic from typeahead to use helper Closes #3966 Closes #4813
- Loading branch information
Showing
3 changed files
with
75 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
angular.module('ui.bootstrap.debounce', []) | ||
/** | ||
* A helper, internal service that debounces a function | ||
*/ | ||
.factory('$$debounce', ['$timeout', function($timeout) { | ||
return function(callback, debounceTime) { | ||
var timeoutPromise; | ||
|
||
return function() { | ||
var self = this; | ||
var args = Array.prototype.slice(arguments); | ||
if (timeoutPromise) { | ||
$timeout.cancel(timeoutPromise); | ||
} | ||
|
||
timeoutPromise = $timeout(function() { | ||
callback.apply(self, args); | ||
}, debounceTime); | ||
}; | ||
}; | ||
}]); |
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,40 @@ | ||
describe('$$debounce', function() { | ||
var $$debounce, $timeout, debouncedFunction, i; | ||
|
||
beforeEach(module('ui.bootstrap.debounce')); | ||
beforeEach(inject(function(_$$debounce_, _$timeout_) { | ||
$$debounce = _$$debounce_; | ||
$timeout = _$timeout_; | ||
i = 0; | ||
debouncedFunction = $$debounce(function() { | ||
i++; | ||
}, 100); | ||
})); | ||
|
||
it('should function like a $timeout when called once during timeout', function() { | ||
debouncedFunction(); | ||
$timeout.flush(50); | ||
|
||
expect(i).toBe(0); | ||
|
||
$timeout.flush(50); | ||
|
||
expect(i).toBe(1); | ||
}); | ||
|
||
it('should only execute 100ms after last call when called twice', function() { | ||
debouncedFunction(); | ||
$timeout.flush(50); | ||
|
||
expect(i).toBe(0); | ||
|
||
debouncedFunction(); | ||
$timeout.flush(50); | ||
|
||
expect(i).toBe(0); | ||
|
||
$timeout.flush(50); | ||
|
||
expect(i).toBe(1); | ||
}); | ||
}); |
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