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

Return subscription for on method #5080

Merged
merged 4 commits into from
Nov 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## main

### ✨ Features and improvements

- ⚠️ Change the return type of `on` method to return a `Subscription` to allow for easy unsubscribe ([#5080](https://github.com/maplibre/maplibre-gl-js/pull/5080))
- _...Add new stuff here..._

### 🐞 Bug fixes
Expand Down
8 changes: 4 additions & 4 deletions src/geo/edge_insets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {interpolates} from '@maplibre/maplibre-gl-style-spec';
import Point from '@mapbox/point-geometry';
import {clamp} from '../util/util';
import {clamp, Complete, RequireAtLeastOne} from '../util/util';

/**
* An `EdgeInset` object represents screen space padding applied to the edges of the viewport.
Expand Down Expand Up @@ -92,7 +92,7 @@ export class EdgeInsets {
*
* @returns state as json
*/
toJSON(): PaddingOptions {
toJSON(): Complete<PaddingOptions> {
return {
top: this.top,
bottom: this.bottom,
Expand Down Expand Up @@ -126,7 +126,7 @@ export class EdgeInsets {
* @see [Fit to the bounds of a LineString](https://maplibre.org/maplibre-gl-js/docs/examples/zoomto-linestring/)
* @see [Fit a map to a bounding box](https://maplibre.org/maplibre-gl-js/docs/examples/fitbounds/)
*/
export type PaddingOptions = {
export type PaddingOptions = RequireAtLeastOne<{
/**
* Padding in pixels from the top of the map canvas.
*/
Expand All @@ -143,4 +143,4 @@ export type PaddingOptions = {
* Padding in pixels from the right of the map canvas.
*/
left: number;
};
}>;
4 changes: 2 additions & 2 deletions src/geo/projection/globe_transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,8 @@
};
}

calculateCenterFromCameraLngLatAlt(ll: LngLat, alt: number, bearing?: number, pitch?: number): {center: LngLat; elevation: number; zoom: number} {
return this._mercatorTransform.calculateCenterFromCameraLngLatAlt(ll, alt, bearing, pitch);
calculateCenterFromCameraLngLatAlt(lngLat: LngLatLike, alt: number, bearing?: number, pitch?: number): {center: LngLat; elevation: number; zoom: number} {
return this._mercatorTransform.calculateCenterFromCameraLngLatAlt(lngLat, alt, bearing, pitch);

Check warning on line 839 in src/geo/projection/globe_transform.ts

View check run for this annotation

Codecov / codecov/patch

src/geo/projection/globe_transform.ts#L838-L839

Added lines #L838 - L839 were not covered by tests
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/geo/projection/mercator_transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ export class MercatorTransform implements ITransform {
return result;
}

calculateCenterFromCameraLngLatAlt(lnglat: LngLat, alt: number, bearing?: number, pitch?: number): {center: LngLat; elevation: number; zoom: number} {
calculateCenterFromCameraLngLatAlt(lnglat: LngLatLike, alt: number, bearing?: number, pitch?: number): {center: LngLat; elevation: number; zoom: number} {
const cameraBearing = bearing !== undefined ? bearing : this.bearing;
const cameraPitch = pitch = pitch !== undefined ? pitch : this.pitch;

Expand Down
2 changes: 1 addition & 1 deletion src/geo/transform_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export interface IReadonlyTransform extends ITransformGetters {
* @param bearing - bearing of the camera, in degrees
* @param pitch - pitch angle of the camera, in degrees
*/
calculateCenterFromCameraLngLatAlt(lngLat: LngLat, alt: number, bearing?: number, pitch?: number): {center: LngLat; elevation: number; zoom: number};
calculateCenterFromCameraLngLatAlt(lngLat: LngLatLike, alt: number, bearing?: number, pitch?: number): {center: LngLat; elevation: number; zoom: number};

getRayDirectionFromPixel(p: Point): vec3;

Expand Down
32 changes: 20 additions & 12 deletions src/source/source_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ describe('SourceCache#addTile', () => {

test('adds tile when uncached', () => new Promise<void>(done => {
const tileID = new OverscaledTileID(0, 0, 0, 0, 0);
const sourceCache = createSourceCache({}).on('dataloading', (data) => {
const sourceCache = createSourceCache({});
sourceCache.on('dataloading', (data) => {
expect(data.tile.tileID).toEqual(tileID);
expect(data.tile.uses).toBe(1);
done();
Expand Down Expand Up @@ -387,43 +388,47 @@ describe('SourceCache#removeTile', () => {

describe('SourceCache / Source lifecycle', () => {
test('does not fire load or change before source load event', () => new Promise<void>((done) => {
const sourceCache = createSourceCache({noLoad: true})
.on('data', () => { throw new Error('test failed: data event fired'); });
const sourceCache = createSourceCache({noLoad: true});
sourceCache.on('data', () => { throw new Error('test failed: data event fired'); });
sourceCache.onAdd(undefined);
setTimeout(() => done(), 1);
}));

test('forward load event', () => new Promise<void>(done => {
const sourceCache = createSourceCache({}).on('data', (e) => {
const sourceCache = createSourceCache({});
sourceCache.on('data', (e) => {
if (e.sourceDataType === 'metadata') done();
});
sourceCache.onAdd(undefined);
}));

test('forward change event', () => new Promise<void>(done => {
const sourceCache = createSourceCache().on('data', (e) => {
const sourceCache = createSourceCache();
sourceCache.on('data', (e) => {
if (e.sourceDataType === 'metadata') done();
});
sourceCache.onAdd(undefined);
sourceCache.getSource().fire(new Event('data'));
}));

test('forward error event', () => new Promise<void>(done => {
const sourceCache = createSourceCache({error: 'Error loading source'}).on('error', (err) => {
const sourceCache = createSourceCache({error: 'Error loading source'});
sourceCache.on('error', (err) => {
expect(err.error).toBe('Error loading source');
done();
});
sourceCache.onAdd(undefined);
}));

test('suppress 404 errors', () => {
const sourceCache = createSourceCache({status: 404, message: 'Not found'})
.on('error', () => { throw new Error('test failed: error event fired'); });
const sourceCache = createSourceCache({status: 404, message: 'Not found'});
sourceCache.on('error', () => { throw new Error('test failed: error event fired'); });
sourceCache.onAdd(undefined);
});

test('loaded() true after source error', () => new Promise<void>(done => {
const sourceCache = createSourceCache({error: 'Error loading source'}).on('error', () => {
const sourceCache = createSourceCache({error: 'Error loading source'});
sourceCache.on('error', () => {
expect(sourceCache.loaded()).toBeTruthy();
done();
});
Expand All @@ -442,7 +447,8 @@ describe('SourceCache / Source lifecycle', () => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
sourceCache.update(transform);
}
}).on('error', () => {
});
sourceCache.on('error', () => {
expect(sourceCache.loaded()).toBeTruthy();
done();
});
Expand All @@ -451,7 +457,8 @@ describe('SourceCache / Source lifecycle', () => {
}));

test('loaded() false after source begins loading following error', () => new Promise<void>(done => {
const sourceCache = createSourceCache({error: 'Error loading source'}).on('error', () => {
const sourceCache = createSourceCache({error: 'Error loading source'});
sourceCache.on('error', () => {
sourceCache.on('dataloading', () => {
expect(sourceCache.loaded()).toBeFalsy();
done();
Expand All @@ -469,7 +476,8 @@ describe('SourceCache / Source lifecycle', () => {
loaded() {
return false;
}
}).on('error', () => {
});
sourceCache.on('error', () => {
expect(sourceCache.loaded()).toBeFalsy();
done();
});
Expand Down
8 changes: 4 additions & 4 deletions src/style/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2447,10 +2447,10 @@ describe('Style#query*Features', () => {

onError = jest.fn();

style.on('error', onError)
.on('style.load', () => {
callback();
});
style.on('error', onError);
style.on('style.load', () => {
callback();
});
}));

test('querySourceFeatures emits an error on incorrect filter', () => {
Expand Down
Loading