Extending FeatureLoader type and refactoring loadFeaturesXhr

This commit is contained in:
Simon Seyock
2020-10-06 10:28:07 +02:00
parent 1ec6713f31
commit 393e83cd29
2 changed files with 123 additions and 102 deletions
+47 -37
View File
@@ -16,15 +16,19 @@ let withCredentials = false;
* load features. * load features.
* *
* This function takes an {@link module:ol/extent~Extent} representing the area to be loaded, * This function takes an {@link module:ol/extent~Extent} representing the area to be loaded,
* a `{number}` representing the resolution (map units per pixel) and an * a `{number}` representing the resolution (map units per pixel), an
* {@link module:ol/proj/Projection} for the projection as * {@link module:ol/proj/Projection} for the projection and success and failure callbacks as
* arguments. `this` within the function is bound to the * arguments. `this` within the function is bound to the
* {@link module:ol/source/Vector} it's called from. * {@link module:ol/source/Vector} it's called from.
* *
* The function is responsible for loading the features and adding them to the * The function is responsible for loading the features and adding them to the
* source. * source.
* @typedef {function(this:(import("./source/Vector").default|import("./VectorTile.js").default), import("./extent.js").Extent, number, * @typedef {function(this:(import("./source/Vector").default|import("./VectorTile.js").default),
* import("./proj/Projection.js").default): void} FeatureLoader * import("./extent.js").Extent,
* number,
* import("./proj/Projection.js").default,
* function(): void=,
* function(): void=): void} FeatureLoader
* @api * @api
*/ */
@@ -43,23 +47,23 @@ let withCredentials = false;
/** /**
* @param {string|FeatureUrlFunction} url Feature URL service. * @param {string|FeatureUrlFunction} url Feature URL service.
* @param {import("./format/Feature.js").default} format Feature format. * @param {import("./format/Feature.js").default} format Feature format.
* @param {function(this:import("./VectorTile.js").default, Array<import("./Feature.js").default>, import("./proj/Projection.js").default, import("./extent.js").Extent): void|function(this:import("./source/Vector").default, Array<import("./Feature.js").default>): void} success
* Function called with the loaded features and optionally with the data
* projection. Called with the vector tile or source as `this`.
* @param {function(this:import("./VectorTile.js").default): void|function(this:import("./source/Vector").default): void} failure
* Function called when loading failed. Called with the vector tile or
* source as `this`.
* @return {FeatureLoader} The feature loader.
*/
export function loadFeaturesXhr(url, format, success, failure) {
return (
/**
* @param {import("./extent.js").Extent} extent Extent. * @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection. * @param {import("./proj/Projection.js").default} projection Projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default} * @param {function(Array<import("./Feature.js").default>, import("./proj/Projection.js").default): void} success Success
* Function called with the loaded features and optionally with the data projection.
* @param {function(): void} failure Failure
* Function called when loading failed.
*/ */
function (extent, resolution, projection) { export function loadFeaturesXhr(
url,
format,
extent,
resolution,
projection,
success,
failure
) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open( xhr.open(
'GET', 'GET',
@@ -94,30 +98,26 @@ export function loadFeaturesXhr(url, format, success, failure) {
source = /** @type {ArrayBuffer} */ (xhr.response); source = /** @type {ArrayBuffer} */ (xhr.response);
} }
if (source) { if (source) {
success.call( success(
this, /** @type {Array<import("./Feature.js").default>} */
format.readFeatures(source, { (format.readFeatures(source, {
extent: extent, extent: extent,
featureProjection: projection, featureProjection: projection,
}), })),
format.readProjection(source) format.readProjection(source)
); );
} else { } else {
failure.call(this); failure();
} }
} else { } else {
failure.call(this); failure();
} }
}.bind(this); };
/** /**
* @private * @private
*/ */
xhr.onerror = function () { xhr.onerror = failure;
failure.call(this);
}.bind(this);
xhr.send(); xhr.send();
}
);
} }
/** /**
@@ -130,25 +130,35 @@ export function loadFeaturesXhr(url, format, success, failure) {
* @api * @api
*/ */
export function xhr(url, format) { export function xhr(url, format) {
return loadFeaturesXhr( /**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @param {function(): void} success Success
* Function called when loading succeeded.
* @param {function(): void} failure Failure
* Function called when loading failed.
* @this {import("./source/Vector").default}
*/
return function (extent, resolution, projection, success, failure) {
const sourceOrTile = /** @type {import("./source/Vector").default} */ (this);
loadFeaturesXhr(
url, url,
format, format,
extent,
resolution,
projection,
/** /**
* @param {Array<import("./Feature.js").default>} features The loaded features. * @param {Array<import("./Feature.js").default>} features The loaded features.
* @param {import("./proj/Projection.js").default} dataProjection Data * @param {import("./proj/Projection.js").default} dataProjection Data
* projection. * projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default}
*/ */
function (features, dataProjection) { function (features, dataProjection) {
const sourceOrTile = /** @type {?} */ (this); sourceOrTile.addFeatures(features);
if (typeof sourceOrTile.addFeatures === 'function') {
/** @type {import("./source/Vector").default} */ (sourceOrTile).addFeatures(
features
);
}
}, },
/* FIXME handle error */ VOID /* FIXME handle error */ VOID
); );
};
} }
/** /**
+13 -2
View File
@@ -528,11 +528,22 @@ export default VectorTile;
* @param {string} url URL. * @param {string} url URL.
*/ */
export function defaultLoadFunction(tile, url) { export function defaultLoadFunction(tile, url) {
const loader = loadFeaturesXhr( tile.setLoader(
/**
* @param {import("../extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("../proj/Projection.js").default} projection Projection.
*/
function (extent, resolution, projection) {
loadFeaturesXhr(
url, url,
tile.getFormat(), tile.getFormat(),
extent,
resolution,
projection,
tile.onLoad.bind(tile), tile.onLoad.bind(tile),
tile.onError.bind(tile) tile.onError.bind(tile)
); );
tile.setLoader(loader); }
);
} }