mapcomplete/Logic/ElementStorage.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-06-23 22:35:19 +00:00
/**
2021-01-02 20:03:40 +00:00
* Keeps track of a dictionary 'elementID' -> UIEventSource<tags>
2020-06-23 22:35:19 +00:00
*/
import {UIEventSource} from "./UIEventSource";
2020-06-23 22:35:19 +00:00
export class ElementStorage {
private _elements = [];
constructor() {
}
addElementById(id: string, eventSource: UIEventSource<any>) {
this._elements[id] = eventSource;
}
addElement(element): UIEventSource<any> {
2021-02-05 15:32:37 +00:00
const eventSource = new UIEventSource<any>(element.properties, "tags of "+element.properties.id);
2020-06-23 22:35:19 +00:00
this._elements[element.properties.id] = eventSource;
return eventSource;
}
addOrGetElement(element: any) : UIEventSource<any>{
2020-06-23 22:35:19 +00:00
const elementId = element.properties.id;
if (elementId in this._elements) {
const es = this._elements[elementId];
const keptKeys = es.data;
// The element already exists
// We add all the new keys to the old keys
for (const k in element.properties) {
const v = element.properties[k];
if (keptKeys[k] !== v) {
keptKeys[k] = v;
es.ping();
}
}
return es;
}else{
return this.addElement(element);
}
}
2020-12-05 02:22:17 +00:00
getEventSourceById(elementId): UIEventSource<any> {
2020-06-23 22:35:19 +00:00
if (elementId in this._elements) {
return this._elements[elementId];
}
2020-12-05 02:22:17 +00:00
console.error("Can not find eventsource with id ", elementId);
}
getEventSourceFor(feature): UIEventSource<any> {
return this.getEventSourceById(feature.properties.id);
2020-06-23 22:35:19 +00:00
}
}