Permalink
Browse files

feat(debounce): Add initial service factory - NEEDS TESTS and DOCS

  • Loading branch information...
petebacondarwin committed Feb 11, 2013
1 parent 7089119 commit 8290293274033e142c3317190a528639b28b83c2
Showing with 48 additions and 0 deletions.
  1. +48 −0 modules/services/debounce/debounce.js
@@ -0,0 +1,48 @@
+/**
+ * uiDebounce service provides a mechanism for creating a wrapper around a function
+ * that ensures that the wrapped function is not called more frequently than a
+ * given time interval.
+ *
+ * @param {!Function} func The function to be wrapped (debounced)
+ * @param {number} wait How long between called to func
+ * @param {Boolean} immediate If true then the function is invoked on the first call to the
+ * wrapper function, otherwise the call will not happen until after the wait time has expired
+ * @return {Function} A debounced wrapper around the func function.
+ *
+ * @example
+ * function lookup(id) { ... lookup something ... }
+ * var debounceLookup = debounce(lookup, 2000, false);
+ * $scope.doLookup = function(msg) {
+ * var promise = debounceLookup(msg);
+ * console.log('called lookup: ', promise);
+ * promise.then(function(value) {
+ * console.log('lookup returned:', value);
+ * });
+ * };
+ */
+angular.module('ui.services').factory('uiDebounce', function($timeout, $q) {
+ return function(func, wait, immediate) {
+ var timeout;
+ var deferred = $q.defer();
+ return function() {
+ var context = this, args = arguments;
+ var later = function() {
+ timeout = null;
+ if(!immediate) {
+ deferred.resolve(func.apply(context, args));
+ deferred = $q.defer();
+ }
+ };
+ var 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;
+ };
+ };
+});

0 comments on commit 8290293

Please sign in to comment.