From 494d1e2562b681ed1b25f7b2b250e5f862d07156 Mon Sep 17 00:00:00 2001 From: Chris Voll Date: Tue, 21 Feb 2017 15:09:53 -0500 Subject: [PATCH] preserve parent tiles in the source cache --- src/source/source_cache.js | 2 +- src/util/lru_cache.js | 15 +++++++++++++++ test/unit/source/source_cache.test.js | 2 +- test/unit/util/lru_cache.test.js | 10 ++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/source/source_cache.js b/src/source/source_cache.js index faf16d5530c..d558c844ad5 100644 --- a/src/source/source_cache.js +++ b/src/source/source_cache.js @@ -262,7 +262,7 @@ class SourceCache extends Evented { } if (this._cache.has(coord.id)) { retain[coord.id] = true; - return this._cache.get(coord.id); + return this._cache.getWithoutRemoving(coord.id); } } } diff --git a/src/util/lru_cache.js b/src/util/lru_cache.js index 52eb8303752..77cad1694f5 100644 --- a/src/util/lru_cache.js +++ b/src/util/lru_cache.js @@ -110,6 +110,21 @@ class LRUCache { return data; } + /** + * Get the value attached to a specific key without removing data + * from the cache. If the key is not found, returns `null` + * + * @param {string} key the key to look up + * @returns {*} the data, or null if it isn't found + * @private + */ + getWithoutRemoving(key: string): ?T { + if (!this.has(key)) { return null; } + + const data = this.data[key]; + return data; + } + /** * Remove a key/value combination from the cache. * diff --git a/test/unit/source/source_cache.test.js b/test/unit/source/source_cache.test.js index f4a603904f4..9b79f49b06a 100644 --- a/test/unit/source/source_cache.test.js +++ b/test/unit/source/source_cache.test.js @@ -871,7 +871,7 @@ test('SourceCache#findLoadedParent', (t) => { t.equal(sourceCache.findLoadedParent(new TileCoord(2, 3, 3), 0, retain), undefined); t.equal(sourceCache.findLoadedParent(new TileCoord(2, 0, 0), 0, retain), tile); t.deepEqual(retain, expectedRetain); - t.equal(sourceCache._cache.order.length, 0); + t.equal(sourceCache._cache.order.length, 1); t.end(); }); diff --git a/test/unit/util/lru_cache.test.js b/test/unit/util/lru_cache.test.js index 8ebb2e38c03..a1e66d6c52b 100644 --- a/test/unit/util/lru_cache.test.js +++ b/test/unit/util/lru_cache.test.js @@ -18,6 +18,16 @@ test('LRUCache', (t) => { t.end(); }); +test('LRUCache - getWithoutRemoving', (t) => { + const cache = new LRUCache(10, () => { + t.fail(); + }); + t.equal(cache.add('washington', 'dc'), cache, '.add()'); + t.equal(cache.getWithoutRemoving('washington'), 'dc', '.getWithoutRemoving()'); + t.deepEqual(cache.keys(), ['washington'], '.keys()'); + t.end(); +}); + test('LRUCache - duplicate add', (t) => { const cache = new LRUCache(10, () => { t.fail();