@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.
@@ -91,75 +91,58 @@ export function useScoutRefresh(options = {}) {
91
91
  }
92
92
  return true;
93
93
  }, []);
94
- // Helper function to find differences between herd modules for debugging
95
- const findHerdModulesDifferences = useCallback((newData, currentData) => {
96
- if (!Array.isArray(newData) || !Array.isArray(currentData)) {
97
- return `Array type mismatch: new=${Array.isArray(newData)}, current=${Array.isArray(currentData)}`;
98
- }
99
- if (newData.length !== currentData.length) {
100
- return `Array length mismatch: new=${newData.length}, current=${currentData.length}`;
101
- }
102
- const differences = [];
103
- for (let i = 0; i < newData.length; i++) {
104
- const newHerd = newData[i];
105
- const currentHerd = currentData[i];
106
- if (!newHerd || !currentHerd) {
107
- differences.push(`Herd ${i}: null/undefined mismatch`);
108
- continue;
109
- }
110
- // Check top-level fields
111
- const fieldsToCheck = [
112
- "timestamp_last_refreshed",
113
- "total_events",
114
- "total_events_with_filters",
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, enableDebugging = false) => {
148
- if (!deepEqual(newData, currentData)) {
149
- console.log(`[useScoutRefresh] ${dataType} data changed, updating store`);
150
- // Add debugging for herd modules to see what actually changed
151
- if (enableDebugging && dataType.includes("Herd modules")) {
152
- const differences = findHerdModulesDifferences(newData, currentData);
153
- console.log(`[useScoutRefresh] ${dataType} differences: ${differences}`);
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
- dispatch(actionCreator(newData));
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
- }, [dispatch, deepEqual, findHerdModulesDifferences]);
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 using normalized comparison
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 using normalized comparison
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.0.111",
3
+ "version": "1.0.113",
4
4
  "description": "Core utilities and helpers for Adventure Labs Scout applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",