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

Ignore style.loadURL abort error when style is removed #4425

Merged
merged 8 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### 🐞 Bug fixes

- Fix lag on fast map zoom ([#4366](https://github.com/maplibre/maplibre-gl-js/pull/4366))
- Fix `style.loadURL` abort error being logged when removing style ([#4425](https://github.com/maplibre/maplibre-gl-js/pull/4425))
- Fix remove hash string when map is removed ([#4427](https://github.com/maplibre/maplibre-gl-js/pull/4427))
- _...Add new stuff here..._

Expand Down
29 changes: 29 additions & 0 deletions src/style/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ 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);
HarelM marked this conversation as resolved.
Show resolved Hide resolved
style.loadURL('style.json');
const promise = new Promise((_, reject) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see other places are using a simpler method:
expect((server.lastRequest as any).aborted).toBe(true);
I think it can be used here as well.
I'll open a PR to update the types so we can remove these "as any".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect((server.lastRequest as any).aborted).toBe(true); is used to test if the request has been aborted, which the test above does ('cancels pending requests if removed'). In our case, we want to test that, when the request is aborted, no error are fired.

Removing these "as any" would be nice, thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using expect((server.lastRequest as any).aborted).toBe(true); would remove the need to use this promise, and wait for it rejection. I find it more elegant. You might still need a sleep(0) though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the nise types, now you can use it without the "as any":
#4446

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the nise types, now you can use it without the "as any"

Thanks !

Using expect((server.lastRequest as any).aborted).toBe(true); would remove the need to use this promise, and wait for it rejection. I find it more elegant. You might still need a sleep(0) though.

We would still need a sleep(0) since the rejection is asynchronous. Without it, the test succeed even if the Style fires the abort error. The promise is just a way to wait for the rejection we want to test rather than just wait for a bit with the sleep(0), but I agree it's not very elegant and it might be too bound to implementation. Should I revert to sleep(0) ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please.

(server.lastRequest as any).addEventListener('abort', reject);
});
style._remove();
await promise.catch(() => {});

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;
HarelM marked this conversation as resolved.
Show resolved Hide resolved
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