From 88119330099c3447ca2cb8e7bd2312631669aa6d Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Sat, 21 Jul 2018 22:31:04 -0400 Subject: [PATCH] Split up loadTiles and loadNotes code Code is similar but different enough that I'd rather have 2 separate functions rather than a single function with a bunch of ifs --- modules/services/osm.js | 118 +++++++++++++++++++++----------------- modules/ui/note_editor.js | 4 +- 2 files changed, 66 insertions(+), 56 deletions(-) diff --git a/modules/services/osm.js b/modules/services/osm.js index fe02d626d..4a3c0bc5c 100644 --- a/modules/services/osm.js +++ b/modules/services/osm.js @@ -77,6 +77,17 @@ function abortRequest(i) { } +function abortUnwantedRequests(cache, tiles) { + _forEach(cache.inflight, function(v, k) { + var wanted = _find(tiles, function(tile) { return k === tile.id; }); + if (!wanted) { + abortRequest(v); + delete cache.inflight[k]; + } + }); +} + + function getLoc(attrs) { var lon = attrs.lon && attrs.lon.value; var lat = attrs.lat && attrs.lat.value; @@ -749,78 +760,44 @@ export default { }, - // Load data (entities or notes) from the API in tiles + // Load data (entities) from the API in tiles // GET /api/0.6/map?bbox= - // GET /api/0.6/notes?bbox= - loadTiles: function(projection, callback, noteOptions) { + loadTiles: function(projection, callback) { if (_off) return; var that = this; - - // are we loading entities or notes? - var loadingNotes = (noteOptions !== undefined); - var path, cache, tilezoom, throttleLoadUsers; - - if (loadingNotes) { - noteOptions = _extend({ limit: 10000, closed: 7}, noteOptions); - path = '/api/0.6/notes?limit=' + noteOptions.limit + '&closed=' + noteOptions.closed + '&bbox='; - cache = _noteCache; - tilezoom = _noteZoom; - throttleLoadUsers = _throttle(function() { - var uids = Object.keys(_userCache.toLoad); - if (!uids.length) return; - that.loadUsers(uids, function() {}); // eagerly load user details - }, 750); - } else { - path = '/api/0.6/map?bbox='; - cache = _tileCache; - tilezoom = _tileZoom; - } + var path = '/api/0.6/map?bbox='; // determine the needed tiles to cover the view - var tiles = tiler.getTiles(projection, tilezoom); + var tiles = tiler.getTiles(projection, _tileZoom); // abort inflight requests that are no longer needed - var hadRequests = !_isEmpty(cache.inflight); - _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)) { + var hadRequests = !_isEmpty(_tileCache.inflight); + abortUnwantedRequests(_tileCache, tiles); + if (hadRequests && _isEmpty(_tileCache.inflight)) { dispatch.call('loaded'); // stop the spinner } // issue new requests.. tiles.forEach(function(tile) { - if (cache.loaded[tile.id] || cache.inflight[tile.id]) return; - if (!loadingNotes && _isEmpty(cache.inflight)) { + if (_tileCache.loaded[tile.id] || _tileCache.inflight[tile.id]) return; + if (_isEmpty(_tileCache.inflight)) { dispatch.call('loading'); // start the spinner } - var options = { skipSeen: !loadingNotes }; - cache.inflight[tile.id] = that.loadFromAPI( + var options = { skipSeen: true }; + _tileCache.inflight[tile.id] = that.loadFromAPI( path + tile.extent.toParam(), function(err, parsed) { - delete cache.inflight[tile.id]; + delete _tileCache.inflight[tile.id]; if (!err) { - cache.loaded[tile.id] = true; + _tileCache.loaded[tile.id] = true; } - - if (loadingNotes) { - throttleLoadUsers(); - dispatch.call('loadedNotes'); - - } else { - if (callback) { - callback(err, _extend({ data: parsed }, tile)); - } - if (_isEmpty(cache.inflight)) { - dispatch.call('loaded'); // stop the spinner - } + if (callback) { + callback(err, _extend({ data: parsed }, tile)); + } + if (_isEmpty(_tileCache.inflight)) { + dispatch.call('loaded'); // stop the spinner } }, options @@ -829,11 +806,44 @@ export default { }, - // Load notes from the API (just calls this.loadTiles) + // Load notes from the API in tiles // GET /api/0.6/notes?bbox= loadNotes: function(projection, noteOptions) { noteOptions = _extend({ limit: 10000, closed: 7}, noteOptions); - this.loadTiles(projection, null, noteOptions); + if (_off) return; + + var that = this; + var path = '/api/0.6/notes?limit=' + noteOptions.limit + '&closed=' + noteOptions.closed + '&bbox='; + var throttleLoadUsers = _throttle(function() { + var uids = Object.keys(_userCache.toLoad); + if (!uids.length) return; + that.loadUsers(uids, function() {}); // eagerly load user details + }, 750); + + // determine the needed tiles to cover the view + var tiles = tiler.getTiles(projection, _noteZoom); + + // abort inflight requests that are no longer needed + abortUnwantedRequests(_noteCache, tiles); + + // issue new requests.. + tiles.forEach(function(tile) { + if (_noteCache.loaded[tile.id] || _noteCache.inflight[tile.id]) return; + + var options = { skipSeen: false }; + _noteCache.inflight[tile.id] = that.loadFromAPI( + path + tile.extent.toParam(), + function(err) { + delete _noteCache.inflight[tile.id]; + if (!err) { + _noteCache.loaded[tile.id] = true; + } + throttleLoadUsers(); + dispatch.call('loadedNotes'); + }, + options + ); + }); }, diff --git a/modules/ui/note_editor.js b/modules/ui/note_editor.js index 1d96d80f1..901419b72 100644 --- a/modules/ui/note_editor.js +++ b/modules/ui/note_editor.js @@ -59,7 +59,7 @@ export function uiNoteEditor(context) { var editor = body.selectAll('.note-editor') .data([0]); - editor = editor.enter() + editor.enter() .append('div') .attr('class', 'modal-section note-editor') .merge(editor) @@ -71,7 +71,7 @@ export function uiNoteEditor(context) { var footer = selection.selectAll('.footer') .data([0]); - footer = footer.enter() + footer.enter() .append('div') .attr('class', 'footer') .merge(footer)