perf(launcher): instrument SWR tracing and optimize session loading flow
This commit is contained in:
@@ -10,7 +10,7 @@ import { load_ae_obj_li__event_presentation } from '$lib/ae_events/ae_events__ev
|
||||
|
||||
const ae_promises: key_val = {};
|
||||
|
||||
// Updated 2026-01-30: Fixed - Removed aggressive ID overwriting
|
||||
// Updated 2026-01-30: Trace-Ready SWR Pattern
|
||||
export async function load_ae_obj_id__event_session({
|
||||
api_cfg,
|
||||
event_session_id,
|
||||
@@ -41,56 +41,91 @@ export async function load_ae_obj_id__event_session({
|
||||
log_lvl?: number;
|
||||
}): Promise<ae_EventSession | null> {
|
||||
if (log_lvl) {
|
||||
console.log(`*** load_ae_obj_id__event_session() *** [V3] id=${event_session_id} (SWR)`);
|
||||
console.log(`🔎 [Trace] load_ae_obj_id__event_session: START (id=${event_session_id}, try_cache=${try_cache})`);
|
||||
}
|
||||
|
||||
// 1. FAST PATH: Return cached data immediately
|
||||
if (try_cache) {
|
||||
try {
|
||||
const cached = await db_events.session.get(event_session_id);
|
||||
if (cached) {
|
||||
if (log_lvl) console.log(`✅ [Trace] load_ae_obj_id: CACHE HIT. Returning stale data for id=${event_session_id}`);
|
||||
|
||||
// Background refresh & nested loads (non-blocking)
|
||||
_refresh_session_id_background({
|
||||
api_cfg, event_session_id, view, try_cache,
|
||||
inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li,
|
||||
enabled, hidden, limit, offset, log_lvl: 0
|
||||
enabled, hidden, limit, offset, log_lvl: log_lvl > 1 ? log_lvl : 0
|
||||
});
|
||||
|
||||
return await _handle_nested_loads(cached, { api_cfg, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, try_cache, log_lvl: 0 });
|
||||
} else if (log_lvl) {
|
||||
console.log(`⏳ [Trace] load_ae_obj_id: CACHE MISS for id=${event_session_id}`);
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
if (log_lvl) console.error(`❌ [Trace] load_ae_obj_id: Cache access error:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. SLOW PATH: Wait for API
|
||||
if (log_lvl) console.log(`🚀 [Trace] load_ae_obj_id: Proceeding to API path for id=${event_session_id}`);
|
||||
return await _refresh_session_id_background({ api_cfg, event_session_id, view, try_cache, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, log_lvl });
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal background refresh for a single session
|
||||
*/
|
||||
async function _refresh_session_id_background({ api_cfg, event_session_id, view, try_cache, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, log_lvl }: any) {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) return null;
|
||||
try {
|
||||
if (log_lvl) console.log(`📡 [Trace] _refresh_session_id: API Fetching id=${event_session_id}`);
|
||||
const result = await api.get_ae_obj_v3({ api_cfg, obj_type: 'event_session', obj_id: event_session_id, view, log_lvl });
|
||||
|
||||
if (result) {
|
||||
const processed = await process_ae_obj__event_session_props({ obj_li: [result], log_lvl });
|
||||
const processed_obj = processed[0];
|
||||
|
||||
if (log_lvl) console.log(`📦 [Trace] _refresh_session_id: Received from API (id=${processed_obj.id})`);
|
||||
|
||||
if (try_cache) {
|
||||
await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'session', obj_li: [processed_obj], properties_to_save, log_lvl });
|
||||
if (log_lvl) console.log(`💾 [Trace] _refresh_session_id: Saved to IDB cache.`);
|
||||
}
|
||||
return await _handle_nested_loads(processed_obj, { api_cfg, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, try_cache: false, log_lvl });
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
if (log_lvl) console.error(`❌ [Trace] _refresh_session_id: API error for id=${event_session_id}:`, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to handle nested collection loads for a session
|
||||
*/
|
||||
async function _handle_nested_loads(session_obj: any, { api_cfg, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, try_cache, log_lvl }: any) {
|
||||
const current_session_id = session_obj.id || session_obj.event_session_id;
|
||||
if (!current_session_id) return session_obj;
|
||||
|
||||
const tasks = [];
|
||||
if (inc_file_li) {
|
||||
tasks.push(load_ae_obj_li__event_file({ api_cfg, for_obj_type: 'event_session', for_obj_id: current_session_id, enabled, limit: 15, try_cache, log_lvl }).then(res => session_obj.event_file_li = res));
|
||||
tasks.push(load_ae_obj_li__event_file({
|
||||
api_cfg, for_obj_type: 'event_session', for_obj_id: current_session_id,
|
||||
enabled, limit: 15, try_cache, log_lvl
|
||||
}).then(res => session_obj.event_file_li = res));
|
||||
}
|
||||
|
||||
if (inc_presentation_li) {
|
||||
tasks.push(load_ae_obj_li__event_presentation({ api_cfg, for_obj_type: 'event_session', for_obj_id: current_session_id, inc_file_li: inc_all_file_li, inc_presenter_li, enabled, hidden, limit, offset, try_cache, log_lvl }).then(res => session_obj.event_presentation_li = res));
|
||||
tasks.push(load_ae_obj_li__event_presentation({
|
||||
api_cfg, for_obj_type: 'event_session', for_obj_id: current_session_id,
|
||||
inc_file_li: inc_all_file_li, inc_presenter_li, enabled, hidden, limit, offset, try_cache, log_lvl
|
||||
}).then(res => session_obj.event_presentation_li = res));
|
||||
}
|
||||
|
||||
if (tasks.length > 0) await Promise.all(tasks);
|
||||
return session_obj;
|
||||
}
|
||||
|
||||
// Updated 2026-01-30: Robust Cache Lookups with Tracing
|
||||
export async function load_ae_obj_li__event_session({
|
||||
api_cfg,
|
||||
for_obj_type = 'event',
|
||||
@@ -131,17 +166,32 @@ export async function load_ae_obj_li__event_session({
|
||||
log_lvl?: number;
|
||||
}): Promise<ae_EventSession[]> {
|
||||
if (log_lvl) {
|
||||
console.log(`*** load_ae_obj_li__event_session() *** [V3] for=${for_obj_type}:${for_obj_id} (SWR)`);
|
||||
console.log(`🔎 [Trace] load_ae_obj_li__event_session: START (for=${for_obj_type}:${for_obj_id}, try_cache=${try_cache})`);
|
||||
}
|
||||
|
||||
if (try_cache) {
|
||||
try {
|
||||
const cached_li = await db_events.session.where('event_location_id').equals(for_obj_id).toArray();
|
||||
// Robust lookup logic
|
||||
let query;
|
||||
if (for_obj_type === 'event_location') query = db_events.session.where('event_location_id').equals(for_obj_id);
|
||||
else if (for_obj_type === 'event') query = db_events.session.where('event_id').equals(for_obj_id);
|
||||
else query = db_events.session.where('for_id').equals(for_obj_id);
|
||||
|
||||
const cached_li = await query.toArray();
|
||||
|
||||
if (cached_li && cached_li.length > 0) {
|
||||
_refresh_session_li_background({ api_cfg, for_obj_type, for_obj_id, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, order_by_li, try_cache, log_lvl: 0 });
|
||||
if (log_lvl) console.log(`✅ [Trace] load_ae_obj_li: CACHE HIT (${cached_li.length} items).`);
|
||||
|
||||
// Background refresh (non-blocking)
|
||||
_refresh_session_li_background({ api_cfg, for_obj_type, for_obj_id, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, order_by_li, try_cache, log_lvl: log_lvl > 1 ? log_lvl : 0 });
|
||||
|
||||
return cached_li;
|
||||
} else if (log_lvl) {
|
||||
console.log(`⏳ [Trace] load_ae_obj_li: CACHE MISS for type=${for_obj_type} id=${for_obj_id}`);
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
if (log_lvl) console.error(`❌ [Trace] load_ae_obj_li: Cache access error:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
return await _refresh_session_li_background({ api_cfg, for_obj_type, for_obj_id, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, order_by_li, try_cache, log_lvl });
|
||||
@@ -150,15 +200,22 @@ export async function load_ae_obj_li__event_session({
|
||||
async function _refresh_session_li_background({ api_cfg, for_obj_type, for_obj_id, inc_file_li, inc_all_file_li, inc_presentation_li, inc_presenter_li, enabled, hidden, limit, offset, order_by_li, try_cache, log_lvl }: any) {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) return [];
|
||||
try {
|
||||
if (log_lvl) console.log(`📡 [Trace] _refresh_session_li: API Fetching for=${for_obj_type}:${for_obj_id}`);
|
||||
const result_li = await api.get_ae_obj_li_v3({ api_cfg, obj_type: 'event_session', for_obj_type, for_obj_id, enabled, hidden, limit, offset, order_by_li, log_lvl });
|
||||
|
||||
if (result_li) {
|
||||
const processed = await process_ae_obj__event_session_props({ obj_li: result_li, log_lvl });
|
||||
if (log_lvl) console.log(`📦 [Trace] _refresh_session_li: Received ${processed.length} items from API.`);
|
||||
|
||||
if (try_cache) {
|
||||
await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'session', obj_li: processed, properties_to_save, log_lvl });
|
||||
if (log_lvl) console.log(`💾 [Trace] _refresh_session_li: Saved to IDB cache.`);
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
if (log_lvl) console.error(`❌ [Trace] _refresh_session_li: API error:`, e);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user