/** * Keeps track of a dictionary 'elementID' -> UIEventSource */ import {UIEventSource} from "./UIEventSource"; export class ElementStorage { private _elements = []; constructor() { } addElementById(id: string, eventSource: UIEventSource) { this._elements[id] = eventSource; } addElement(element): UIEventSource { const eventSource = new UIEventSource(element.properties, "tags of "+element.properties.id); this._elements[element.properties.id] = eventSource; return eventSource; } addOrGetElement(element: any) : UIEventSource{ 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); } } getEventSourceById(elementId): UIEventSource { if (elementId in this._elements) { return this._elements[elementId]; } console.error("Can not find eventsource with id ", elementId); } getEventSourceFor(feature): UIEventSource { return this.getEventSourceById(feature.properties.id); } }