Skip to content

Commit

Permalink
minor cleanup (fixup into refactor)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Apr 16, 2018
1 parent c0bf558 commit ab2e59c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { create as createSource } from './source';

import Tile from './tile';
import { Event, ErrorEvent, Evented } from '../util/evented';
import Cache from './tile_cache';
import TileCache from './tile_cache';
import Coordinate from '../geo/coordinate';
import { keysDifference } from '../util/util';
import EXTENT from '../data/extent';
Expand Down Expand Up @@ -44,7 +44,7 @@ class SourceCache extends Evented {
_sourceErrored: boolean;
_tiles: {[any]: Tile};
_prevLng: number | void;
_cache: Cache<Tile>;
_cache: TileCache;
_timers: {[any]: TimeoutID};
_cacheTimers: {[any]: TimeoutID};
_maxTileCacheSize: ?number;
Expand Down Expand Up @@ -86,7 +86,7 @@ class SourceCache extends Evented {
this._source = createSource(id, options, dispatcher, this);

this._tiles = {};
this._cache = new Cache(0, this._unloadTile.bind(this));
this._cache = new TileCache(0, this._unloadTile.bind(this));
this._timers = {};
this._cacheTimers = {};
this._maxTileCacheSize = null;
Expand Down
33 changes: 12 additions & 21 deletions src/source/tile_cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import { OverscaledTileID } from './tile_id';
import type Tile from './tile';

/**
* A [least-recently-used cache](http://en.wikipedia.org/wiki/Cache_algorithms)
Expand All @@ -9,16 +10,16 @@ import { OverscaledTileID } from './tile_id';
*
* @private
*/
class TileCache<T> {
class TileCache {
max: number;
data: {[key: number | string]: Array<{ value: T, timeout: ?TimeoutID}>};
data: {[key: number | string]: Array<{ value: Tile, timeout: ?TimeoutID}>};
order: Array<number>;
onRemove: (element: T) => void;
onRemove: (element: Tile) => void;
/**
* @param {number} max number of permitted values
* @param {Function} onRemove callback called with items when they expire
*/
constructor(max: number, onRemove: (element: T) => void) {
constructor(max: number, onRemove: (element: Tile) => void) {
this.max = max;
this.onRemove = onRemove;
this.reset();
Expand Down Expand Up @@ -54,7 +55,7 @@ class TileCache<T> {
* @returns {TileCache} this cache
* @private
*/
add(tileID: OverscaledTileID, data: T, expiryTimeout: number | void) {
add(tileID: OverscaledTileID, data: Tile, expiryTimeout: number | void) {
const key = tileID.wrapped().key;
if (this.data[key] === undefined) {
this.data[key] = [];
Expand Down Expand Up @@ -93,16 +94,6 @@ class TileCache<T> {
return tileID.wrapped().key in this.data;
}

/**
* List all keys in the cache
*
* @returns {Array<number>} an array of keys in this cache.
* @private
*/
keys(): Array<number> {
return this.order;
}

/**
* Get the value attached to a specific key and remove data from cache.
* If the key is not found, returns `null`
Expand All @@ -111,15 +102,15 @@ class TileCache<T> {
* @returns {*} the data, or null if it isn't found
* @private
*/
getAndRemove(tileID: OverscaledTileID): ?T {
getAndRemove(tileID: OverscaledTileID): ?Tile {
if (!this.has(tileID)) { return null; }
return this._getAndRemoveByKey(tileID.wrapped().key);
}

/*
* Get and remove the value with the specified key.
*/
_getAndRemoveByKey(key: number): ?T {
_getAndRemoveByKey(key: number): ?Tile {
const data = this.data[key].shift();
if (data.timeout) clearTimeout(data.timeout);

Expand All @@ -139,7 +130,7 @@ class TileCache<T> {
* @returns {*} the data, or null if it isn't found
* @private
*/
get(tileID: OverscaledTileID): ?T {
get(tileID: OverscaledTileID): ?Tile {
if (!this.has(tileID)) { return null; }

const data = this.data[tileID.wrapped().key][0];
Expand All @@ -150,11 +141,11 @@ class TileCache<T> {
* Remove a key/value combination from the cache.
*
* @param {OverscaledTileID} tileID the key for the pair to delete
* @param {T} value If a value is provided, remove that exact version of the value.
* @param {Tile} value If a value is provided, remove that exact version of the value.
* @returns {TileCache} this cache
* @private
*/
remove(tileID: OverscaledTileID, value: ?{ value: T, timeout: ?TimeoutID}) {
remove(tileID: OverscaledTileID, value: ?{ value: Tile, timeout: ?TimeoutID}) {
if (!this.has(tileID)) { return this; }
const key = tileID.wrapped().key;

Expand All @@ -178,7 +169,7 @@ class TileCache<T> {
* @returns {TileCache} this cache
* @private
*/
setMaxSize(max: number): TileCache<T> {
setMaxSize(max: number): TileCache {
this.max = max;

while (this.order.length > this.max) {
Expand Down

0 comments on commit ab2e59c

Please sign in to comment.