Skip to content

Commit

Permalink
Ignore style.loadURL abort error when style is removed (#4425)
Browse files Browse the repository at this point in the history
* Add test for style#loadURL abort error

* Ignore style.loadURL abort error

* Add test for style#loadURL firing error if request fails

* Fix a style#loadURL test to use promises

* Fix a style#loadURL test to remove sleep(0)

* Revert "Fix a style#loadURL test to remove sleep(0)"

This reverts commit f6e7764.

---------

Co-authored-by: Harel M <harel.mazor@gmail.com>
  • Loading branch information
madoci and HarelM authored Jul 29, 2024
1 parent 6db4607 commit fc0012e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Fix lag on fast map zoom ([#4366](https://github.com/maplibre/maplibre-gl-js/pull/4366))
- Fix unguarded read access to possibly undefined object ([#4431](https://github.com/maplibre/maplibre-gl-js/pull/4431))
- Fix remove hash string when map is removed ([#4427](https://github.com/maplibre/maplibre-gl-js/pull/4427))
- Fix `style.loadURL` abort error being logged when removing style ([#4425](https://github.com/maplibre/maplibre-gl-js/pull/4425))
- _...Add new stuff here..._

## 4.5.0
Expand Down
26 changes: 26 additions & 0 deletions src/style/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ describe('Style#loadURL', () => {
style._remove();
expect((server.lastRequest as any).aborted).toBe(true);
});

test('does not fire an error if removed', async () => {
const style = new Style(getStubMap());
const spy = jest.fn();

style.on('error', spy);
style.loadURL('style.json');
style._remove();
await sleep(0);

expect(spy).not.toHaveBeenCalled();
});

test('fires an error if the request fails', async () => {
const style = new Style(getStubMap());
const errorStatus = 400;

const promise = style.once('error');
style.loadURL('style.json');
server.respondWith(request => request.respond(errorStatus));
server.respond();
const {error} = await promise;

expect(error).toBeTruthy();
expect(error.status).toBe(errorStatus);
});
});

describe('Style#loadJSON', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,13 @@ export class Style extends Evented {

const request = this.map._requestManager.transformRequest(url, ResourceType.Style);
this._loadStyleRequest = new AbortController();
const abortController = this._loadStyleRequest;
getJSON<StyleSpecification>(request, this._loadStyleRequest).then((response) => {
this._loadStyleRequest = null;
this._load(response.data, options, previousStyle);
}).catch((error) => {
this._loadStyleRequest = null;
if (error) {
if (error && !abortController.signal.aborted) { // ignore abort
this.fire(new ErrorEvent(error));
}
});
Expand Down

0 comments on commit fc0012e

Please sign in to comment.