wip
This commit is contained in:
@@ -47,7 +47,9 @@ function processMessage(message) {
|
||||
console.log(message);
|
||||
try {
|
||||
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);
|
||||
progvars[variable] = value;
|
||||
|
||||
@@ -64,7 +66,7 @@ function processMessage(message) {
|
||||
}
|
||||
}
|
||||
|
||||
function calculateDiff(oldValue, newValue) {
|
||||
function calculateDiff(provVarsVariable, oldValue, newValue) {
|
||||
const changes = [];
|
||||
|
||||
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) {
|
||||
// Recursive diff for objects
|
||||
const subChanges = findChanges(currentPath, oldKeyValue, newKeyValue);
|
||||
if (subChanges > 0) changedCount++;
|
||||
if (subChanges > 0) changedCount += subChanges;
|
||||
} else if (oldKeyValue !== newKeyValue) {
|
||||
// Direct value change
|
||||
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 (changedCount > 0 && changedCount > oldKeys.length / 3) {
|
||||
changes.push(`${path} = ${JSON.stringify(newVal)};`);
|
||||
// Clear individual changes as we replace the entire node
|
||||
changes.splice(-changedCount);
|
||||
// Replace the node
|
||||
changes.push(`${path} = ${JSON.stringify(newVal)};`);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -114,9 +118,9 @@ function calculateDiff(oldValue, newValue) {
|
||||
|
||||
// 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['+provVarsVariable+']', oldValue, newValue);
|
||||
} else if (oldValue !== newValue) {
|
||||
changes.push(`progvars = ${JSON.stringify(newValue)};`);
|
||||
changes.push(`progvars[${provVarsVariable}] = ${JSON.stringify(newValue)};`);
|
||||
}
|
||||
|
||||
return changes.join('\n');
|
||||
|
||||
Reference in New Issue
Block a user