This commit is contained in:
Your Name
2024-02-16 19:54:22 -06:00
parent 0f492814f4
commit 28ff6d11e2

View File

@@ -47,7 +47,9 @@ function processMessage(message) {
console.log(message); console.log(message);
try { try {
const { variable, value } = JSON.parse(message); const { variable, value } = JSON.parse(message);
const diff = JSON.stringify(calculateDiff(progvars[variable], value)); console.log({var: variable, val: value});
const diff = calculateDiff(variable, progvars[variable], value);
console.log({diff: diff});
send_ws_message(diff); send_ws_message(diff);
progvars[variable] = value; progvars[variable] = value;
@@ -64,7 +66,7 @@ function processMessage(message) {
} }
} }
function calculateDiff(oldValue, newValue) { function calculateDiff(provVarsVariable, oldValue, newValue) {
const changes = []; const changes = [];
function findChanges(path, oldVal, newVal) { function findChanges(path, oldVal, newVal) {
@@ -94,7 +96,7 @@ function calculateDiff(oldValue, newValue) {
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object' && newKeyValue !== null) { } 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 += subChanges;
} 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)};`);
@@ -104,8 +106,10 @@ function calculateDiff(oldValue, newValue) {
// 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)};`);
// Clear individual changes as we replace the entire node // Clear individual changes as we replace the entire node
changes.splice(-changedCount);
// Replace the node
changes.push(`${path} = ${JSON.stringify(newVal)};`);
return 0; return 0;
} }
@@ -114,9 +118,9 @@ function calculateDiff(oldValue, newValue) {
// Adjust initial call to handle non-object types // Adjust initial call to handle non-object types
if ((typeof oldValue === 'object' && oldValue !== null) && (typeof newValue === 'object' && newValue !== null)) { if ((typeof oldValue === 'object' && oldValue !== null) && (typeof newValue === 'object' && newValue !== null)) {
findChanges('progvars', oldValue, newValue); findChanges('progvars['+provVarsVariable+']', oldValue, newValue);
} else if (oldValue !== newValue) { } else if (oldValue !== newValue) {
changes.push(`progvars = ${JSON.stringify(newValue)};`); changes.push(`progvars[${provVarsVariable}] = ${JSON.stringify(newValue)};`);
} }
return changes.join('\n'); return changes.join('\n');