Skip to content

Commit

Permalink
Use custom throttle function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Nov 8, 2017
1 parent 45cf158 commit 44f6aa6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"grid-index": "^1.0.0",
"jsonlint-lines-primitives": "~1.6.0",
"lodash.isequal": "^3.0.4",
"lodash.throttle": "^4.1.1",
"mapbox-gl-supported": "^1.2.0",
"minimist": "0.0.8",
"package-json-versionify": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/ui/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const util = require('../util/util');
const window = require('../util/window');
const throttle = require('lodash.throttle');
const throttle = require('../util/throttle');

import type Map from './map';

Expand Down
35 changes: 35 additions & 0 deletions src/util/throttle.js
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;
};
26 changes: 26 additions & 0 deletions src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,29 @@ exports.parseCacheControl = function(cacheControl: string): Object {

return header;
};

/**
* Throttle the given function to run at most every `time` milliseconds.
*/
exports.throttle = function(fn: () => void, time: number): () => number {
let pending = false;
let timerId = 0;

const later = () => {
timerId = 0;
if (pending) {
fn();
pending = false;
}
};

return () => {
if (timerId) {
pending = true;
} else {
fn();
timerId = setTimeout(later, time);
}
return timerId;
};
};
49 changes: 49 additions & 0 deletions test/unit/util/throttle.test.js
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();
});
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6189,10 +6189,6 @@ lodash.templatesettings@^3.0.0:
lodash._reinterpolate "^3.0.0"
lodash.escape "^3.0.0"

lodash.throttle@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"

lodash.toplainobject@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash.toplainobject/-/lodash.toplainobject-3.0.0.tgz#28790ad942d293d78aa663a07ecf7f52ca04198d"
Expand Down

0 comments on commit 44f6aa6

Please sign in to comment.