Skip to content

Commit

Permalink
Improve types a bit for addSource and getSource (#4616)
Browse files Browse the repository at this point in the history
* Improve types a bit for addSource and getSource

* Update changelog
  • Loading branch information
HarelM authored Aug 28, 2024
1 parent 050c5d5 commit d60007b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- Heatmap Fix for 3D terrain ([#4571](https://github.com/maplibre/maplibre-gl-js/pull/4571))
- Fix Map#off to not remove listener with layer(s) registered with Map#once ([#4592](https://github.com/maplibre/maplibre-gl-js/pull/4592))
- Improve types a bit for `addSource` and `getSource` ([#4616](https://github.com/maplibre/maplibre-gl-js/pull/4616))
- Fix the color near the horizon when terrain is enabled without any sky ([#4607](https://github.com/maplibre/maplibre-gl-js/pull/4607))
- _...Add new stuff here..._

Expand Down
7 changes: 4 additions & 3 deletions src/source/source_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Point from '@mapbox/point-geometry';
import {browser} from '../util/browser';
import {OverscaledTileID} from './tile_id';
import {SourceFeatureState} from './source_state';
import {config} from '../util/config';

import type {Source} from './source';
import type {Map} from '../ui/map';
Expand All @@ -20,8 +21,8 @@ import type {Transform} from '../geo/transform';
import type {TileState} from './tile';
import type {SourceSpecification} from '@maplibre/maplibre-gl-style-spec';
import type {MapSourceDataEvent} from '../ui/events';
import {Terrain} from '../render/terrain';
import {config} from '../util/config';
import type {Terrain} from '../render/terrain';
import type {CanvasSourceSpecification} from './canvas_source';

/**
* @internal
Expand Down Expand Up @@ -78,7 +79,7 @@ export class SourceCache extends Evented {
static maxUnderzooming: number;
static maxOverzooming: number;

constructor(id: string, options: SourceSpecification, dispatcher: Dispatcher) {
constructor(id: string, options: SourceSpecification | CanvasSourceSpecification, dispatcher: Dispatcher) {
super();
this.id = id;
this.dispatcher = dispatcher;
Expand Down
3 changes: 2 additions & 1 deletion src/style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {
DiffOperations,
SkySpecification
} from '@maplibre/maplibre-gl-style-spec';
import type {CanvasSourceSpecification} from '../source/canvas_source';
import type {CustomLayerInterface} from './style_layer/custom_style_layer';
import type {Validator} from './validate_style';
import {
Expand Down Expand Up @@ -833,7 +834,7 @@ export class Style extends Evented {
return this.imageManager.listImages();
}

addSource(id: string, source: SourceSpecification, options: StyleSetterOptions = {}) {
addSource(id: string, source: SourceSpecification | CanvasSourceSpecification, options: StyleSetterOptions = {}) {
this._checkLoaded();

if (this.sourceCaches[id] !== undefined) {
Expand Down
7 changes: 4 additions & 3 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import type {
TerrainSpecification,
SkySpecification
} from '@maplibre/maplibre-gl-style-spec';
import type {CanvasSourceSpecification} from '../source/canvas_source';
import type {MapGeoJSONFeature} from '../util/vectortile_to_geojson';
import type {ControlPosition, IControl} from './control/control';
import type {QueryRenderedFeaturesOptions, QuerySourceFeatureOptions} from '../source/query_features';
Expand Down Expand Up @@ -1930,7 +1931,7 @@ export class Map extends Camera {
* ```
* @see GeoJSON source: [Add live realtime data](https://maplibre.org/maplibre-gl-js/docs/examples/live-geojson/)
*/
addSource(id: string, source: SourceSpecification): this {
addSource(id: string, source: SourceSpecification | CanvasSourceSpecification): this {
this._lazyInitEmptyStyle();
this.style.addSource(id, source);
return this._update(true);
Expand Down Expand Up @@ -2089,8 +2090,8 @@ export class Map extends Camera {
* @see [Animate a point](https://maplibre.org/maplibre-gl-js/docs/examples/animate-point-along-line/)
* @see [Add live realtime data](https://maplibre.org/maplibre-gl-js/docs/examples/live-geojson/)
*/
getSource(id: string): Source | undefined {
return this.style.getSource(id);
getSource<TSource extends Source>(id: string): TSource | undefined {
return this.style.getSource(id) as TSource;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/integration/render/run_render_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ async function getImageFromStyle(styleForTest: StyleWithTestData, page: Page): P
map._render();
break;
case 'updateFakeCanvas': {
const canvasSource = map.getSource(operation[1]) as CanvasSource;
const canvasSource = map.getSource<CanvasSource>(operation[1]);
canvasSource.play();
// update before pause should be rendered
await updateFakeCanvas(window.document, testData.addFakeCanvas.id, operation[2]);
Expand Down

0 comments on commit d60007b

Please sign in to comment.