wip
This commit is contained in:
@@ -65,49 +65,64 @@ function processMessage(message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function calculateDiff(oldValue, newValue) {
|
function calculateDiff(oldValue, newValue) {
|
||||||
const changes = [];
|
const changes = [];
|
||||||
|
|
||||||
function findChanges(path, oldVal, newVal) {
|
function findChanges(path, oldVal, newVal) {
|
||||||
const oldKeys = oldVal ? Object.keys(oldVal) : [];
|
// 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 newKeys = Object.keys(newVal);
|
||||||
const allKeys = new Set([...oldKeys, ...newKeys]);
|
const allKeys = new Set([...oldKeys, ...newKeys]);
|
||||||
let changedCount = 0;
|
let changedCount = 0;
|
||||||
|
|
||||||
allKeys.forEach(key => {
|
allKeys.forEach(key => {
|
||||||
const oldKeyValue = oldVal ? oldVal[key] : undefined;
|
const oldKeyValue = oldVal[key];
|
||||||
const newKeyValue = newVal[key];
|
const newKeyValue = newVal[key];
|
||||||
const currentPath = path ? `${path}['${key}']` : `['${key}']`;
|
const currentPath = path ? `${path}['${key}']` : `['${key}']`;
|
||||||
|
|
||||||
if (!oldVal || !(key in oldVal)) {
|
if (!oldVal || !(key in oldVal)) {
|
||||||
// New key added
|
// New key added
|
||||||
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
|
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
|
||||||
changedCount++;
|
changedCount++;
|
||||||
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object') {
|
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object' && newKeyValue !== null) {
|
||||||
// Recursive diff for objects
|
// Recursive diff for objects
|
||||||
const subChanges = findChanges(currentPath, oldKeyValue, newKeyValue);
|
const subChanges = findChanges(currentPath, oldKeyValue, newKeyValue);
|
||||||
if (subChanges > 0) changedCount++;
|
if (subChanges > 0) changedCount++;
|
||||||
} else if (oldKeyValue !== newKeyValue) {
|
} else if (oldKeyValue !== newKeyValue) {
|
||||||
// Direct value change
|
// Direct value change
|
||||||
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
|
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
|
||||||
changedCount++;
|
changedCount++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// If more than 1/3 of the properties have been changed, replace the entire node
|
// If more than 1/3 of the properties have been changed, replace the entire node
|
||||||
if (changedCount > 0 && changedCount > oldKeys.length / 3) {
|
if (changedCount > 0 && changedCount > oldKeys.length / 3) {
|
||||||
changes.push(`${path} = ${JSON.stringify(newVal)};`);
|
changes.push(`${path} = ${JSON.stringify(newVal)};`);
|
||||||
// Clear individual changes as we replace the entire node
|
// Clear individual changes as we replace the entire node
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return changedCount;
|
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 ////
|
//// HTTP SERVER ////
|
||||||
|
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
|
|||||||
Reference in New Issue
Block a user