mapcomplete/Logic/FeatureSource/Sources/RememberingSource.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
1.3 KiB
TypeScript
Raw Normal View History

/**
* Every previously added point is remembered, but new points are added.
* Data coming from upstream will always overwrite a previous value
*/
import FeatureSource, { Tiled } from "../FeatureSource"
import { Store, UIEventSource } from "../../UIEventSource"
import { BBox } from "../../BBox"
import { Feature } from "geojson"
2021-11-07 16:34:51 +01:00
export default class RememberingSource implements FeatureSource, Tiled {
public readonly features: Store<Feature[]>
2021-11-07 16:34:51 +01:00
public readonly tileIndex: number
public readonly bbox: BBox
constructor(source: FeatureSource & Tiled) {
const self = this
2021-11-07 16:34:51 +01:00
this.tileIndex = source.tileIndex
this.bbox = source.bbox
2021-11-07 16:34:51 +01:00
2021-01-04 22:59:11 +01:00
const empty = []
const featureSource = new UIEventSource<Feature[]>(empty)
2022-06-06 19:37:22 +02:00
this.features = featureSource
source.features.addCallbackAndRunD((features) => {
2021-01-04 22:59:11 +01:00
const oldFeatures = self.features?.data ?? empty
// Then new ids
const ids = new Set<string>(features.map((f) => f.properties.id + f.geometry.type))
2021-01-04 22:59:11 +01:00
// the old data
const oldData = oldFeatures.filter(
(old) => !ids.has(old.feature.properties.id + old.feature.geometry.type)
2022-09-08 21:40:48 +02:00
)
2022-06-06 19:37:22 +02:00
featureSource.setData([...features, ...oldData])
})
}
}