-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throttle calls to "window.history.replaceState" to fix Mobile Safari error message #5613
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
753b37c
Throttle calls to "window.history.replaceState" to fix Mobile Safari …
45cf158
Fix unit tests
3ac5097
Use custom throttle function
7c0da67
Appease the linter gods
3eb7a25
Revert changes to debug/index.html
ebc326e
Revert changes to util.js
20f3fe0
Switch to function declarations
6eb096b
Use @mourner's throttle implementation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,36 @@ | ||
// @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; | ||
|
||
const throttledFunction = () => { | ||
const time = Date.now(); | ||
|
||
if (nextTime <= time && !pending) { | ||
nextTime = time + period; | ||
unthrottledFunction(); | ||
} else if (!pending) { | ||
pending = true; | ||
// eslint-disable-next-line no-use-before-define | ||
setTimeout(_throttledFunction, nextTime - time); | ||
} | ||
}; | ||
|
||
// This callback to `setTimeout` is written outside `throttledFunction` to | ||
// reduce the number of closures created. | ||
const _throttledFunction = () => { | ||
pending = false; | ||
throttledFunction(); | ||
}; | ||
|
||
return throttledFunction; | ||
}; |
This file was deleted.
Oops, something went wrong.
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; | ||
throttle(() => { executionCount++; }, 0); | ||
t.equal(executionCount, 0); | ||
t.end(); | ||
}); | ||
|
||
t.test('executes unthrottled function immediately when period is 0', (t) => { | ||
let executionCount = 0; | ||
const throttledFunction = throttle(() => { executionCount++; }, 0); | ||
throttledFunction(); | ||
throttledFunction(); | ||
throttledFunction(); | ||
t.equal(executionCount, 3); | ||
t.end(); | ||
}); | ||
|
||
t.test('executes unthrottled function immediately once when period is > 0', (t) => { | ||
let executionCount = 0; | ||
const 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; | ||
const throttledFunction = throttle(() => { executionCount++; }, 5); | ||
throttledFunction(); | ||
throttledFunction(); | ||
throttledFunction(); | ||
setTimeout(() => { | ||
t.equal(executionCount, 2); | ||
t.end(); | ||
}, 10); | ||
}); | ||
|
||
t.end(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ESLint docs on the rule state that:
So if this code weren't transpiled (which may happen in future) it would throw an error in theory. So perhaps let's rewrite to either use
function
declaration instead of arrows, or declare one of the vars at the top withlet
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoa! I've been out in the woods too long. Fixed in 20f3fe0.