From efd9e6df4be665a1f4de0781f3a740d5e9393c35 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Tue, 18 Oct 2016 21:48:39 -0400 Subject: [PATCH] Memoize and avoid extra calls to utilDetect() --- modules/services/mapillary.js | 2 +- modules/svg/gpx.js | 3 ++- modules/svg/labels.js | 2 +- modules/svg/lines.js | 3 ++- modules/ui/background.js | 3 ++- modules/util/detect.js | 4 +++- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/modules/services/mapillary.js b/modules/services/mapillary.js index e30ccd34c..2be4ba2b5 100644 --- a/modules/services/mapillary.js +++ b/modules/services/mapillary.js @@ -18,6 +18,7 @@ var apibase = 'https://a.mapillary.com/v2/', maxPages = 10, tileZoom = 14, dispatch = d3.dispatch('loadedImages', 'loadedSigns'), + detected = utilDetect(), mapillaryCache, mapillaryClicks, mapillaryImage, @@ -298,7 +299,6 @@ export default { signsSupported: function() { - var detected = utilDetect(); return (!(detected.ie || detected.browser.toLowerCase() === 'safari')); }, diff --git a/modules/svg/gpx.js b/modules/svg/gpx.js index 1b115322e..10afc9dcb 100644 --- a/modules/svg/gpx.js +++ b/modules/svg/gpx.js @@ -7,6 +7,7 @@ import toGeoJSON from 'togeojson'; export function svgGpx(projection, context, dispatch) { var showLabels = true, + detected = utilDetect(), layer; @@ -27,7 +28,7 @@ export function svgGpx(projection, context, dispatch) { .on('drop.localgpx', function() { d3.event.stopPropagation(); d3.event.preventDefault(); - if (!utilDetect().filedrop) return; + if (!detected.filedrop) return; drawGpx.files(d3.event.dataTransfer.files); }) .on('dragenter.localgpx', over) diff --git a/modules/svg/labels.js b/modules/svg/labels.js index 969db0fca..d1bf681e8 100644 --- a/modules/svg/labels.js +++ b/modules/svg/labels.js @@ -340,7 +340,7 @@ export function svgLabels(projection, context) { var coord = projection(entity.loc), margin = 2, - textDirection = utilDetect().textDirection, + textDirection = detected.textDirection, offset = pointOffsets[textDirection], p = { height: height, diff --git a/modules/svg/lines.js b/modules/svg/lines.js index ea51a7e96..f88174a1a 100644 --- a/modules/svg/lines.js +++ b/modules/svg/lines.js @@ -12,6 +12,7 @@ import { utilDetect } from '../util/detect'; export function svgLines(projection) { + var detected = utilDetect(); var highway_stack = { motorway: 0, @@ -135,7 +136,7 @@ export function svgLines(projection) { .merge(oneways) .attr('d', function(d) { return d.d; }); - if (utilDetect().ie) { + if (detected.ie) { oneways.each(function() { this.parentNode.insertBefore(this, this); }); } }; diff --git a/modules/ui/background.js b/modules/ui/background.js index feb8db766..2e956fb92 100644 --- a/modules/ui/background.js +++ b/modules/ui/background.js @@ -15,6 +15,7 @@ import { tooltip } from '../util/tooltip'; export function uiBackground(context) { var key = 'B', + detected = utilDetect(), opacities = [1, 0.75, 0.5, 0.25], directions = [ ['right', [0.5, 0]], @@ -45,7 +46,7 @@ export function uiBackground(context) { .style('opacity', d) .attr('data-opacity', d); - if (!utilDetect().opera) { + if (!detected.opera) { utilSetTransform(bg, 0, 0); } diff --git a/modules/util/detect.js b/modules/util/detect.js index 54c7a1d06..4fa915a52 100644 --- a/modules/util/detect.js +++ b/modules/util/detect.js @@ -1,9 +1,11 @@ import { currentLocale, setTextDirection } from './locale'; import { utilStringQs } from './index'; +var detected; export function utilDetect() { - var detected = {}; + if (detected) return detected; + detected = {}; var ua = navigator.userAgent, m = null;