mapcomplete/Logic/FeatureSource/Sources/RememberingSource.ts

32 lines
1.2 KiB
TypeScript
Raw Normal View History

import FeatureSource, {FeatureSourceForLayer} from "./FeatureSource";
import {UIEventSource} from "../UIEventSource";
import FilteredLayer from "../../Models/FilteredLayer";
/**
* Every previously added point is remembered, but new points are added.
* Data coming from upstream will always overwrite a previous value
*/
2021-01-04 22:59:11 +01:00
export default class RememberingSource implements FeatureSource {
public readonly features: UIEventSource<{ feature: any, freshness: Date }[]>;
public readonly name;
constructor(source: FeatureSource) {
const self = this;
this.name = "RememberingSource of " + source.name;
2021-01-04 22:59:11 +01:00
const empty = [];
this.features = source.features.map(features => {
const oldFeatures = self.features?.data ?? empty;
if (features === undefined) {
return oldFeatures;
}
2021-01-04 22:59:11 +01:00
// Then new ids
const ids = new Set<string>(features.map(f => f.feature.properties.id + f.feature.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))
2021-01-04 22:59:11 +01:00
return [...features, ...oldData];
})
}
2021-01-04 22:59:11 +01:00
}