wip
This commit is contained in:
@@ -68,13 +68,22 @@ 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}']`;
|
||||||
|
|
||||||
@@ -82,7 +91,7 @@ function calculateDiff(oldValue, newValue) {
|
|||||||
// 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++;
|
||||||
@@ -103,11 +112,17 @@ function calculateDiff(oldValue, newValue) {
|
|||||||
return changedCount;
|
return changedCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adjust initial call to handle non-object types
|
||||||
|
if ((typeof oldValue === 'object' && oldValue !== null) && (typeof newValue === 'object' && newValue !== null)) {
|
||||||
findChanges('progvars', oldValue, newValue);
|
findChanges('progvars', oldValue, newValue);
|
||||||
|
} else if (oldValue !== newValue) {
|
||||||
|
changes.push(`progvars = ${JSON.stringify(newValue)};`);
|
||||||
|
}
|
||||||
|
|
||||||
return changes.join('\n');
|
return changes.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//// HTTP SERVER ////
|
//// HTTP SERVER ////
|
||||||
|
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
|
|||||||
Reference in New Issue
Block a user