Replace instanceof checks with other logic

This commit is contained in:
ahocevar
2018-10-05 16:16:51 +02:00
parent 96e99e481e
commit 9163558511
40 changed files with 232 additions and 268 deletions

View File

@@ -5,8 +5,6 @@ import {assert} from './asserts.js';
import {listen, unlisten, unlistenByKey} from './events.js';
import EventType from './events/EventType.js';
import BaseObject, {getChangeEventType} from './Object.js';
import Geometry from './geom/Geometry.js';
import Style from './style/Style.js';
/**
* @typedef {typeof Feature|typeof import("./render/Feature.js").default} FeatureClass
@@ -62,7 +60,7 @@ import Style from './style/Style.js';
*/
class Feature extends BaseObject {
/**
* @param {Geometry|Object<string, *>=} opt_geometryOrProperties
* @param {import("./geom/Geometry.js").default|Object<string, *>=} opt_geometryOrProperties
* You may pass a Geometry object directly, or an object literal containing
* properties. If you pass an object literal, you may include a Geometry
* associated with a `geometry` key.
@@ -86,7 +84,7 @@ class Feature extends BaseObject {
/**
* User provided style.
* @private
* @type {Style|Array<Style>|import("./style/Style.js").StyleFunction}
* @type {import("./style/Style.js").StyleLike}
*/
this.style_ = null;
@@ -106,10 +104,9 @@ class Feature extends BaseObject {
this, getChangeEventType(this.geometryName_),
this.handleGeometryChanged_, this);
if (opt_geometryOrProperties !== undefined) {
if (opt_geometryOrProperties instanceof Geometry) {
const geometry = /** @type {Geometry} */ (opt_geometryOrProperties);
if (opt_geometryOrProperties) {
if (typeof /** @type {?} */ (opt_geometryOrProperties).getSimplifiedGeometry === 'function') {
const geometry = /** @type {import("./geom/Geometry.js").default} */ (opt_geometryOrProperties);
this.setGeometry(geometry);
} else {
/** @type {Object<string, *>} */
@@ -143,13 +140,13 @@ class Feature extends BaseObject {
* Get the feature's default geometry. A feature may have any number of named
* geometries. The "default" geometry (the one that is rendered by default) is
* set when calling {@link module:ol/Feature~Feature#setGeometry}.
* @return {Geometry|undefined} The default geometry for the feature.
* @return {import("./geom/Geometry.js").default|undefined} The default geometry for the feature.
* @api
* @observable
*/
getGeometry() {
return (
/** @type {Geometry|undefined} */ (this.get(this.geometryName_))
/** @type {import("./geom/Geometry.js").default|undefined} */ (this.get(this.geometryName_))
);
}
@@ -178,7 +175,7 @@ class Feature extends BaseObject {
/**
* Get the feature's style. Will return what was provided to the
* {@link module:ol/Feature~Feature#setStyle} method.
* @return {Style|Array<Style>|import("./style/Style.js").StyleFunction} The feature style.
* @return {import("./style/Style.js").StyleLike} The feature style.
* @api
*/
getStyle() {
@@ -221,7 +218,7 @@ class Feature extends BaseObject {
/**
* Set the default geometry for the feature. This will update the property
* with the name returned by {@link module:ol/Feature~Feature#getGeometryName}.
* @param {Geometry|undefined} geometry The new geometry.
* @param {import("./geom/Geometry.js").default|undefined} geometry The new geometry.
* @api
* @observable
*/
@@ -233,7 +230,7 @@ class Feature extends BaseObject {
* Set the style for the feature. This can be a single style object, an array
* of styles, or a function that takes a resolution and returns an array of
* styles. If it is `null` the feature has no style (a `null` style).
* @param {Style|Array<Style>|import("./style/Style.js").StyleFunction} style Style for this feature.
* @param {import("./style/Style.js").StyleLike} style Style for this feature.
* @api
* @fires module:ol/events/Event~Event#event:change
*/
@@ -281,7 +278,7 @@ class Feature extends BaseObject {
* Convert the provided object into a feature style function. Functions passed
* through unchanged. Arrays of Style or single style objects wrapped
* in a new feature style function.
* @param {import("./style/Style.js").StyleFunction|!Array<Style>|!Style} obj
* @param {!import("./style/Style.js").StyleFunction|!Array<import("./style/Style.js").default>|!import("./style/Style.js").default} obj
* A feature style function, a single style, or an array of styles.
* @return {import("./style/Style.js").StyleFunction} A style function.
*/
@@ -290,15 +287,16 @@ export function createStyleFunction(obj) {
return obj;
} else {
/**
* @type {Array<Style>}
* @type {Array<import("./style/Style.js").default>}
*/
let styles;
if (Array.isArray(obj)) {
styles = obj;
} else {
assert(obj instanceof Style,
assert(typeof /** @type {?} */ (obj).getZIndex === 'function',
41); // Expected an `import("./style/Style.js").Style` or an array of `import("./style/Style.js").Style`
styles = [obj];
const style = /** @type {import("./style/Style.js").default} */ (obj);
styles = [style];
}
return function() {
return styles;