Merge pull request #1296 from twpayne/improve-type-checking

Improve type checking
This commit is contained in:
Tom Payne
2013-11-20 02:27:42 -08:00
15 changed files with 71 additions and 39 deletions

View File

@@ -75,7 +75,7 @@ ol.Collection = function(opt_array) {
/**
* @private
* @type {Array}
* @type {Array.<*>}
*/
this.array_ = opt_array || [];
@@ -112,7 +112,7 @@ ol.Collection.prototype.extend = function(arr) {
/**
* Iterate over each element, calling the provided callback.
* @param {function(this: S, T, number, Array.<T>): ?} f The function to call
* @param {function(this: S, T, number, Array.<T>): *} f The function to call
* for every element. This function takes 3 arguments (the element, the
* index and the array). The return value is ignored.
* @param {S=} opt_obj The object to be used as the value of 'this' within f.

View File

@@ -111,7 +111,8 @@ ol.renderer.canvas.Map.prototype.renderFrame = function(frameState) {
for (i = 0, ii = layersArray.length; i < ii; ++i) {
layer = layersArray[i];
layerRenderer = this.getLayerRenderer(layer);
layerRenderer =
/** @type {ol.renderer.canvas.Layer} */ (this.getLayerRenderer(layer));
layerState = layerStates[goog.getUid(layer)];
if (!layerState.visible ||
layerState.sourceState != ol.source.State.READY ||
@@ -133,6 +134,8 @@ ol.renderer.canvas.Map.prototype.renderFrame = function(frameState) {
var dy = goog.vec.Mat4.getElement(transform, 1, 3);
var dw = image.width * goog.vec.Mat4.getElement(transform, 0, 0);
var dh = image.height * goog.vec.Mat4.getElement(transform, 1, 1);
goog.asserts.assert(goog.isNumber(image.width));
goog.asserts.assert(goog.isNumber(image.height));
context.drawImage(image, 0, 0, image.width, image.height,
Math.round(dx), Math.round(dy), Math.round(dw), Math.round(dh));
} else {

View File

@@ -28,7 +28,6 @@ goog.inherits(ol.renderer.dom.Layer, ol.renderer.Layer);
/**
* @protected
* @return {!Element} Target.
*/
ol.renderer.dom.Layer.prototype.getTarget = function() {

View File

@@ -93,7 +93,8 @@ ol.renderer.dom.Map.prototype.renderFrame = function(frameState) {
var layerKey;
for (layerKey in this.getLayerRenderers()) {
if (!(layerKey in layerStates)) {
layerRenderer = this.getLayerRendererByKey(layerKey);
layerRenderer = /** @type {ol.renderer.dom.Layer} */
(this.getLayerRendererByKey(layerKey));
goog.dom.removeNode(layerRenderer.getTarget());
}
}

View File

@@ -383,6 +383,7 @@ ol.renderer.dom.TileLayerZ_.prototype.removeTilesOutsideExtent =
function(extent, opt_tileRange) {
var tileRange = this.tileGrid_.getTileRangeForExtentAndZ(
extent, this.tileCoordOrigin_.z, opt_tileRange);
/** @type {Array.<ol.Tile>} */
var tilesToRemove = [];
var tile, tileCoordKey;
for (tileCoordKey in this.tiles_) {

View File

@@ -129,10 +129,16 @@ ol.renderer.Layer.prototype.scheduleExpireCache =
function(frameState, tileSource) {
if (tileSource.canExpireCache()) {
frameState.postRenderFunctions.push(
goog.partial(function(tileSource, map, frameState) {
var tileSourceKey = goog.getUid(tileSource).toString();
tileSource.expireCache(frameState.usedTiles[tileSourceKey]);
}, tileSource));
goog.partial(
/**
* @param {ol.source.Tile} tileSource Tile source.
* @param {ol.Map} map Map.
* @param {ol.FrameState} frameState Frame state.
*/
function(tileSource, map, frameState) {
var tileSourceKey = goog.getUid(tileSource).toString();
tileSource.expireCache(frameState.usedTiles[tileSourceKey]);
}, tileSource));
}
};

View File

@@ -114,11 +114,16 @@ ol.renderer.webgl.ImageLayer.prototype.renderFrame =
texture = this.createTexture_(image_);
if (!goog.isNull(this.texture)) {
frameState.postRenderFunctions.push(
goog.partial(function(gl, texture) {
if (!gl.isContextLost()) {
gl.deleteTexture(texture);
}
}, gl, this.texture));
goog.partial(
/**
* @param {WebGLRenderingContext} gl GL.
* @param {WebGLTexture} texture Texture.
*/
function(gl, texture) {
if (!gl.isContextLost()) {
gl.deleteTexture(texture);
}
}, gl, this.texture));
}
}
}

View File

@@ -124,12 +124,18 @@ ol.renderer.webgl.Layer.prototype.bindFramebuffer =
this.framebufferDimension != framebufferDimension) {
frameState.postRenderFunctions.push(
goog.partial(function(gl, framebuffer, texture) {
if (!gl.isContextLost()) {
gl.deleteFramebuffer(framebuffer);
gl.deleteTexture(texture);
}
}, gl, this.framebuffer, this.texture));
goog.partial(
/**
* @param {WebGLRenderingContext} gl GL.
* @param {WebGLFramebuffer} framebuffer Framebuffer.
* @param {WebGLTexture} texture Texture.
*/
function(gl, framebuffer, texture) {
if (!gl.isContextLost()) {
gl.deleteFramebuffer(framebuffer);
gl.deleteTexture(texture);
}
}, gl, this.framebuffer, this.texture));
var texture = gl.createTexture();
gl.bindTexture(goog.webgl.TEXTURE_2D, texture);

View File

@@ -279,6 +279,9 @@ ol.renderer.webgl.TileLayer.prototype.renderFrame =
this.manageTilePyramid(
frameState, tileSource, tileGrid, projection, extent, z,
tileLayer.getPreload(),
/**
* @param {ol.Tile} tile Tile.
*/
function(tile) {
if (tile.getState() == ol.TileState.LOADED &&
!mapRenderer.isTileTextureLoaded(tile) &&

View File

@@ -61,7 +61,8 @@ ol.DebugTile_.prototype.getImage = function(opt_context) {
canvas.width = tileSize[0];
canvas.height = tileSize[1];
var context = canvas.getContext('2d');
var context = /** @type {CanvasRenderingContext2D} */
(canvas.getContext('2d'));
context.strokeStyle = 'black';
context.strokeRect(0.5, 0.5, tileSize[0] + 0.5, tileSize[1] + 0.5);

View File

@@ -10,6 +10,7 @@ goog.require('goog.object');
* Object's properties (e.g. 'hasOwnProperty' is not allowed as a key). Expiring
* items from the cache is the responsibility of the user.
* @constructor
* @template T
*/
ol.structs.LRUCache = function() {
@@ -96,7 +97,7 @@ ol.structs.LRUCache.prototype.containsKey = function(key) {
/**
* @param {function(this: S, *, string, ol.structs.LRUCache): ?} f The function
* @param {function(this: S, T, string, ol.structs.LRUCache): ?} f The function
* to call for every entry from the oldest to the newer. This function takes
* 3 arguments (the entry value, the entry key and the LRUCache object).
* The return value is ignored.
@@ -114,7 +115,7 @@ ol.structs.LRUCache.prototype.forEach = function(f, opt_obj) {
/**
* @param {string} key Key.
* @return {*} Value.
* @return {T} Value.
*/
ol.structs.LRUCache.prototype.get = function(key) {
var entry = this.entries_[key];
@@ -175,7 +176,7 @@ ol.structs.LRUCache.prototype.getValues = function() {
/**
* @return {*} Last value.
* @return {T} Last value.
*/
ol.structs.LRUCache.prototype.peekLast = function() {
goog.asserts.assert(!goog.isNull(this.oldest_));
@@ -193,7 +194,7 @@ ol.structs.LRUCache.prototype.peekLastKey = function() {
/**
* @return {*} value Value.
* @return {T} value Value.
*/
ol.structs.LRUCache.prototype.pop = function() {
goog.asserts.assert(!goog.isNull(this.oldest_));
@@ -215,7 +216,7 @@ ol.structs.LRUCache.prototype.pop = function() {
/**
* @param {string} key Key.
* @param {*} value Value.
* @param {T} value Value.
*/
ol.structs.LRUCache.prototype.set = function(key, value) {
goog.asserts.assert(!(key in {}));

View File

@@ -15,25 +15,26 @@ goog.require('goog.object');
* @see http://hg.python.org/cpython/file/2.7/Lib/heapq.py
*
* @constructor
* @param {function(?): number} priorityFunction Priority function.
* @param {function(?): string} keyFunction Key function.
* @param {function(T): number} priorityFunction Priority function.
* @param {function(T): string} keyFunction Key function.
* @template T
*/
ol.structs.PriorityQueue = function(priorityFunction, keyFunction) {
/**
* @type {function(?): number}
* @type {function(T): number}
* @private
*/
this.priorityFunction_ = priorityFunction;
/**
* @type {function(?): string}
* @type {function(T): string}
* @private
*/
this.keyFunction_ = keyFunction;
/**
* @type {Array}
* @type {Array.<T>}
* @private
*/
this.elements_ = [];
@@ -88,7 +89,7 @@ ol.structs.PriorityQueue.prototype.clear = function() {
/**
* Remove and return the highest-priority element. O(log N).
* @return {*} Element.
* @return {T} Element.
*/
ol.structs.PriorityQueue.prototype.dequeue = function() {
var elements = this.elements_;
@@ -112,7 +113,7 @@ ol.structs.PriorityQueue.prototype.dequeue = function() {
/**
* Enqueue an element. O(log N).
* @param {*} element Element.
* @param {T} element Element.
*/
ol.structs.PriorityQueue.prototype.enqueue = function(element) {
goog.asserts.assert(!(this.keyFunction_(element) in this.queuedElements_));
@@ -197,7 +198,7 @@ ol.structs.PriorityQueue.prototype.isKeyQueued = function(key) {
/**
* @param {*} element Element.
* @param {T} element Element.
* @return {boolean} Is queued.
*/
ol.structs.PriorityQueue.prototype.isQueued = function(element) {

View File

@@ -16,7 +16,7 @@ ol.DEFAULT_TILE_CACHE_HIGH_WATER_MARK = 2048;
/**
* @constructor
* @extends {ol.structs.LRUCache}
* @extends {ol.structs.LRUCache.<ol.Tile>}
* @param {number=} opt_highWaterMark High water mark.
*/
ol.TileCache = function(opt_highWaterMark) {
@@ -49,9 +49,6 @@ ol.TileCache.prototype.expireCache = function(usedTiles) {
var tile, zKey;
while (this.canExpireCache()) {
tile = /** @type {ol.Tile} */ (this.peekLast());
// TODO: Enforce ol.Tile in ol.TileCache#set
goog.asserts.assert(tile instanceof ol.Tile,
'ol.TileCache#expireCache only works with ol.Tile values.');
zKey = tile.tileCoord.z.toString();
if (zKey in usedTiles && usedTiles[zKey].contains(tile.tileCoord)) {
break;

View File

@@ -17,7 +17,7 @@ ol.TilePriorityFunction;
/**
* @constructor
* @extends {ol.structs.PriorityQueue}
* @extends {ol.structs.PriorityQueue.<Array>}
* @param {ol.TilePriorityFunction} tilePriorityFunction
* Tile priority function.
* @param {function(): ?} tileChangeCallback