@adventurelabs/scout-core 1.0.79 → 1.0.80
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 +56 -13
- package/package.json +1 -1
|
@@ -77,7 +77,7 @@ export function useScoutRefresh(options = {}) {
|
|
|
77
77
|
if (cacheResult.data && cacheResult.data.length > 0) {
|
|
78
78
|
cachedHerdModules = cacheResult.data;
|
|
79
79
|
console.log(`[useScoutRefresh] Loaded ${cachedHerdModules.length} herd modules from cache in ${cacheLoadDuration}ms (age: ${Math.round(cacheResult.age / 1000)}s, stale: ${cacheResult.isStale})`);
|
|
80
|
-
// Set data source to CACHE
|
|
80
|
+
// Set data source to CACHE initially
|
|
81
81
|
dispatch(setDataSource(EnumDataSource.CACHE));
|
|
82
82
|
dispatch(setDataSourceInfo({
|
|
83
83
|
source: EnumDataSource.CACHE,
|
|
@@ -88,22 +88,65 @@ export function useScoutRefresh(options = {}) {
|
|
|
88
88
|
// Immediately update the store with cached data
|
|
89
89
|
dispatch(setHerdModules(cachedHerdModules));
|
|
90
90
|
dispatch(setHerdModulesLoadingState(EnumHerdModulesLoadingState.SUCCESSFULLY_LOADED));
|
|
91
|
-
//
|
|
91
|
+
// Always load user data from API
|
|
92
|
+
const userStartTime = Date.now();
|
|
93
|
+
const res_new_user = await server_get_user();
|
|
94
|
+
const userApiDuration = Date.now() - userStartTime;
|
|
95
|
+
timingRefs.current.userApiDuration = userApiDuration;
|
|
96
|
+
dispatch(setUserApiDuration(userApiDuration));
|
|
97
|
+
if (res_new_user && res_new_user.data) {
|
|
98
|
+
dispatch(setUser(res_new_user.data));
|
|
99
|
+
}
|
|
100
|
+
// If cache is fresh, we still background fetch but don't wait
|
|
92
101
|
if (!cacheResult.isStale) {
|
|
93
|
-
console.log("[useScoutRefresh] Cache is fresh,
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
console.log("[useScoutRefresh] Cache is fresh, background fetching fresh data...");
|
|
103
|
+
// Background fetch fresh data without blocking
|
|
104
|
+
(async () => {
|
|
105
|
+
try {
|
|
106
|
+
console.log("[useScoutRefresh] Starting background fetch...");
|
|
107
|
+
const backgroundStartTime = Date.now();
|
|
108
|
+
const [backgroundHerdModulesResult, backgroundUserResult] = await Promise.all([
|
|
109
|
+
server_load_herd_modules(),
|
|
110
|
+
server_get_user()
|
|
111
|
+
]);
|
|
112
|
+
const backgroundDuration = Date.now() - backgroundStartTime;
|
|
113
|
+
console.log(`[useScoutRefresh] Background fetch completed in ${backgroundDuration}ms`);
|
|
114
|
+
// Validate background responses
|
|
115
|
+
if (backgroundHerdModulesResult.data &&
|
|
116
|
+
Array.isArray(backgroundHerdModulesResult.data) &&
|
|
117
|
+
backgroundUserResult &&
|
|
118
|
+
backgroundUserResult.data) {
|
|
119
|
+
// Update cache with fresh data
|
|
120
|
+
try {
|
|
121
|
+
await scoutCache.setHerdModules(backgroundHerdModulesResult.data, cacheTtlMs);
|
|
122
|
+
console.log(`[useScoutRefresh] Background cache updated with TTL: ${Math.round(cacheTtlMs / 1000)}s`);
|
|
123
|
+
}
|
|
124
|
+
catch (cacheError) {
|
|
125
|
+
console.warn("[useScoutRefresh] Background cache save failed:", cacheError);
|
|
126
|
+
}
|
|
127
|
+
// Update store with fresh data
|
|
128
|
+
dispatch(setHerdModules(backgroundHerdModulesResult.data));
|
|
129
|
+
dispatch(setUser(backgroundUserResult.data));
|
|
130
|
+
// Update data source to DATABASE
|
|
131
|
+
dispatch(setDataSource(EnumDataSource.DATABASE));
|
|
132
|
+
dispatch(setDataSourceInfo({
|
|
133
|
+
source: EnumDataSource.DATABASE,
|
|
134
|
+
timestamp: Date.now(),
|
|
135
|
+
}));
|
|
136
|
+
console.log("[useScoutRefresh] Background fetch completed and store updated");
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
console.warn("[useScoutRefresh] Background fetch returned invalid data");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (backgroundError) {
|
|
143
|
+
console.warn("[useScoutRefresh] Background fetch failed:", backgroundError);
|
|
144
|
+
}
|
|
145
|
+
})();
|
|
103
146
|
const totalDuration = Date.now() - startTime;
|
|
104
147
|
dispatch(setHerdModulesLoadedInMs(totalDuration));
|
|
105
148
|
dispatch(setStatus(EnumScoutStateStatus.DONE_LOADING));
|
|
106
|
-
console.log(`[useScoutRefresh] Cache-first refresh completed in ${totalDuration}ms`);
|
|
149
|
+
console.log(`[useScoutRefresh] Cache-first refresh completed in ${totalDuration}ms (background fetch in progress)`);
|
|
107
150
|
onRefreshComplete?.();
|
|
108
151
|
return;
|
|
109
152
|
}
|