forked from MapComplete/MapComplete
More refactoring: using a decent, configurable datapipeline now
This commit is contained in:
parent
6ac8ec84e4
commit
e42a668c4a
17 changed files with 434 additions and 265 deletions
40
Logic/FeatureSource/FeatureSourceMerger.ts
Normal file
40
Logic/FeatureSource/FeatureSourceMerger.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import FeatureSource from "./FeatureSource";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
|
||||
export default class FeatureSourceMerger implements FeatureSource {
|
||||
|
||||
public features: UIEventSource<{ feature: any; freshness: Date }[]> = new UIEventSource<{feature: any; freshness: Date}[]>([]);
|
||||
private readonly _sources: FeatureSource[];
|
||||
|
||||
constructor(sources: FeatureSource[]) {
|
||||
this._sources = sources;
|
||||
const self = this;
|
||||
for (const source of sources) {
|
||||
source.features.addCallback(() => self.Update());
|
||||
}
|
||||
}
|
||||
|
||||
private Update() {
|
||||
let all = {}; // Mapping 'id' -> {feature, freshness}
|
||||
for (const source of this._sources) {
|
||||
for (const f of source.features.data) {
|
||||
const id = f.feature.properties.id+f.feature.geometry.type;
|
||||
const oldV = all[id];
|
||||
if(oldV === undefined){
|
||||
all[id] = f;
|
||||
}else{
|
||||
if(oldV.freshness < f.freshness){
|
||||
all[id]=f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const newList = [];
|
||||
for (const id in all) {
|
||||
newList.push(all[id]);
|
||||
}
|
||||
this.features.setData(newList);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue