refactor: clean up try_cache defaults and add SWR to sponsorship loaders

- Remove vestigial try_cache param from generate_qr_code (never used in body)
- Remove vestigial try_cache from ae_core_functions: load_ae_obj_id__site_domain,
  update_ae_obj_id_crud, update_ae_obj_id_crud_v2 (none referenced it in body)
- Add proper SWR pattern to load_ae_obj_id__sponsorship_cfg and
  load_ae_obj_id__sponsorship; change defaults from false to true
- Change load_ae_obj_id__event_file default try_cache from false to true
  (consistent with load_ae_obj_li__event_file)
- Change load_ae_obj_id__hosted_file default try_cache from false to true
  (consistent with load_ae_obj_li__hosted_file)
- Remove stale try_cache arg from element_ae_crud.svelte caller
This commit is contained in:
Scott Idem
2026-03-11 14:57:23 -04:00
parent 50a20ebc44
commit fde3801ea6
6 changed files with 82 additions and 61 deletions

View File

@@ -52,13 +52,11 @@ const ae_promises: key_val = {}; // Promise<any>;
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) {

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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;

View File

@@ -188,11 +188,11 @@ async function _process_generic_props<T extends Record<string, any>>({
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<ae_SponsorshipCfg | null> {
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<ae_Sponsorship | null> {
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;
}

View File

@@ -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) {