Refactoring: add generics to some featureSources

This commit is contained in:
Pieter Vander Vennet 2023-05-05 01:25:12 +02:00
parent e038394331
commit dec4296204
3 changed files with 21 additions and 38 deletions

View file

@ -18,7 +18,7 @@ export interface Tiled {
/** /**
* A feature source which only contains features for the defined layer * A feature source which only contains features for the defined layer
*/ */
export interface FeatureSourceForLayer extends FeatureSource { export interface FeatureSourceForLayer<T extends Feature = Feature> extends FeatureSource<T> {
readonly layer: FilteredLayer readonly layer: FilteredLayer
} }

View file

@ -1,30 +1,28 @@
import { FeatureSource , FeatureSourceForLayer, Tiled } from "../FeatureSource" import {FeatureSource} from "../FeatureSource"
import {ImmutableStore, Store} from "../../UIEventSource" import {ImmutableStore, Store} from "../../UIEventSource"
import FilteredLayer from "../../../Models/FilteredLayer"
import { BBox } from "../../BBox"
import {Feature} from "geojson" import {Feature} from "geojson"
/** /**
* A simple, read only feature store. * A simple, read only feature store.
*/ */
export default class StaticFeatureSource implements FeatureSource { export default class StaticFeatureSource<T extends Feature = Feature> implements FeatureSource<T> {
public readonly features: Store<Feature[]> public readonly features: Store<T[]>
constructor( constructor(
features: features:
| Store<Feature[]> | Store<T[]>
| Feature[] | T[]
| { features: Feature[] } | { features: T[] }
| { features: Store<Feature[]> } | { features: Store<T[]> }
) { ) {
if (features === undefined) { if (features === undefined) {
throw "Static feature source received undefined as source" throw "Static feature source received undefined as source"
} }
let feats: Feature[] | Store<Feature[]> let feats: T[] | Store<T[]>
if (features["features"]) { if (features["features"]) {
feats = features["features"] feats = features["features"]
} else { } else {
feats = <Feature[] | Store<Feature[]>>features feats = <T[] | Store<T[]>>features
} }
if (Array.isArray(feats)) { if (Array.isArray(feats)) {
@ -34,23 +32,7 @@ export default class StaticFeatureSource implements FeatureSource {
} }
} }
public static fromGeojson(geojson: Feature[]): StaticFeatureSource { public static fromGeojson<T extends Feature>(geojson: T[]): StaticFeatureSource<T> {
return new StaticFeatureSource(geojson) return new StaticFeatureSource(geojson)
} }
} }
export class TiledStaticFeatureSource
extends StaticFeatureSource
implements Tiled, FeatureSourceForLayer
{
public readonly bbox: BBox = BBox.global
public readonly tileIndex: number
public readonly layer: FilteredLayer
constructor(features: Store<Feature[]>, layer: FilteredLayer, tileIndex: number = 0) {
super(features)
this.tileIndex = tileIndex
this.layer = layer
this.bbox = BBox.fromTileIndex(this.tileIndex)
}
}

View file

@ -3,12 +3,13 @@ import StaticFeatureSource from "./StaticFeatureSource"
import {BBox} from "../../BBox" import {BBox} from "../../BBox"
import FilteredLayer from "../../../Models/FilteredLayer" import FilteredLayer from "../../../Models/FilteredLayer"
import {Store} from "../../UIEventSource" import {Store} from "../../UIEventSource"
import {Feature} from "geojson";
/** /**
* Results in a feature source which has all the elements that touch the given features * Results in a feature source which has all the elements that touch the given features
*/ */
export default class BBoxFeatureSource extends StaticFeatureSource { export default class BBoxFeatureSource<T extends Feature = Feature> extends StaticFeatureSource<T> {
constructor(features: FeatureSource, mustTouch: Store<BBox>) { constructor(features: FeatureSource<T>, mustTouch: Store<BBox>) {
super( super(
features.features.mapD( features.features.mapD(
(features) => { (features) => {
@ -29,10 +30,10 @@ export default class BBoxFeatureSource extends StaticFeatureSource {
} }
} }
export class BBoxFeatureSourceForLayer extends BBoxFeatureSource implements FeatureSourceForLayer { export class BBoxFeatureSourceForLayer<T extends Feature = Feature> extends BBoxFeatureSource<T> implements FeatureSourceForLayer {
readonly layer: FilteredLayer readonly layer: FilteredLayer
constructor(features: FeatureSourceForLayer, mustTouch: Store<BBox>) { constructor(features: FeatureSourceForLayer<T>, mustTouch: Store<BBox>) {
super(features, mustTouch) super(features, mustTouch)
this.layer = features.layer this.layer = features.layer
} }