@adventurelabs/scout-core 1.0.111 → 1.0.113
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/useScoutRefresh.js +47 -64
- package/package.json +1 -1
|
@@ -91,75 +91,58 @@ export function useScoutRefresh(options = {}) {
|
|
|
91
91
|
}
|
|
92
92
|
return true;
|
|
93
93
|
}, []);
|
|
94
|
-
// Helper function to
|
|
95
|
-
const
|
|
96
|
-
if (!Array.isArray(
|
|
97
|
-
return
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
"events_page_index",
|
|
116
|
-
];
|
|
117
|
-
fieldsToCheck.forEach((field) => {
|
|
118
|
-
if (newHerd[field] !== currentHerd[field]) {
|
|
119
|
-
differences.push(`Herd ${i}.${field}: ${currentHerd[field]} → ${newHerd[field]}`);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
// Check array lengths for nested data
|
|
123
|
-
const arrayFields = [
|
|
124
|
-
"devices",
|
|
125
|
-
"events",
|
|
126
|
-
"plans",
|
|
127
|
-
"zones",
|
|
128
|
-
"sessions",
|
|
129
|
-
"layers",
|
|
130
|
-
"providers",
|
|
131
|
-
];
|
|
132
|
-
arrayFields.forEach((field) => {
|
|
133
|
-
const newArray = newHerd[field];
|
|
134
|
-
const currentArray = currentHerd[field];
|
|
135
|
-
if (Array.isArray(newArray) && Array.isArray(currentArray)) {
|
|
136
|
-
if (newArray.length !== currentArray.length) {
|
|
137
|
-
differences.push(`Herd ${i}.${field}[]: length ${currentArray.length} → ${newArray.length}`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
return differences.length > 0
|
|
143
|
-
? differences.join(", ")
|
|
144
|
-
: "No significant differences found";
|
|
94
|
+
// Helper function to sort herd modules consistently by ID
|
|
95
|
+
const sortHerdModulesById = useCallback((herdModules) => {
|
|
96
|
+
if (!Array.isArray(herdModules))
|
|
97
|
+
return herdModules;
|
|
98
|
+
return [...herdModules].sort((a, b) => {
|
|
99
|
+
const aId = a?.herd?.id || 0;
|
|
100
|
+
const bId = b?.herd?.id || 0;
|
|
101
|
+
return aId - bId;
|
|
102
|
+
});
|
|
103
|
+
}, []);
|
|
104
|
+
// Helper function to normalize herd modules for comparison (excludes timestamp metadata)
|
|
105
|
+
const normalizeHerdModulesForComparison = useCallback((herdModules) => {
|
|
106
|
+
if (!Array.isArray(herdModules))
|
|
107
|
+
return herdModules;
|
|
108
|
+
return herdModules.map((hm) => {
|
|
109
|
+
if (!hm || typeof hm !== "object")
|
|
110
|
+
return hm;
|
|
111
|
+
// Create a copy without timestamp metadata that doesn't represent business data changes
|
|
112
|
+
const { timestamp_last_refreshed, ...businessData } = hm;
|
|
113
|
+
return businessData;
|
|
114
|
+
});
|
|
145
115
|
}, []);
|
|
146
|
-
// Helper function to conditionally dispatch only if data has changed
|
|
147
|
-
const conditionalDispatch = useCallback((newData, currentData, actionCreator, dataType,
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
116
|
+
// Helper function to conditionally dispatch only if business data has changed
|
|
117
|
+
const conditionalDispatch = useCallback((newData, currentData, actionCreator, dataType, skipTimestampOnlyUpdates = false) => {
|
|
118
|
+
// For herd modules, sort both datasets by ID before comparison
|
|
119
|
+
let dataToCompare = newData;
|
|
120
|
+
let currentToCompare = currentData;
|
|
121
|
+
if (dataType.includes("Herd modules")) {
|
|
122
|
+
dataToCompare = sortHerdModulesById(newData);
|
|
123
|
+
currentToCompare = sortHerdModulesById(currentData);
|
|
124
|
+
// If we want to skip timestamp-only updates, normalize the data for comparison
|
|
125
|
+
if (skipTimestampOnlyUpdates) {
|
|
126
|
+
dataToCompare = normalizeHerdModulesForComparison(dataToCompare);
|
|
127
|
+
currentToCompare =
|
|
128
|
+
normalizeHerdModulesForComparison(currentToCompare);
|
|
154
129
|
}
|
|
155
|
-
|
|
130
|
+
}
|
|
131
|
+
if (!deepEqual(dataToCompare, currentToCompare)) {
|
|
132
|
+
console.log(`[useScoutRefresh] ${dataType} business data changed, updating store`);
|
|
133
|
+
dispatch(actionCreator(newData)); // Always dispatch original unsorted data
|
|
156
134
|
return true;
|
|
157
135
|
}
|
|
158
136
|
else {
|
|
159
|
-
console.log(`[useScoutRefresh] ${dataType} data unchanged, skipping store update`);
|
|
137
|
+
console.log(`[useScoutRefresh] ${dataType} business data unchanged, skipping store update`);
|
|
160
138
|
return false;
|
|
161
139
|
}
|
|
162
|
-
}, [
|
|
140
|
+
}, [
|
|
141
|
+
dispatch,
|
|
142
|
+
deepEqual,
|
|
143
|
+
sortHerdModulesById,
|
|
144
|
+
normalizeHerdModulesForComparison,
|
|
145
|
+
]);
|
|
163
146
|
// Helper function to handle IndexedDB errors - memoized for stability
|
|
164
147
|
const handleIndexedDbError = useCallback(async (error, operation, retryFn) => {
|
|
165
148
|
if (error instanceof Error &&
|
|
@@ -266,7 +249,7 @@ export function useScoutRefresh(options = {}) {
|
|
|
266
249
|
}
|
|
267
250
|
});
|
|
268
251
|
}
|
|
269
|
-
// Conditionally update store with fresh background data
|
|
252
|
+
// Conditionally update store with fresh background data, skip timestamp-only changes
|
|
270
253
|
const currentHerdModules = store.getState().scout.herd_modules;
|
|
271
254
|
const currentUser = store.getState().scout.user;
|
|
272
255
|
conditionalDispatch(backgroundHerdModulesResult.data, currentHerdModules, setHerdModules, "Herd modules (background)", true);
|
|
@@ -378,7 +361,7 @@ export function useScoutRefresh(options = {}) {
|
|
|
378
361
|
await scoutCache.setHerdModules(compatible_new_herd_modules, cacheTtlMs);
|
|
379
362
|
});
|
|
380
363
|
}
|
|
381
|
-
// Step 4: Conditionally update store with fresh data
|
|
364
|
+
// Step 4: Conditionally update store with fresh data, skip timestamp-only changes
|
|
382
365
|
const dataProcessingStartTime = Date.now();
|
|
383
366
|
const currentHerdModules = store.getState().scout.herd_modules;
|
|
384
367
|
const currentUser = store.getState().scout.user;
|