Skip to content

Commit

Permalink
Merge pull request #15780 from sebakerckhof/fix/featureloader-types
Browse files Browse the repository at this point in the history
Add feature type generic to feature loader types
  • Loading branch information
ahocevar committed May 6, 2024
2 parents f11c760 + d3687ce commit cbe7677
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/ol/featureloader.js
Expand Up @@ -24,11 +24,13 @@ let withCredentials = false;
*
* The function is responsible for loading the features and adding them to the
* source.
*
* @template {import("./Feature.js").FeatureLike} [FeatureType=import("./Feature.js").default]
* @typedef {function(this:(import("./source/Vector").default|import("./VectorTile.js").default),
* import("./extent.js").Extent,
* number,
* import("./proj/Projection.js").default,
* function(Array<import("./Feature.js").default>): void=,
* function(Array<FeatureType>): void=,
* function(): void=): void} FeatureLoader
* @api
*/
Expand All @@ -46,12 +48,13 @@ let withCredentials = false;
*/

/**
* @template {import("./Feature.js").FeatureLike} [FeatureType=import("./Feature.js").FeatureLike]
* @param {string|FeatureUrlFunction} url Feature URL service.
* @param {import("./format/Feature.js").default} format Feature format.
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @param {function(Array<import("./Feature.js").default>, import("./proj/Projection.js").default): void} success Success
* @param {function(Array<FeatureType>, 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.
Expand Down Expand Up @@ -102,7 +105,7 @@ export function loadFeaturesXhr(
}
if (source) {
success(
/** @type {Array<import("./Feature.js").default>} */
/** @type {Array<FeatureType>} */
(
format.readFeatures(source, {
extent: extent,
Expand All @@ -129,31 +132,33 @@ export function loadFeaturesXhr(
* Create an XHR feature loader for a `url` and `format`. The feature loader
* loads features (with XHR), parses the features, and adds them to the
* vector source.
* @template {import("./Feature.js").FeatureLike} [FeatureType=import("./Feature.js").FeatureLike]
* @param {string|FeatureUrlFunction} url Feature URL service.
* @param {import("./format/Feature.js").default<typeof import("./Feature.js").default|typeof import("./render/Feature.js").default>} format Feature format.
* @return {FeatureLoader} The feature loader.
* @param {import("./format/Feature.js").default<import('./format/Feature.js').FeatureToFeatureClass<FeatureType>>} format Feature format.
* @return {FeatureLoader<FeatureType>} The feature loader.
* @api
*/
export function xhr(url, format) {
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @param {function(Array<import("./Feature.js").default>): void} [success] Success
* @param {function(Array<FeatureType>): void} [success] Success
* Function called when loading succeeded.
* @param {function(): void} [failure] Failure
* Function called when loading failed.
*/
return function (extent, resolution, projection, success, failure) {
const source = /** @type {import("./source/Vector").default} */ (this);
const source =
/** @type {import("./source/Vector").default<FeatureType>} */ (this);
loadFeaturesXhr(
url,
format,
extent,
resolution,
projection,
/**
* @param {Array<import("./Feature.js").default>} features The loaded features.
* @param {Array<FeatureType>} features The loaded features.
* @param {import("./proj/Projection.js").default} dataProjection Data
* projection.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/ol/source/Vector.js
Expand Up @@ -85,7 +85,7 @@ export class VectorSourceEvent extends Event {
* and the collection will stay in sync.
* @property {import("../format/Feature.js").default<import("../format/Feature.js").FeatureToFeatureClass<FeatureType>>} [format] The feature format used by the XHR
* feature loader when `url` is set. Required if `url` is set, otherwise ignored.
* @property {import("../featureloader.js").FeatureLoader} [loader]
* @property {import("../featureloader.js").FeatureLoader<FeatureType>} [loader]
* The loader function used to load features, from a remote source for example.
* If this is not set and `url` is set, the source will create and use an XHR
* feature loader. The `'featuresloadend'` and `'featuresloaderror'` events
Expand Down Expand Up @@ -211,7 +211,7 @@ class VectorSource extends Source {

/**
* @private
* @type {import("../featureloader.js").FeatureLoader}
* @type {import("../featureloader.js").FeatureLoader<FeatureType>}
*/
this.loader_ = VOID;

Expand Down Expand Up @@ -1171,7 +1171,7 @@ class VectorSource extends Source {
/**
* Set the new loader of the source. The next render cycle will use the
* new loader.
* @param {import("../featureloader.js").FeatureLoader} loader The loader to set.
* @param {import("../featureloader.js").FeatureLoader<FeatureType>} loader The loader to set.
* @api
*/
setLoader(loader) {
Expand Down

0 comments on commit cbe7677

Please sign in to comment.