mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
refactors Discourse.debouncePromise
This commit is contained in:
parent
aef4227073
commit
4c4a2f220e
2 changed files with 14 additions and 16 deletions
|
@ -47,25 +47,20 @@ Discourse.debounce = function(func, wait) {
|
||||||
@param {Number} wait how long to wait
|
@param {Number} wait how long to wait
|
||||||
**/
|
**/
|
||||||
Discourse.debouncePromise = function(func, wait) {
|
Discourse.debouncePromise = function(func, wait) {
|
||||||
var timeout = null;
|
var self, args, promise;
|
||||||
var args = null;
|
var later = function() {
|
||||||
var context = null;
|
func.apply(self, args).then(function (funcResult) {
|
||||||
|
promise.resolve(funcResult);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return function() {
|
return function() {
|
||||||
context = this;
|
self = this;
|
||||||
var promise = Ember.Deferred.create();
|
|
||||||
args = arguments;
|
args = arguments;
|
||||||
|
promise = Ember.Deferred.create();
|
||||||
|
|
||||||
if (!timeout) {
|
Ember.run.debounce(null, later, wait);
|
||||||
timeout = Em.run.later(function () {
|
|
||||||
timeout = null;
|
|
||||||
func.apply(context, args).then(function (y) {
|
|
||||||
promise.resolve(y);
|
|
||||||
});
|
|
||||||
}, wait);
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -49,14 +49,17 @@ test("executes only once, no matter how many times debounced function is called
|
||||||
originalAndCallbackFiredOnce("(second call was supressed)");
|
originalAndCallbackFiredOnce("(second call was supressed)");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("does not prolong the timeout when the debounced function is called for the second time during the timeout", function() {
|
test("prolongs the timeout when the debounced function is called for the second time during the timeout", function() {
|
||||||
debounced().then(callback);
|
debounced().then(callback);
|
||||||
|
|
||||||
clock.tick(50);
|
clock.tick(50);
|
||||||
debounced().then(callback);
|
debounced().then(callback);
|
||||||
|
|
||||||
clock.tick(50);
|
clock.tick(50);
|
||||||
originalAndCallbackFiredOnce("exactly at the end of the original timeout");
|
nothingFired("at the end of the original timeout");
|
||||||
|
|
||||||
|
clock.tick(50);
|
||||||
|
originalAndCallbackFiredOnce("exactly at the end of the prolonged timeout");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("preserves last call's context and params when executing delayed function", function() {
|
test("preserves last call's context and params when executing delayed function", function() {
|
||||||
|
|
Loading…
Reference in a new issue