-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Wojciechowski
committed
Nov 8, 2017
1 parent
45cf158
commit 44f6aa6
Showing
6 changed files
with
111 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @flow | ||
|
||
/** | ||
* Throttle the given function to run at most every `period` milliseconds. | ||
*/ | ||
module.exports = function(unthrottledFunction: () => void, period: number): () => void { | ||
|
||
// The next time (unix epoch) that the function is allowed to execute | ||
let nextTime = 0; | ||
|
||
// `true` if there is a pending "setTimeout" operation that'll invoke the | ||
// function at a later time. `false` if there is not. | ||
let pending = false; | ||
|
||
let throttledFunction = () => { | ||
const time = Date.now(); | ||
|
||
if (nextTime <= time && !pending) { | ||
nextTime = time + period; | ||
unthrottledFunction(); | ||
} else if (!pending) { | ||
pending = true; | ||
setTimeout(_throttledFunction, nextTime - time); | ||
} | ||
}; | ||
|
||
// This callback to `setTimeout` is written outside `throttledFunction` to | ||
// reduce the number of closures created. | ||
let _throttledFunction = () => { | ||
pending = false; | ||
throttledFunction(); | ||
}; | ||
|
||
return throttledFunction; | ||
}; |
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 @@ | ||
'use strict'; | ||
// @flow | ||
|
||
const test = require('mapbox-gl-js-test').test; | ||
const throttle = require('../../../src/util/throttle'); | ||
|
||
test('throttle', (t) => { | ||
|
||
t.test('does not execute unthrottled function unless throttled function is invoked', (t) => { | ||
let executionCount = 0; | ||
let throttledFunction = throttle(() => executionCount++, 0); | ||
t.equal(executionCount, 0); | ||
t.end(); | ||
}); | ||
|
||
t.test('executes unthrottled function immediately when period is 0', (t) => { | ||
let executionCount = 0; | ||
let throttledFunction = throttle(() => executionCount++, 0); | ||
throttledFunction(); | ||
throttledFunction(); | ||
throttledFunction(); | ||
t.equal(executionCount, 3); | ||
t.end(); | ||
}); | ||
|
||
t.test('executes unthrottled function once when period is > 0', (t) => { | ||
let executionCount = 0; | ||
let throttledFunction = throttle(() => executionCount++, 5); | ||
throttledFunction(); | ||
throttledFunction(); | ||
throttledFunction(); | ||
t.equal(executionCount, 1); | ||
t.end(); | ||
}); | ||
|
||
t.test('queues exactly one execution of unthrottled function when period is > 0', (t) => { | ||
let executionCount = 0; | ||
let throttledFunction = throttle(() => executionCount++, 5); | ||
throttledFunction(); | ||
throttledFunction(); | ||
throttledFunction(); | ||
setTimeout(() => { | ||
t.equal(executionCount, 2); | ||
t.end(); | ||
}, 10); | ||
}); | ||
|
||
t.end(); | ||
}); |
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