This commit is contained in:
Your Name
2024-02-16 19:38:14 -06:00
parent 5c2c44b780
commit 3ba35357c7

View File

@@ -65,49 +65,64 @@ function processMessage(message) {
}
function calculateDiff(oldValue, newValue) {
const changes = [];
const changes = [];
function findChanges(path, oldVal, newVal) {
const oldKeys = oldVal ? Object.keys(oldVal) : [];
function findChanges(path, oldVal, newVal) {
// Check if both values are objects (and not null), otherwise compare directly
if (!(typeof oldVal === 'object' && oldVal !== null) ||
!(typeof newVal === 'object' && newVal !== null)) {
if (oldVal !== newVal) {
changes.push(`${path} = ${JSON.stringify(newVal)};`);
}
return oldVal !== newVal ? 1 : 0;
}
const oldKeys = Object.keys(oldVal);
const newKeys = Object.keys(newVal);
const allKeys = new Set([...oldKeys, ...newKeys]);
let changedCount = 0;
allKeys.forEach(key => {
const oldKeyValue = oldVal ? oldVal[key] : undefined;
const newKeyValue = newVal[key];
const currentPath = path ? `${path}['${key}']` : `['${key}']`;
const oldKeyValue = oldVal[key];
const newKeyValue = newVal[key];
const currentPath = path ? `${path}['${key}']` : `['${key}']`;
if (!oldVal || !(key in oldVal)) {
// New key added
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
changedCount++;
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object') {
// Recursive diff for objects
const subChanges = findChanges(currentPath, oldKeyValue, newKeyValue);
if (subChanges > 0) changedCount++;
} else if (oldKeyValue !== newKeyValue) {
// Direct value change
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
changedCount++;
}
if (!oldVal || !(key in oldVal)) {
// New key added
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
changedCount++;
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object' && newKeyValue !== null) {
// Recursive diff for objects
const subChanges = findChanges(currentPath, oldKeyValue, newKeyValue);
if (subChanges > 0) changedCount++;
} else if (oldKeyValue !== newKeyValue) {
// Direct value change
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
changedCount++;
}
});
// If more than 1/3 of the properties have been changed, replace the entire node
if (changedCount > 0 && changedCount > oldKeys.length / 3) {
changes.push(`${path} = ${JSON.stringify(newVal)};`);
// Clear individual changes as we replace the entire node
return 0;
changes.push(`${path} = ${JSON.stringify(newVal)};`);
// Clear individual changes as we replace the entire node
return 0;
}
return changedCount;
}
findChanges('progvars', oldValue, newValue);
return changes.join('\n');
}
// Adjust initial call to handle non-object types
if ((typeof oldValue === 'object' && oldValue !== null) && (typeof newValue === 'object' && newValue !== null)) {
findChanges('progvars', oldValue, newValue);
} else if (oldValue !== newValue) {
changes.push(`progvars = ${JSON.stringify(newValue)};`);
}
return changes.join('\n');
}
//// HTTP SERVER ////
const WebSocket = require('ws');