-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #1: Added debounceWait options for the component
- Created utility lib to do the job, based on the underscore debounce - Added some dependencies for better testing - Tested the debounce! 👍
- Loading branch information
1 parent
4df851f
commit 1581791
Showing
5 changed files
with
118 additions
and
9 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
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,49 @@ | ||
/** | ||
* Returns a function that will not be triggered after right after a call, waiting ${wait} seconds | ||
* to be able to invoke it again. If ${immediate}, it will call again in the leading edge, instead of trailing. | ||
* It's a adapted version of {@link http://underscorejs.org/#debounce} | ||
* @param {Function} func | ||
* @param {number} wait | ||
* @param {boolean} [immediate=false] | ||
* @returns {Function} denounced | ||
*/ | ||
export default function debounce(func, wait, immediate = false) { | ||
let timeout, timestamp, context, args, result; | ||
|
||
const later = () => { | ||
const last = Date.now() - timestamp; | ||
|
||
if (last < wait && last >= 0) { | ||
timeout = setTimeout(later, wait - last); | ||
} | ||
else { | ||
timeout = null; | ||
if (!immediate) { | ||
result = func.apply(context, args); | ||
if (!timeout) { | ||
context = args = null; | ||
} | ||
} | ||
} | ||
|
||
}; | ||
|
||
return function (..._args) { | ||
const callNow = immediate && !timeout; | ||
|
||
context = this; | ||
args = _args; | ||
timestamp = Date.now(); | ||
|
||
if (!timeout) { | ||
timeout = setTimeout(later, wait); | ||
} | ||
|
||
if (callNow) { | ||
result = func.apply(context, args); | ||
context = args = null; | ||
} | ||
|
||
return result; | ||
}; | ||
} |
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,43 @@ | ||
import chai, {expect} from 'chai'; | ||
import sinon from 'sinon'; | ||
import spies from 'chai-spies'; | ||
|
||
import debounce from '../../src/utils/debounce'; | ||
|
||
describe('utils/debounce', () => { | ||
chai.use(spies); | ||
let clock, spy; | ||
|
||
beforeEach(() => { | ||
clock = sinon.useFakeTimers(); | ||
spy = chai.spy(function () { | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
clock.restore(); | ||
}); | ||
|
||
it('Should block any calls before wait time is not finished', () => { | ||
const debounced = debounce(spy, 100, true); | ||
debounced(); // First call | ||
|
||
clock.tick(50); | ||
debounced(); //should not call | ||
|
||
clock.tick(100); | ||
debounced(); //should call again | ||
|
||
expect(spy).to.have.been.called.twice; | ||
}); | ||
|
||
it("Should delay the call if not immediate", () => { | ||
const debounced = debounce(spy, 100); | ||
debounced(); // Should not call yet | ||
|
||
clock.tick(101); | ||
debounced(); //Call | ||
|
||
expect(spy).to.have.been.called.once; | ||
}); | ||
}); |