From 02713e48fbeaa95fe3b82e4eae6c5c994f0cc262 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Sat, 21 Jul 2018 11:11:39 -0400 Subject: [PATCH] Move cache management out of tiler, it's responsibilty of service Also remove some unused code and eslint warnings --- modules/services/mapillary.js | 40 +++++++++---------------------- modules/services/openstreetcam.js | 30 ++++++++++------------- modules/services/osm.js | 16 +++++++++---- modules/services/streetside.js | 26 ++++++++++---------- modules/util/tiler.js | 21 ++++------------ 5 files changed, 53 insertions(+), 80 deletions(-) diff --git a/modules/services/mapillary.js b/modules/services/mapillary.js index 431b8f1e8..3338211ab 100644 --- a/modules/services/mapillary.js +++ b/modules/services/mapillary.js @@ -1,5 +1,4 @@ /* global Mapillary:false */ -import _filter from 'lodash-es/filter'; import _find from 'lodash-es/find'; import _flatten from 'lodash-es/flatten'; import _forEach from 'lodash-es/forEach'; @@ -44,18 +43,6 @@ function abortRequest(i) { } -function nearNullIsland(x, y, z) { - if (z >= 7) { - var center = Math.pow(2, z - 1); - var width = Math.pow(2, z - 6); - var min = center - (width / 2); - var max = center + (width / 2) - 1; - return x >= min && x <= max && y >= min && y <= max; - } - return false; -} - - function maxPageAtZoom(z) { if (z < 15) return 2; if (z === 15) return 5; @@ -66,28 +53,23 @@ function maxPageAtZoom(z) { } -function localeTimestamp(s) { - if (!s) return null; - var detected = utilDetect(); - var options = { - day: 'numeric', month: 'short', year: 'numeric', - hour: 'numeric', minute: 'numeric', second: 'numeric', - timeZone: 'UTC' - }; - var d = new Date(s); - if (isNaN(d.getTime())) return null; - return d.toLocaleString(detected.locale, options); -} - - function loadTiles(which, url, projection) { var s = projection.scale() * 2 * Math.PI; var currZoom = Math.floor(Math.max(Math.log(s) / Math.log(2) - 8, 0)); var dimension = projection.clipExtent()[1]; - var tiles = geoTile.getTiles(projection, dimension, tileZoom, 0); + var tiles = geoTile.getTiles(projection, dimension, tileZoom); - geoTile.removeInflightRequests(which, tiles, abortRequest, ',0'); + // abort inflight requests that are no longer needed + var cache = _mlyCache[which]; + _forEach(cache.inflight, function(v, k) { + var wanted = _find(tiles, function(tile) { return k.indexOf(tile.id + ',') === 0; }); + + if (!wanted) { + abortRequest(v); + delete cache.inflight[k]; + } + }); tiles.forEach(function(tile) { loadNextTilePage(which, currZoom, url, tile); diff --git a/modules/services/openstreetcam.js b/modules/services/openstreetcam.js index dd9693176..f7577b51d 100644 --- a/modules/services/openstreetcam.js +++ b/modules/services/openstreetcam.js @@ -1,4 +1,3 @@ -import _filter from 'lodash-es/filter'; import _find from 'lodash-es/find'; import _flatten from 'lodash-es/flatten'; import _forEach from 'lodash-es/forEach'; @@ -53,18 +52,6 @@ function abortRequest(i) { } -function nearNullIsland(x, y, z) { - if (z >= 7) { - var center = Math.pow(2, z - 1), - width = Math.pow(2, z - 6), - min = center - (width / 2), - max = center + (width / 2) - 1; - return x >= min && x <= max && y >= min && y <= max; - } - return false; -} - - function maxPageAtZoom(z) { if (z < 15) return 2; if (z === 15) return 5; @@ -76,13 +63,22 @@ function maxPageAtZoom(z) { function loadTiles(which, url, projection) { - var s = projection.scale() * 2 * Math.PI, - currZoom = Math.floor(Math.max(Math.log(s) / Math.log(2) - 8, 0)); + var s = projection.scale() * 2 * Math.PI; + var currZoom = Math.floor(Math.max(Math.log(s) / Math.log(2) - 8, 0)); var dimension = projection.clipExtent()[1]; - var tiles = geoTile.getTiles(projection, dimension, tileZoom, 0); + var tiles = geoTile.getTiles(projection, dimension, tileZoom); - geoTile.removeInflightRequests(which, tiles, abortRequest, ',0'); + // abort inflight requests that are no longer needed + var cache = _oscCache[which]; + _forEach(cache.inflight, function(v, k) { + var wanted = _find(tiles, function(tile) { return k.indexOf(tile.id + ',') === 0; }); + + if (!wanted) { + abortRequest(v); + delete cache.inflight[k]; + } + }); tiles.forEach(function(tile) { loadNextTilePage(which, currZoom, url, tile); diff --git a/modules/services/osm.js b/modules/services/osm.js index edf390a93..2aecabb6e 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -2,7 +2,6 @@ import _chunk from 'lodash-es/chunk'; import _cloneDeep from 'lodash-es/cloneDeep'; import _extend from 'lodash-es/extend'; import _forEach from 'lodash-es/forEach'; -import _filter from 'lodash-es/filter'; import _find from 'lodash-es/find'; import _groupBy from 'lodash-es/groupBy'; import _isEmpty from 'lodash-es/isEmpty'; @@ -778,12 +777,19 @@ export default { tilezoom = _tileZoom; } - // get tiles - var tiles = geoTile.getTiles(projection, dimensions, tilezoom, 0); + // determine the needed tiles to cover the view + var tiles = geoTile.getTiles(projection, dimensions, tilezoom); - // remove inflight requests that no longer cover the view.. + // abort inflight requests that are no longer needed var hadRequests = !_isEmpty(cache.inflight); - geoTile.removeInflightRequests(cache, tiles, abortRequest); + _forEach(cache.inflight, function(v, k) { + var wanted = _find(tiles, function(tile) { return k === tile.id; }); + if (!wanted) { + abortRequest(v); + delete cache.inflight[k]; + } + }); + if (hadRequests && !loadingNotes && _isEmpty(cache.inflight)) { dispatch.call('loaded'); // stop the spinner } diff --git a/modules/services/streetside.js b/modules/services/streetside.js index 3f5c6f1a6..b7b592cba 100644 --- a/modules/services/streetside.js +++ b/modules/services/streetside.js @@ -1,4 +1,5 @@ import _extend from 'lodash-es/extend'; +import _find from 'lodash-es/find'; import _flatten from 'lodash-es/flatten'; import _forEach from 'lodash-es/forEach'; import _map from 'lodash-es/map'; @@ -53,6 +54,7 @@ var _pannellumViewer; var _sceneOptions; var _dataUrlArray = []; + /** * abortRequest(). */ @@ -60,19 +62,6 @@ function abortRequest(i) { i.abort(); } -/** - * nearNullIsland(). - */ -function nearNullIsland(x, y, z) { - if (z >= 7) { - var center = Math.pow(2, z - 1); - var width = Math.pow(2, z - 6); - var min = center - (width / 2); - var max = center + (width / 2) - 1; - return x >= min && x <= max && y >= min && y <= max; - } - return false; -} /** * localeTimeStamp(). @@ -99,6 +88,17 @@ function loadTiles(which, url, projection, margin) { .margin(margin) .getTiles(projection, dimension, tileZoom); + // abort inflight requests that are no longer needed + var cache = _ssCache[which]; + _forEach(cache.inflight, function(v, k) { + var wanted = _find(tiles, function(tile) { return k.indexOf(tile.id + ',') === 0; }); + + if (!wanted) { + abortRequest(v); + delete cache.inflight[k]; + } + }); + tiles.forEach(function (tile) { loadNextTilePage(which, currZoom, url, tile); }); diff --git a/modules/util/tiler.js b/modules/util/tiler.js index 180f1197d..27bb17759 100644 --- a/modules/util/tiler.js +++ b/modules/util/tiler.js @@ -1,5 +1,3 @@ -import _filter from 'lodash-es/filter'; -import _find from 'lodash-es/find'; import { range as d3_range } from 'd3-array'; import { geoExtent } from '../geo'; @@ -19,7 +17,10 @@ export function utilTiler() { } - function nearNullIsland(x, y, z) { + function nearNullIsland(tile) { + var x = tile[0]; + var y = tile[1]; + var z = tile[2]; if (z >= 7) { var center = Math.pow(2, z - 1); var width = Math.pow(2, z - 6); @@ -93,7 +94,7 @@ export function utilTiler() { return tiler() .map(function(tile) { - if (_skipNullIsland && nearNullIsland(tile[0], tile[1], tile[2])) { + if (_skipNullIsland && nearNullIsland(tile)) { return false; } var x = tile[0] * ts - origin[0]; @@ -110,18 +111,6 @@ export function utilTiler() { }; - // remove inflight requests that no longer cover the view.. - tiler.removeInflightRequests = function(cache, tiles, callback, modifier) { - return _filter(cache.inflight, function(v, i) { - var wanted = _find(tiles, function(tile) { return i === tile.id + modifier; }); - if (!wanted) { - delete cache.inflight[i]; - } - return !wanted; - }).map(callback); // abort request - }; - - tiler.scaleExtent = function(val) { if (!arguments.length) return _scaleExtent; _scaleExtent = val;