From 6bbd35d644f7d3fd65d0ff465e246fe5024ebcc6 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sun, 18 Dec 2016 17:11:49 -0500 Subject: [PATCH] cache path name to avoid race conditions when updating url --- frontend/src/Metamaps/Util.js | 4 ++-- frontend/src/Metamaps/Visualize.js | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/frontend/src/Metamaps/Util.js b/frontend/src/Metamaps/Util.js index 4b55342a..9dfc8d3b 100644 --- a/frontend/src/Metamaps/Util.js +++ b/frontend/src/Metamaps/Util.js @@ -237,10 +237,10 @@ const Util = { return obj }, {}) }, - updateQueryParams: function(newValues) { + updateQueryParams: function(newValues, pathname = window.location.pathname) { const qp = Object.assign({}, Util.queryParams(), newValues) const newString = Object.keys(qp).filter(key => !!key).map(key => `${key}=${qp[key]}`).join('&') - history.replaceState({}, document.title, `${window.location.pathname}?${newString}`) + history.replaceState({}, document.title, `${pathname}?${newString}`) } } diff --git a/frontend/src/Metamaps/Visualize.js b/frontend/src/Metamaps/Visualize.js index bacf7f03..ea337ba2 100644 --- a/frontend/src/Metamaps/Visualize.js +++ b/frontend/src/Metamaps/Visualize.js @@ -1,4 +1,4 @@ -/* global $ */ +/* global $, window */ import { find as _find, indexOf as _indexOf, uniq as _uniq, debounce } from 'lodash' @@ -151,8 +151,9 @@ const Visualize = { // monkey patch scale function const oldScale = self.mGraph.canvas.scale + const cachedPathname = window.location.pathname const updateScaleInUrl = debounce(() => { - Util.updateQueryParams({ scale: self.mGraph.canvas.scaleOffsetX.toFixed(2) }) + Util.updateQueryParams({ scale: self.mGraph.canvas.scaleOffsetX.toFixed(2) }, pathname) }, 200) self.mGraph.canvas.scale = function(x, y, disablePlot) { const returnValue = oldScale.apply(self.mGraph.canvas, arguments) @@ -165,7 +166,7 @@ const Visualize = { const updateTranslateInUrl = debounce(() => { const newX = self.mGraph.canvas.translateOffsetX.toFixed(2) const newY = self.mGraph.canvas.translateOffsetY.toFixed(2) - Util.updateQueryParams({ translate: `${newX},${newY}` }) + Util.updateQueryParams({ translate: `${newX},${newY}` }, pathname) }, 200) self.mGraph.canvas.translate = function(x, y, disablePlot) { const returnValue = oldTranslate.apply(self.mGraph.canvas, arguments)