forked from Third-Culture-Software/bhima
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(accounts): debounce read request
This commit adds a debounce() call to the read request. Since it uses promises and Angular's $timeout(), it will require flushing the $timeouts in tests. Closes Third-Culture-Software#2892.
- Loading branch information
Showing
7 changed files
with
135 additions
and
88 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,41 @@ | ||
angular.module('bhima.services') | ||
.factory('debounce', ['$timeout', '$q', ($timeout, $q) => { | ||
|
||
// The service is actually this function, which we call with the func | ||
// that should be debounced and how long to wait in between calls | ||
return function debounce(func, wait, immediate) { | ||
let timeout; | ||
// Create a deferred object that will be resolved when we need to | ||
// actually call the func | ||
let deferred = $q.defer(); | ||
|
||
return function fn() { | ||
const context = this; | ||
|
||
// eslint-disable-next-line prefer-rest-params | ||
const args = arguments; | ||
|
||
const later = function () { | ||
timeout = null; | ||
if (!immediate) { | ||
deferred.resolve(func.apply(context, args)); | ||
deferred = $q.defer(); | ||
} | ||
}; | ||
|
||
const callNow = immediate && !timeout; | ||
if (timeout) { | ||
$timeout.cancel(timeout); | ||
} | ||
|
||
timeout = $timeout(later, wait); | ||
|
||
if (callNow) { | ||
deferred.resolve(func.apply(context, args)); | ||
deferred = $q.defer(); | ||
} | ||
|
||
return deferred.promise; | ||
}; | ||
}; | ||
}]); |
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
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
Oops, something went wrong.