diff --git a/src/lib/ae_core/ae_core_functions.ts b/src/lib/ae_core/ae_core_functions.ts index cc3552d0..cbaf6493 100644 --- a/src/lib/ae_core/ae_core_functions.ts +++ b/src/lib/ae_core/ae_core_functions.ts @@ -52,13 +52,11 @@ const ae_promises: key_val = {}; // Promise; async function load_ae_obj_id__site_domain({ api_cfg, fqdn, - try_cache = false, timeout = 7000, log_lvl = 0 }: { api_cfg: any; fqdn: string; - try_cache?: boolean; timeout?: number; log_lvl?: number; }) { @@ -273,7 +271,6 @@ async function update_ae_obj_id_crud({ field_name, new_field_value, params = {}, - try_cache = false, log_lvl = 0 }: { api_cfg: any; @@ -282,9 +279,8 @@ async function update_ae_obj_id_crud({ object_reload?: boolean; field_name: string; new_field_value: any; - params: any | key_val; - try_cache: boolean; - log_lvl: number; + params?: any | key_val; + log_lvl?: number; }) { let patch_result: any = null; @@ -365,7 +361,6 @@ async function update_ae_obj_id_crud_v2({ field_name, new_field_value, params = {}, - try_cache = false, log_lvl = 0 }: { api_cfg: any; @@ -375,7 +370,6 @@ async function update_ae_obj_id_crud_v2({ field_name: string; new_field_value: any; params?: any | key_val; - try_cache?: boolean; log_lvl?: number; }) { if (log_lvl) { diff --git a/src/lib/ae_core/core__hosted_files.ts b/src/lib/ae_core/core__hosted_files.ts index fcdeb946..c8fd9c38 100644 --- a/src/lib/ae_core/core__hosted_files.ts +++ b/src/lib/ae_core/core__hosted_files.ts @@ -12,7 +12,7 @@ export async function load_ae_obj_id__hosted_file({ api_cfg, hosted_file_id, view = 'default', - try_cache = false, + try_cache = true, log_lvl = 0 }: { api_cfg: any; diff --git a/src/lib/ae_core/core__qr_code.ts b/src/lib/ae_core/core__qr_code.ts index 060bcafd..f18c4309 100644 --- a/src/lib/ae_core/core__qr_code.ts +++ b/src/lib/ae_core/core__qr_code.ts @@ -19,7 +19,6 @@ export async function generate_qr_code({ obj_id, str, // For encoding a string (like a URL) into a QR code. return_blob = true, // blob or url? - try_cache = false, log_lvl = 0 }: { api_cfg: any; @@ -31,7 +30,6 @@ export async function generate_qr_code({ obj_id?: string; str?: string; return_blob?: boolean; - try_cache?: boolean; log_lvl?: number; }) { if (log_lvl) { diff --git a/src/lib/ae_events/ae_events__event_file.ts b/src/lib/ae_events/ae_events__event_file.ts index 8e370e72..d55bdda0 100644 --- a/src/lib/ae_events/ae_events__event_file.ts +++ b/src/lib/ae_events/ae_events__event_file.ts @@ -13,7 +13,7 @@ export async function load_ae_obj_id__event_file({ event_file_id, inc_hosted_file = false, view = 'default', - try_cache = false, + try_cache = true, log_lvl = 0 }: { api_cfg: any; diff --git a/src/lib/ae_sponsorships/ae_sponsorships_functions.ts b/src/lib/ae_sponsorships/ae_sponsorships_functions.ts index a75c87cb..2f271c29 100644 --- a/src/lib/ae_sponsorships/ae_sponsorships_functions.ts +++ b/src/lib/ae_sponsorships/ae_sponsorships_functions.ts @@ -188,11 +188,11 @@ async function _process_generic_props>({ return processed_obj_li; } -// Updated 2026-01-20 to V3 +// Updated 2026-03-11: SWR pattern export async function load_ae_obj_id__sponsorship_cfg({ api_cfg, sponsorship_cfg_id, - try_cache = false, + try_cache = true, log_lvl = 0 }: { api_cfg: any; @@ -201,40 +201,55 @@ export async function load_ae_obj_id__sponsorship_cfg({ log_lvl?: number; }): Promise { if (log_lvl) { - console.log(`*** load_ae_obj_id__sponsorship_cfg() *** [V3] id=${sponsorship_cfg_id}`); + console.log(`*** load_ae_obj_id__sponsorship_cfg() *** [V3] id=${sponsorship_cfg_id} (SWR)`); } - const sponsorship_cfg_obj_get_result = await api.get_ae_obj_v3({ - api_cfg, - obj_type: 'sponsorship_cfg', - obj_id: sponsorship_cfg_id, - log_lvl - }); + // 1. FAST PATH: Cache hit + if (try_cache) { + try { + const cached = await db_sponsorships.cfg.get(sponsorship_cfg_id); + if (cached) { + _refresh_sponsorship_cfg_id_background({ api_cfg, sponsorship_cfg_id, try_cache, log_lvl: 0 }); + return cached as unknown as ae_SponsorshipCfg; + } + } catch (e) {} + } - if (sponsorship_cfg_obj_get_result) { - if (try_cache) { - const processed_obj_li = await process_ae_obj__sponsorship_cfg_props({ - obj_li: [sponsorship_cfg_obj_get_result], - log_lvl - }); - await db_save_ae_obj_li__ae_obj({ - db_instance: db_sponsorships, - table_name: 'cfg', - obj_li: processed_obj_li, - properties_to_save: properties_to_save_sponsorship_cfg, - log_lvl - }); + // 2. SLOW PATH: Wait for API + return await _refresh_sponsorship_cfg_id_background({ api_cfg, sponsorship_cfg_id, try_cache, log_lvl }); +} + +async function _refresh_sponsorship_cfg_id_background({ api_cfg, sponsorship_cfg_id, try_cache, log_lvl }: any) { + if (typeof navigator !== 'undefined' && !navigator.onLine) return null; + try { + const result = await api.get_ae_obj_v3({ + api_cfg, + obj_type: 'sponsorship_cfg', + obj_id: sponsorship_cfg_id, + log_lvl + }); + if (result) { + if (try_cache) { + const processed_obj_li = await process_ae_obj__sponsorship_cfg_props({ obj_li: [result], log_lvl }); + await db_save_ae_obj_li__ae_obj({ + db_instance: db_sponsorships, + table_name: 'cfg', + obj_li: processed_obj_li, + properties_to_save: properties_to_save_sponsorship_cfg, + log_lvl + }); + } + return result; } - return sponsorship_cfg_obj_get_result; - } + } catch (e) {} return null; } -// Updated 2026-01-20 to V3 +// Updated 2026-03-11: SWR pattern export async function load_ae_obj_id__sponsorship({ api_cfg, sponsorship_id, - try_cache = false, + try_cache = true, log_lvl = 0 }: { api_cfg: any; @@ -243,32 +258,47 @@ export async function load_ae_obj_id__sponsorship({ log_lvl?: number; }): Promise { if (log_lvl) { - console.log(`*** load_ae_obj_id__sponsorship() *** [V3] id=${sponsorship_id}`); + console.log(`*** load_ae_obj_id__sponsorship() *** [V3] id=${sponsorship_id} (SWR)`); } - const sponsorship_obj_get_result = await api.get_ae_obj_v3({ - api_cfg, - obj_type: 'sponsorship', - obj_id: sponsorship_id, - log_lvl - }); + // 1. FAST PATH: Cache hit + if (try_cache) { + try { + const cached = await db_sponsorships.sponsorship.get(sponsorship_id); + if (cached) { + _refresh_sponsorship_id_background({ api_cfg, sponsorship_id, try_cache, log_lvl: 0 }); + return cached as unknown as ae_Sponsorship; + } + } catch (e) {} + } - if (sponsorship_obj_get_result) { - if (try_cache) { - const processed_obj_li = await process_ae_obj__sponsorship_props({ - obj_li: [sponsorship_obj_get_result], - log_lvl - }); - await db_save_ae_obj_li__ae_obj({ - db_instance: db_sponsorships, - table_name: 'sponsorship', - obj_li: processed_obj_li, - properties_to_save: properties_to_save_sponsorship, - log_lvl - }); + // 2. SLOW PATH: Wait for API + return await _refresh_sponsorship_id_background({ api_cfg, sponsorship_id, try_cache, log_lvl }); +} + +async function _refresh_sponsorship_id_background({ api_cfg, sponsorship_id, try_cache, log_lvl }: any) { + if (typeof navigator !== 'undefined' && !navigator.onLine) return null; + try { + const result = await api.get_ae_obj_v3({ + api_cfg, + obj_type: 'sponsorship', + obj_id: sponsorship_id, + log_lvl + }); + if (result) { + if (try_cache) { + const processed_obj_li = await process_ae_obj__sponsorship_props({ obj_li: [result], log_lvl }); + await db_save_ae_obj_li__ae_obj({ + db_instance: db_sponsorships, + table_name: 'sponsorship', + obj_li: processed_obj_li, + properties_to_save: properties_to_save_sponsorship, + log_lvl + }); + } + return result; } - return sponsorship_obj_get_result; - } + } catch (e) {} return null; } diff --git a/src/lib/elements/element_ae_crud.svelte b/src/lib/elements/element_ae_crud.svelte index 9c99c182..e4ed79e0 100644 --- a/src/lib/elements/element_ae_crud.svelte +++ b/src/lib/elements/element_ae_crud.svelte @@ -125,7 +125,6 @@ field_name: field_name, new_field_value: new_field_value, params: {}, - try_cache: true, // Defaulting to true as per project preference log_lvl: 0 }) .then(function (results: any) {