From 3ba35357c764dcc2145b346796b4d36c9d11611d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 16 Feb 2024 19:38:14 -0600 Subject: [PATCH] wip --- shell/watcher.js | 79 ++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/shell/watcher.js b/shell/watcher.js index d1a3c95..19425c6 100644 --- a/shell/watcher.js +++ b/shell/watcher.js @@ -65,48 +65,63 @@ function processMessage(message) { } function calculateDiff(oldValue, newValue) { - const changes = []; - - function findChanges(path, oldVal, newVal) { - const oldKeys = oldVal ? Object.keys(oldVal) : []; + const changes = []; + + 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}']`; - - 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++; - } + 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' && 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 ////