Skip to content

Commit

Permalink
Allow updating an image before it loads or fails to load (#12992) (h/t
Browse files Browse the repository at this point in the history
…@maciejmatu)

Co-authored-by: maciejmatu <[email protected]>
  • Loading branch information
stepankuzmin and maciejmatu authored Dec 4, 2023
1 parent b8ad02e commit dd10457
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/source/image_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class ImageSource extends Evented implements Source {
* });
*/
updateImage(options: {url: string, coordinates?: Coordinates}): this {
if (!this.image || !options.url) {
if (!options.url) {
return this;
}
if (this._imageRequest && options.url !== this.options.url) {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/source/image_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ test('ImageSource', (t) => {
img.onload();
img.data = new Uint8Array(req.response);
};
const respondNotFound = () => {
const req = requests.shift();
req.setStatus(404);
req.onerror();
req.response = new ArrayBuffer(1);
};
t.stub(browser, 'getImageData').callsFake(() => new ArrayBuffer(1));

t.test('constructor', (t) => {
Expand Down Expand Up @@ -256,5 +262,22 @@ test('ImageSource', (t) => {
t.end();
});

t.test('updates image before first image was loaded', (t) => {
const source = createSource({url : '/notfound.png'});
const map = new StubMap();
const spy = t.spy(map._requestManager, 'transformRequest');
const errorStub = t.stub(console, 'error');
source.onAdd(map);
respondNotFound();
t.ok(errorStub.calledOnce);
t.notOk(source.image);
source.updateImage({url: '/image2.png'});
respond();
t.ok(spy.calledTwice);
t.equal(spy.getCall(1).args[0], '/image2.png');
t.equal(spy.getCall(1).args[1], 'Image');
t.end();
});

t.end();
});

0 comments on commit dd10457

Please sign in to comment.