Skip to content
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 8 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class Tile {
expirationTime: any;
expiredRequestCount: number;
state: TileState;
placementThrottler: any;
timeAdded: any;
fadeEndTime: any;
rawTileData: ArrayBuffer;
Expand Down
10 changes: 8 additions & 2 deletions src/ui/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import type Map from './map';

Expand All @@ -13,12 +14,17 @@ import type Map from './map';
*/
class Hash {
_map: Map;
_updateHash: () => void;

constructor() {
constructor(options: ?{throttle?: Boolean;}) {
util.bindAll([
'_onHashChange',
'_updateHash'
], this);

// Mobile Safari doesn't allow updating the hash more than 100 times per 30 seconds.
const throttleTime = (options && options.throttle === false) ? 0 : 30 * 1000 / 100;
this._updateHash = throttle(this._updateHashUnthrottled.bind(this), throttleTime);
}

/*
Expand Down Expand Up @@ -82,7 +88,7 @@ class Hash {
return false;
}

_updateHash() {
_updateHashUnthrottled() {
const hash = this.getHashString();
window.history.replaceState('', '', hash);
}
Expand Down
36 changes: 36 additions & 0 deletions src/util/throttle.js
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
Copy link
Member

@mourner mourner Nov 9, 2017

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:

In ES6, block-level bindings (let and const) introduce a “temporal dead zone” where a ReferenceError will be thrown with any attempt to access the variable before its declaration.

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 with let?

Copy link
Contributor Author

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.

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;
};
59 changes: 0 additions & 59 deletions src/util/throttler.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/unit/ui/hash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Map = require('../../../src/ui/map');

test('hash', (t) => {
function createHash() {
return new Hash();
return new Hash({throttle: false});
}

function createMap() {
Expand Down
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;
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();
});