2020-06-24 00:35:19 +02:00
|
|
|
/**
|
2021-01-02 21:03:40 +01:00
|
|
|
* Keeps track of a dictionary 'elementID' -> UIEventSource<tags>
|
2020-06-24 00:35:19 +02:00
|
|
|
*/
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "./UIEventSource";
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
export class ElementStorage {
|
|
|
|
|
2021-04-17 23:36:46 +02:00
|
|
|
private _elements = new Map<string, UIEventSource<any>>();
|
2020-06-24 00:35:19 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
addElementById(id: string, eventSource: UIEventSource<any>) {
|
2021-04-17 23:36:46 +02:00
|
|
|
this._elements.set(id, eventSource);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-25 15:19:44 +01:00
|
|
|
/**
|
|
|
|
* Creates a UIEventSource for the tags of the given feature.
|
|
|
|
* If an UIEventsource has been created previously, the same UIEventSource will be returned
|
|
|
|
*
|
|
|
|
* Note: it will cleverly merge the tags, if needed
|
|
|
|
*/
|
|
|
|
addOrGetElement(feature: any): UIEventSource<any> {
|
|
|
|
const elementId = feature.properties.id;
|
2021-04-21 01:26:13 +02:00
|
|
|
const newProperties = feature.properties;
|
2021-03-25 15:19:44 +01:00
|
|
|
|
2021-04-21 01:26:13 +02:00
|
|
|
const es = this.addOrGetById(elementId, newProperties)
|
2020-06-24 00:35:19 +02:00
|
|
|
|
2021-04-21 01:26:13 +02:00
|
|
|
// At last, we overwrite the tag of the new feature to use the tags in the already existing event source
|
|
|
|
feature.properties = es.data
|
|
|
|
return es;
|
|
|
|
}
|
|
|
|
|
|
|
|
addOrGetById(elementId: string, newProperties: any): UIEventSource<any> {
|
|
|
|
if (!this._elements.has(elementId)) {
|
|
|
|
const eventSource = new UIEventSource<any>(newProperties, "tags of " + elementId);
|
|
|
|
this._elements.set(elementId, eventSource);
|
2021-03-25 15:19:44 +01:00
|
|
|
return eventSource;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2021-04-21 01:26:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
const es = this._elements.get(elementId);
|
|
|
|
if (es.data == newProperties) {
|
|
|
|
// Reference comparison gives the same object! we can just return the event source
|
|
|
|
return es;
|
|
|
|
}
|
|
|
|
const keptKeys = es.data;
|
|
|
|
// The element already exists
|
|
|
|
// We use the new feature to overwrite all the properties in the already existing eventsource
|
2021-05-06 01:33:09 +02:00
|
|
|
console.log("Merging multiple instances of ", elementId)
|
2021-04-21 01:26:13 +02:00
|
|
|
let somethingChanged = false;
|
|
|
|
for (const k in newProperties) {
|
|
|
|
const v = newProperties[k];
|
|
|
|
if (keptKeys[k] !== v) {
|
|
|
|
keptKeys[k] = v;
|
|
|
|
somethingChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (somethingChanged) {
|
|
|
|
es.ping();
|
|
|
|
}
|
|
|
|
return es;
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
|
|
|
|
2020-12-05 03:22:17 +01:00
|
|
|
getEventSourceById(elementId): UIEventSource<any> {
|
2021-04-17 23:36:46 +02:00
|
|
|
if (this._elements.has(elementId)) {
|
|
|
|
return this._elements.get(elementId);
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|
2020-12-05 03:22:17 +01:00
|
|
|
console.error("Can not find eventsource with id ", elementId);
|
2021-04-17 23:36:46 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
has(id) {
|
|
|
|
return this._elements.has(id);
|
2020-12-05 03:22:17 +01:00
|
|
|
}
|
2020-06-24 00:35:19 +02:00
|
|
|
}
|