nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
93 lines (92 loc) • 3.24 kB
JavaScript
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
import { NopeEventEmitter } from "../eventEmitter/nopeEventEmitter";
import { determineDifference } from "../helpers/setMethods";
import { NopeObservable } from "../observables/nopeObservable";
import { extractUniqueValues, tranformMap } from "./mapMethods";
export class MergeData {
constructor(originalData, _extractData) {
this.originalData = originalData;
this._extractData = _extractData;
this.onChange = new NopeEventEmitter();
this.data = new NopeObservable();
this.data.setContent([]);
}
/**
* Update the underlying data.
*
* @author M.Karkowski
* @param {*} [data=this.originalData]
* @memberof MergeData
*/
update(data = null, force = false) {
if (data !== null) {
this.originalData = data;
}
const afterAdding = this._extractData(this.originalData);
const diff = determineDifference(new Set(this.data.getContent()), afterAdding);
if (force || diff.removed.size > 0 || diff.added.size > 0) {
// Update the currently used subscriptions
this.data.setContent(Array.from(afterAdding));
// Now emit, that there is a new subscription.
this.onChange.emit({
added: Array.from(diff.added),
removed: Array.from(diff.removed),
});
}
}
/**
* Disposes the Element.
*
* @author M.Karkowski
* @memberof MergeData
*/
dispose() {
this.data.dispose();
this.onChange.dispose();
}
}
export class MapBasedMergeData extends MergeData {
constructor(originalData, _path = "", _pathKey = null) {
super(originalData, (m) => {
return extractUniqueValues(m, _path, _pathKey);
});
this._path = _path;
this._pathKey = _pathKey;
this.amountOf = new Map();
this.simplified = new Map();
this.keyMapping = new Map();
this.keyMappingReverse = new Map();
this.conflicts = new Map();
this.orgKeyToExtractedValue = new Map();
this.extractedKey = [];
this.extractedValue = [];
}
/**
* Update the underlying data.
*
* @author M.Karkowski
* @param {*} [data=this.originalData]
* @memberof MergeData
*/
update(data = null) {
if (data !== null) {
this.originalData = data;
}
// Now lets update the amount of the data:
const result = tranformMap(this.originalData, this._path, this._pathKey);
// Now assign the results to our items.
this.simplified = result.extractedMap;
this.amountOf = result.amountOf;
this.keyMapping = result.keyMapping;
this.keyMappingReverse = result.keyMappingReverse;
this.conflicts = result.conflicts;
this.orgKeyToExtractedValue = result.orgKeyToExtractedValue;
this.extractedKey = [...this.simplified.keys()];
this.extractedValue = [...this.simplified.values()];
super.update(data);
}
}