JSFiddle - React, Tailwind, and code Playground
by InfinitiesLoop
JavaScript
(function() {
window.ChangeSet = function() {
this.changes = [];
this.changesLookup = {};
};
window.ChangeSet.prototype = {
change: function(entityId, property, oldValue, newValue) {
var change = this.getPropertyChange(entityId, property)
|| this._addPropertyChange(entityId, property);
if (typeof change.oldValue === "undefined")
change.oldValue = oldValue + "";
change.newValue = newValue + "";
return this._purgeCheck(change);
// todo: add events for changes
},
getEntityChanges: function(entityId) {
if (typeof entityId === "undefined")
return this.changes;
return this.changesLookup[entityId];
},
getPropertyChange: function(entityId, property) {
var entityChanges = this.getChanges(entityId);
if (!entityChanges) return null;
return find(entityChanges.properties, property);
},
_addPropertyChange: function(entityId, property) {
var entityChanges = this.getEntityChanges(entityId)
|| this._addEntityChange(entityId);
var change = { id: property };
entityChanges.properties.push(change);
return change;
},
_addEntityChange: function(entityId) {
var change = { id: entityId, properties: [] };
this.changesLookup[entityId] = change;
changes.push(change);
return change;
},
_purgeCheck: function(propertyChange) {
if (propertyChange.oldValue === propertyChange.newValue) {
// todo: remove the property change, remove the entity changes if that is empty too
return false;
}
return true;
}
};
function find(list, id) {
for(var i = 0, l = list.length; i < l; i++) {
var item =...