Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect static image loaders by arguments length #15039

Merged
merged 1 commit into from Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ol/Image.js
Expand Up @@ -37,7 +37,8 @@ import {toPromise} from './functions.js';

/**
* Loader function used for image sources. Receives extent, resolution and pixel ratio as arguments.
* The function returns an {@link import("./DataTile.js").ImageLike image}, an
* For images that cover any extent and resolution (static images), the loader function should not accept
* any arguments. The function returns an {@link import("./DataTile.js").ImageLike image}, an
* {@link import("./Image.js").ImageObject image object}, or a promise for the same.
* For loaders that generate images, the promise should not resolve until the image is loaded.
* If the returned image does not match the extent, resolution or pixel ratio passed to the loader,
Expand Down
27 changes: 17 additions & 10 deletions src/ol/source/Image.js
Expand Up @@ -172,6 +172,12 @@ class ImageSource extends Source {
* @type {number}
*/
this.wantedResolution_;

/**
* @private
* @type {boolean}
*/
this.static_ = options.loader ? options.loader.length === 0 : false;
}

/**
Expand Down Expand Up @@ -263,18 +269,19 @@ class ImageSource extends Source {
if (this.loader) {
const requestExtent = getRequestExtent(extent, resolution, pixelRatio, 1);
const requestResolution = this.findNearestResolution(resolution);
if (this.image) {
if (
((this.wantedExtent_ &&
if (
this.image &&
(this.static_ ||
(((this.wantedExtent_ &&
containsExtent(this.wantedExtent_, requestExtent)) ||
containsExtent(this.image.getExtent(), requestExtent)) &&
((this.wantedResolution_ &&
fromResolutionLike(this.wantedResolution_) === requestResolution) ||
fromResolutionLike(this.image.getResolution()) ===
requestResolution)
) {
return this.image;
}
((this.wantedResolution_ &&
fromResolutionLike(this.wantedResolution_) ===
requestResolution) ||
fromResolutionLike(this.image.getResolution()) ===
requestResolution)))
) {
return this.image;
}
this.wantedExtent_ = requestExtent;
this.wantedResolution_ = requestResolution;
Expand Down
22 changes: 22 additions & 0 deletions test/browser/spec/ol/source/Image.test.js
@@ -0,0 +1,22 @@
import ImageSource from '../../../../../src/ol/source/Image.js';

describe('ol/source/Image', function () {
describe('constructor', function () {
it('does not set the static_ flag when no loader is configured', function () {
const source = new ImageSource({});
expect(source.static_).to.be(false);
});
it('does not set the static_ flag when loader accepts arguments', function () {
const source = new ImageSource({
loader: function (extent, resolution, projection) {},
});
expect(source.static_).to.be(false);
});
it('sets the static_ flag when loader accepts no arguments', function () {
const source = new ImageSource({
loader: function () {},
});
expect(source.static_).to.be(true);
});
});
});