(state, diff)
| 32 | }; |
| 33 | |
| 34 | function handleDiff(state, diff) { |
| 35 | let diffEntities = 0; |
| 36 | let entitiesWithUpdatedValues = {}; |
| 37 | |
| 38 | let records = indexes.records; |
| 39 | let dirty = indexes.dirty; |
| 40 | |
| 41 | for(let remove of diff.remove) { |
| 42 | let [e, a, v] = remove; |
| 43 | if(!records.index[e]) { |
| 44 | console.error(`Attempting to remove an attribute of an entity that doesn't exist: ${e}`); |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | let entity = records.index[e]; |
| 49 | let values = entity[a]; |
| 50 | if(!values) continue; |
| 51 | dirty.insert(e, a); |
| 52 | |
| 53 | if(values.length <= 1 && values[0] === v) { |
| 54 | delete entity[a]; |
| 55 | } else { |
| 56 | let ix = values.indexOf(v); |
| 57 | if(ix === -1) continue; |
| 58 | values.splice(ix, 1); |
| 59 | } |
| 60 | |
| 61 | // Update indexes |
| 62 | if(a === "tag") indexes.byTag.remove(v, e); |
| 63 | else if(a === "name") indexes.byName.remove(v, e); |
| 64 | else if(a === "class") indexes.byClass.remove(v, e); |
| 65 | else if(a === "style") indexes.byStyle.remove(v, e); |
| 66 | // @NOTE: We intentionally leak children -> parent for now to easily restore |
| 67 | // children that get recreated with the same id which don't have an associated diff in their parent. |
| 68 | //else if(a === "children") indexes.byChild.remove(v, e); |
| 69 | else if(a === "value") entitiesWithUpdatedValues[e] = true; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | for(let insert of diff.insert) { |
| 74 | let [e, a, v] = insert; |
| 75 | let entity = records.index[e]; |
| 76 | if(!entity) { |
| 77 | entity = {}; |
| 78 | records.insert(e, entity); |
| 79 | diffEntities++; // Nuke this and use records.dirty |
| 80 | } |
| 81 | |
| 82 | dirty.insert(e, a); |
| 83 | |
| 84 | if(!entity[a]) entity[a] = []; |
| 85 | entity[a].push(v); |
| 86 | |
| 87 | // Update indexes |
| 88 | if(a === "tag") indexes.byTag.insert(v, e); |
| 89 | else if(a === "name") indexes.byName.insert(v, e); |
| 90 | else if(a === "class") indexes.byClass.insert(v, e); |
| 91 | else if(a === "style") indexes.byStyle.insert(v, e); |
no test coverage detected