Merge pull request #8787 from ahocevar/no-instanceof

Replace instanceof checks with other logic
This commit is contained in:
Andreas Hocevar
2018-10-12 20:44:41 +02:00
committed by GitHub
40 changed files with 232 additions and 268 deletions

View File

@@ -5,6 +5,7 @@
import {getUid} from '../util.js';
import {assert} from '../asserts.js';
import Feature from '../Feature.js';
import GeometryType from '../geom/GeometryType.js';
import {scale as scaleCoordinate, add as addCoordinate} from '../coordinate.js';
import {listen} from '../events.js';
import EventType from '../events/EventType.js';
@@ -76,7 +77,7 @@ class Cluster extends VectorSource {
*/
this.geometryFunction = options.geometryFunction || function(feature) {
const geometry = /** @type {Point} */ (feature.getGeometry());
assert(geometry instanceof Point,
assert(geometry.getType() == GeometryType.POINT,
10); // The default `geometryFunction` can only handle `Point` geometries
return geometry;
};

View File

@@ -230,10 +230,7 @@ class ImageSource extends Source {
* @param {string} src Source.
*/
export function defaultImageLoadFunction(image, src) {
const img = image.getImage();
if (img instanceof HTMLImageElement || img instanceof HTMLVideoElement) {
img.src = src;
}
/** @type {HTMLImageElement|HTMLVideoElement} */ (image.getImage()).src = src;
}

View File

@@ -11,7 +11,6 @@ import EventType from '../events/EventType.js';
import {Processor} from 'pixelworks/lib/index';
import {equals, getCenter, getHeight, getWidth} from '../extent.js';
import LayerType from '../LayerType.js';
import Layer from '../layer/Layer.js';
import ImageLayer from '../layer/Image.js';
import TileLayer from '../layer/Tile.js';
import {assign} from '../obj.js';
@@ -19,7 +18,6 @@ import CanvasImageLayerRenderer from '../renderer/canvas/ImageLayer.js';
import CanvasTileLayerRenderer from '../renderer/canvas/TileLayer.js';
import ImageSource from '../source/Image.js';
import SourceState from '../source/State.js';
import TileSource from '../source/Tile.js';
import {create as createTransform} from '../transform.js';
@@ -488,20 +486,22 @@ function createRenderers(sources) {
/**
* Create a renderer for the provided source.
* @param {import("./Source.js").default|import("../layer/Layer.js").default} source The source.
* @param {import("./Source.js").default|import("../layer/Layer.js").default} layerOrSource The layer or source.
* @return {import("../renderer/canvas/Layer.js").default} The renderer.
*/
function createRenderer(source) {
function createRenderer(layerOrSource) {
const tileSource = /** @type {import("./Tile.js").default} */ (layerOrSource);
const imageSource = /** @type {import("./Image.js").default} */ (layerOrSource);
const layer = /** @type {import("../layer/Layer.js").default} */ (layerOrSource);
let renderer = null;
if (source instanceof TileSource) {
renderer = createTileRenderer(source);
} else if (source instanceof ImageSource) {
renderer = createImageRenderer(source);
} else if (source instanceof TileLayer) {
renderer = new CanvasTileLayerRenderer(source);
} else if (source instanceof Layer &&
(source.getType() == LayerType.IMAGE || source.getType() == LayerType.VECTOR)) {
renderer = new CanvasImageLayerRenderer(source);
if (typeof tileSource.getTile === 'function') {
renderer = createTileRenderer(tileSource);
} else if (typeof imageSource.getImage === 'function') {
renderer = createImageRenderer(imageSource);
} else if (layer.getType() === LayerType.TILE) {
renderer = new CanvasTileLayerRenderer(/** @type {import("../layer/Tile.js").default} */ (layer));
} else if (layer.getType() == LayerType.IMAGE || layer.getType() == LayerType.VECTOR) {
renderer = new CanvasImageLayerRenderer(/** @type {import("../layer/Image.js").default} */ (layer));
}
return renderer;
}

View File

@@ -392,10 +392,7 @@ class TileImage extends UrlTile {
* @param {string} src Source.
*/
function defaultTileLoadFunction(imageTile, src) {
const image = imageTile.getImage();
if (image instanceof HTMLImageElement || image instanceof HTMLVideoElement) {
image.src = src;
}
/** @type {HTMLImageElement|HTMLVideoElement} */ (imageTile.getImage()).src = src;
}
export default TileImage;

View File

@@ -258,11 +258,11 @@ class VectorSource extends Source {
this.featuresCollection_ = null;
let collection, features;
if (options.features instanceof Collection) {
if (Array.isArray(options.features)) {
features = options.features;
} else if (options.features) {
collection = options.features;
features = collection.getArray();
} else if (Array.isArray(options.features)) {
features = options.features;
}
if (!useSpatialIndex && collection === undefined) {
collection = new Collection(features);