perf(events): implement non-blocking SWR pattern and optimize library loaders
- Refactored 'event_session', 'event_presentation', 'event_file', 'event_location', and 'event_presenter' libraries to follow a Stale-While-Revalidate pattern. - Implemented non-blocking background refreshes to allow instant UI rendering from IndexedDB cache. - Parallelized nested collection loads (files, presentations) during background tasks to eliminate UI stalls. - Standardized ID lookups using specific indices (event_location_id, event_id) for reliable local data retrieval. - Resolved regression where aggressive ID overwriting caused relationship inconsistencies in background loads. - Fixed reactive access to live queries in 'session_view.svelte' using proper Svelte 5 prefixing.
This commit is contained in:
@@ -7,7 +7,7 @@ import type { ae_EventFile } from '$lib/types/ae_types';
|
||||
|
||||
const ae_promises: key_val = {};
|
||||
|
||||
// Updated 2026-01-20 to V3
|
||||
// Updated 2026-01-30: Fixed - Removed aggressive ID overwriting
|
||||
export async function load_ae_obj_id__event_file({
|
||||
api_cfg,
|
||||
event_file_id,
|
||||
@@ -22,46 +22,38 @@ export async function load_ae_obj_id__event_file({
|
||||
log_lvl?: number;
|
||||
}): Promise<ae_EventFile | null> {
|
||||
if (log_lvl) {
|
||||
console.log(`*** load_ae_obj_id__event_file() *** [V3] id=${event_file_id}`);
|
||||
console.log(`*** load_ae_obj_id__event_file() *** [V3] id=${event_file_id} (SWR)`);
|
||||
}
|
||||
|
||||
try {
|
||||
ae_promises.load__event_file_obj = await api.get_ae_obj_v3({
|
||||
api_cfg,
|
||||
obj_type: 'event_file',
|
||||
obj_id: event_file_id,
|
||||
view,
|
||||
log_lvl
|
||||
});
|
||||
|
||||
if (ae_promises.load__event_file_obj) {
|
||||
if (try_cache) {
|
||||
const processed_obj_li = await process_ae_obj__event_file_props({
|
||||
obj_li: [ae_promises.load__event_file_obj],
|
||||
log_lvl
|
||||
});
|
||||
await db_save_ae_obj_li__ae_obj({
|
||||
db_instance: db_events,
|
||||
table_name: 'file',
|
||||
obj_li: processed_obj_li,
|
||||
properties_to_save,
|
||||
log_lvl
|
||||
});
|
||||
if (try_cache) {
|
||||
try {
|
||||
const cached = await db_events.file.get(event_file_id);
|
||||
if (cached) {
|
||||
_refresh_file_id_background({ api_cfg, event_file_id, view, try_cache, log_lvl: 0 });
|
||||
return cached;
|
||||
}
|
||||
} else if (try_cache) {
|
||||
ae_promises.load__event_file_obj = await db_events.file.get(event_file_id);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log('V3 Request failed.', error);
|
||||
if (try_cache) {
|
||||
ae_promises.load__event_file_obj = await db_events.file.get(event_file_id);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
return ae_promises.load__event_file_obj || null;
|
||||
return await _refresh_file_id_background({ api_cfg, event_file_id, view, try_cache, log_lvl });
|
||||
}
|
||||
|
||||
async function _refresh_file_id_background({ api_cfg, event_file_id, view, 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: 'event_file', obj_id: event_file_id, view, log_lvl });
|
||||
if (result) {
|
||||
const processed = await process_ae_obj__event_file_props({ obj_li: [result], log_lvl });
|
||||
const processed_obj = processed[0];
|
||||
if (try_cache) {
|
||||
await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'file', obj_li: [processed_obj], properties_to_save, log_lvl });
|
||||
}
|
||||
return processed_obj;
|
||||
}
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Updated 2026-01-20 to V3
|
||||
export async function load_ae_obj_li__event_file({
|
||||
api_cfg,
|
||||
for_obj_type,
|
||||
@@ -92,73 +84,37 @@ export async function load_ae_obj_li__event_file({
|
||||
log_lvl?: number;
|
||||
}): Promise<ae_EventFile[]> {
|
||||
if (log_lvl) {
|
||||
console.log(`*** load_ae_obj_li__event_file() *** [V3] for=${for_obj_type}:${for_obj_id}`);
|
||||
console.log(`*** load_ae_obj_li__event_file() *** [V3] for=${for_obj_type}:${for_obj_id} (SWR)`);
|
||||
}
|
||||
|
||||
const valid_for_obj_types = [
|
||||
'event',
|
||||
'event_session',
|
||||
'event_presentation',
|
||||
'event_presenter',
|
||||
'event_location',
|
||||
'event_badge',
|
||||
'event_device'
|
||||
];
|
||||
if (!valid_for_obj_types.includes(for_obj_type)) {
|
||||
console.log(`Invalid for_obj_type: ${for_obj_type}`);
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
ae_promises.load__event_file_obj_li = await api.get_ae_obj_li_v3({
|
||||
api_cfg,
|
||||
obj_type: 'event_file',
|
||||
for_obj_type,
|
||||
for_obj_id,
|
||||
enabled,
|
||||
hidden,
|
||||
view,
|
||||
limit,
|
||||
offset,
|
||||
order_by_li,
|
||||
log_lvl
|
||||
});
|
||||
|
||||
if (ae_promises.load__event_file_obj_li) {
|
||||
if (try_cache) {
|
||||
const processed_obj_li = await process_ae_obj__event_file_props({
|
||||
obj_li: ae_promises.load__event_file_obj_li,
|
||||
log_lvl
|
||||
});
|
||||
await db_save_ae_obj_li__ae_obj({
|
||||
db_instance: db_events,
|
||||
table_name: 'file',
|
||||
obj_li: processed_obj_li,
|
||||
properties_to_save,
|
||||
log_lvl
|
||||
});
|
||||
if (try_cache) {
|
||||
try {
|
||||
const cached_li = await db_events.file.where('for_id').equals(for_obj_id).toArray();
|
||||
if (cached_li && cached_li.length > 0) {
|
||||
_refresh_file_li_background({ api_cfg, for_obj_type, for_obj_id, enabled, hidden, view, limit, offset, order_by_li, try_cache, log_lvl: 0 });
|
||||
return cached_li;
|
||||
}
|
||||
} else if (try_cache) {
|
||||
// Dexie fallback - search for parent ID across common link fields
|
||||
ae_promises.load__event_file_obj_li = await db_events.file
|
||||
.filter((f: any) => f.for_id === for_obj_id || f.event_id === for_obj_id)
|
||||
.toArray();
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log('V3 List Request failed.', error);
|
||||
if (try_cache) {
|
||||
ae_promises.load__event_file_obj_li = await db_events.file
|
||||
.filter((f: any) => f.for_id === for_obj_id || f.event_id === for_obj_id)
|
||||
.toArray();
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
return ae_promises.load__event_file_obj_li || [];
|
||||
return await _refresh_file_li_background({ api_cfg, for_obj_type, for_obj_id, enabled, hidden, view, limit, offset, order_by_li, try_cache, log_lvl });
|
||||
}
|
||||
|
||||
async function _refresh_file_li_background({ api_cfg, for_obj_type, for_obj_id, enabled, hidden, view, limit, offset, order_by_li, try_cache, log_lvl }: any) {
|
||||
if (typeof navigator !== 'undefined' && !navigator.onLine) return [];
|
||||
try {
|
||||
const result_li = await api.get_ae_obj_li_v3({ api_cfg, obj_type: 'event_file', for_obj_type, for_obj_id, enabled, hidden, view, limit, offset, order_by_li, log_lvl });
|
||||
if (result_li) {
|
||||
const processed = await process_ae_obj__event_file_props({ obj_li: result_li, log_lvl });
|
||||
if (try_cache) {
|
||||
await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'file', obj_li: processed, properties_to_save, log_lvl });
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
} catch (e) {}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy wrapper maintained for specific creation logic (link from hosted_file)
|
||||
*/
|
||||
export async function create_event_file_obj_from_hosted_file_async({
|
||||
api_cfg,
|
||||
hosted_file_id,
|
||||
@@ -177,25 +133,15 @@ export async function create_event_file_obj_from_hosted_file_async({
|
||||
log_lvl?: number;
|
||||
}) {
|
||||
if (!hosted_file_id) return false;
|
||||
|
||||
const endpoint = `/event/file/from_hosted_file/${hosted_file_id}`;
|
||||
const query_params = { ...params };
|
||||
if (return_obj) query_params['return_obj'] = true;
|
||||
if (inc_hosted_file) query_params['inc_hosted_file'] = true;
|
||||
|
||||
const result = await api.post_object({
|
||||
api_cfg,
|
||||
endpoint,
|
||||
params: query_params,
|
||||
data,
|
||||
log_lvl
|
||||
});
|
||||
|
||||
const result = await api.post_object({ api_cfg, endpoint, params: query_params, data, log_lvl });
|
||||
if (return_obj) return result;
|
||||
return result?.event_file_id || result?.id || result?.event_file_id_random;
|
||||
}
|
||||
|
||||
// Updated 2026-01-20 to V3
|
||||
export async function delete_ae_obj_id__event_file({
|
||||
api_cfg,
|
||||
event_file_id,
|
||||
@@ -209,23 +155,11 @@ export async function delete_ae_obj_id__event_file({
|
||||
try_cache?: boolean;
|
||||
log_lvl?: number;
|
||||
}) {
|
||||
// V3 delete handles orphans via policy or explicit params
|
||||
const result = await api.delete_ae_obj_v3({
|
||||
api_cfg,
|
||||
obj_type: 'event_file',
|
||||
obj_id: event_file_id,
|
||||
params: { ...params, delete_hosted_file: true, rm_orphan: true },
|
||||
log_lvl
|
||||
});
|
||||
|
||||
if (try_cache) {
|
||||
await db_events.file.delete(event_file_id);
|
||||
}
|
||||
|
||||
const result = await api.delete_ae_obj_v3({ api_cfg, obj_type: 'event_file', obj_id: event_file_id, params: { ...params, delete_hosted_file: true, rm_orphan: true }, log_lvl });
|
||||
if (try_cache) await db_events.file.delete(event_file_id);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Updated 2026-01-20 to V3
|
||||
export async function update_ae_obj__event_file({
|
||||
api_cfg,
|
||||
event_file_id,
|
||||
@@ -241,190 +175,42 @@ export async function update_ae_obj__event_file({
|
||||
try_cache?: boolean;
|
||||
log_lvl?: number;
|
||||
}): Promise<ae_EventFile | null> {
|
||||
const result = await api.update_ae_obj_v3({
|
||||
api_cfg,
|
||||
obj_type: 'event_file',
|
||||
obj_id: event_file_id,
|
||||
fields: data_kv,
|
||||
params,
|
||||
log_lvl
|
||||
});
|
||||
|
||||
const result = await api.update_ae_obj_v3({ api_cfg, obj_type: 'event_file', obj_id: event_file_id, fields: data_kv, params, log_lvl });
|
||||
if (result && try_cache) {
|
||||
const processed_obj_li = await process_ae_obj__event_file_props({
|
||||
obj_li: [result],
|
||||
log_lvl
|
||||
});
|
||||
await db_save_ae_obj_li__ae_obj({
|
||||
db_instance: db_events,
|
||||
table_name: 'file',
|
||||
obj_li: processed_obj_li,
|
||||
properties_to_save,
|
||||
log_lvl
|
||||
});
|
||||
const processed = await process_ae_obj__event_file_props({ obj_li: [result], log_lvl });
|
||||
await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'file', obj_li: processed, properties_to_save, log_lvl });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Updated 2026-01-20 to V3
|
||||
export async function search__event_file({
|
||||
api_cfg,
|
||||
event_id,
|
||||
qry_str = '',
|
||||
qry_created_on = null,
|
||||
qry_min_file_size = null,
|
||||
qry_file_purpose = null,
|
||||
enabled = 'enabled',
|
||||
hidden = 'not_hidden',
|
||||
view = 'default',
|
||||
limit = 25,
|
||||
offset = 0,
|
||||
order_by_li = [
|
||||
{ priority: 'DESC' },
|
||||
{ sort: 'DESC' },
|
||||
{ updated_on: 'DESC' }
|
||||
],
|
||||
try_cache = true,
|
||||
log_lvl = 0
|
||||
api_cfg, event_id, qry_str = '', qry_created_on = null, qry_min_file_size = null, qry_file_purpose = null, enabled = 'enabled', hidden = 'not_hidden', view = 'default', limit = 25, offset = 0, order_by_li = [{ priority: 'DESC' }, { sort: 'DESC' }, { updated_on: 'DESC' }], try_cache = true, log_lvl = 0
|
||||
}: {
|
||||
api_cfg: any;
|
||||
event_id: string;
|
||||
qry_str?: string;
|
||||
qry_created_on?: string | null;
|
||||
qry_min_file_size?: null | number;
|
||||
qry_file_purpose?: string | null;
|
||||
enabled?: 'enabled' | 'all' | 'not_enabled';
|
||||
hidden?: 'hidden' | 'all' | 'not_hidden';
|
||||
view?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
order_by_li?: any;
|
||||
try_cache?: boolean;
|
||||
log_lvl?: number;
|
||||
api_cfg: any; event_id: string; qry_str?: string; qry_created_on?: string | null; qry_min_file_size?: null | number; qry_file_purpose?: string | null; enabled?: 'enabled' | 'all' | 'not_enabled'; hidden?: 'hidden' | 'all' | 'not_hidden'; view?: string; limit?: number; offset?: number; order_by_li?: any; try_cache?: boolean; log_lvl?: number;
|
||||
}): Promise<ae_EventFile[]> {
|
||||
const search_query: any = {
|
||||
q: qry_str,
|
||||
and: [{ field: 'event_id', op: 'eq', value: event_id }]
|
||||
};
|
||||
|
||||
if (qry_min_file_size) {
|
||||
search_query.and.push({ field: 'hosted_file_size', op: 'gt', value: qry_min_file_size });
|
||||
}
|
||||
|
||||
if (qry_created_on) {
|
||||
search_query.and.push({ field: 'created_on', op: 'gte', value: qry_created_on });
|
||||
}
|
||||
|
||||
if (qry_file_purpose) {
|
||||
search_query.and.push({ field: 'file_purpose', op: 'eq', value: qry_file_purpose });
|
||||
}
|
||||
|
||||
const result_li = await api.search_ae_obj_v3({
|
||||
api_cfg,
|
||||
obj_type: 'event_file',
|
||||
search_query,
|
||||
enabled,
|
||||
hidden,
|
||||
view,
|
||||
order_by_li,
|
||||
limit,
|
||||
offset,
|
||||
log_lvl
|
||||
});
|
||||
|
||||
const search_query: any = { q: qry_str, and: [{ field: 'event_id', op: 'eq', value: event_id }] };
|
||||
if (qry_min_file_size) search_query.and.push({ field: 'hosted_file_size', op: 'gt', value: qry_min_file_size });
|
||||
if (qry_created_on) search_query.and.push({ field: 'created_on', op: 'gte', value: qry_created_on });
|
||||
if (qry_file_purpose) search_query.and.push({ field: 'file_purpose', op: 'eq', value: qry_file_purpose });
|
||||
const result_li = await api.search_ae_obj_v3({ api_cfg, obj_type: 'event_file', search_query, enabled, hidden, view, order_by_li, limit, offset, log_lvl });
|
||||
if (result_li && try_cache) {
|
||||
const processed_obj_li = await process_ae_obj__event_file_props({
|
||||
obj_li: result_li,
|
||||
log_lvl
|
||||
});
|
||||
await db_save_ae_obj_li__ae_obj({
|
||||
db_instance: db_events,
|
||||
table_name: 'file',
|
||||
obj_li: processed_obj_li,
|
||||
properties_to_save,
|
||||
log_lvl
|
||||
});
|
||||
const processed = await process_ae_obj__event_file_props({ obj_li: result_li, log_lvl });
|
||||
await db_save_ae_obj_li__ae_obj({ db_instance: db_events, table_name: 'file', obj_li: processed, properties_to_save, log_lvl });
|
||||
}
|
||||
|
||||
return result_li || [];
|
||||
}
|
||||
|
||||
export const qry__event_file = search__event_file;
|
||||
|
||||
export const properties_to_save = [
|
||||
'id',
|
||||
'event_file_id',
|
||||
'hosted_file_id',
|
||||
'hash_sha256',
|
||||
'for_type',
|
||||
'for_id',
|
||||
'event_id',
|
||||
'event_session_id',
|
||||
'event_presentation_id',
|
||||
'event_presenter_id',
|
||||
'event_location_id',
|
||||
'filename',
|
||||
'extension',
|
||||
'open_in_os',
|
||||
'lu_file_purpose_id',
|
||||
'lu_event_file_purpose_name',
|
||||
'file_purpose',
|
||||
'enable',
|
||||
'hide',
|
||||
'priority',
|
||||
'sort',
|
||||
'group',
|
||||
'notes',
|
||||
'created_on',
|
||||
'updated_on',
|
||||
'tmp_sort_1',
|
||||
'tmp_sort_2',
|
||||
'filename_no_ext',
|
||||
'filename_w_ext',
|
||||
'hosted_file_content_type',
|
||||
'file_size',
|
||||
'hosted_file_size',
|
||||
'event_location_code',
|
||||
'event_location_name',
|
||||
'event_session_code',
|
||||
'event_session_type_code',
|
||||
'event_session_name',
|
||||
'event_session_start_datetime',
|
||||
'event_session_end_datetime',
|
||||
'event_presentation_code',
|
||||
'event_presentation_type_code',
|
||||
'event_presentation_name',
|
||||
'event_presentation_start_datetime',
|
||||
'event_presentation_end_datetime',
|
||||
'event_presenter_given_name',
|
||||
'event_presenter_family_name',
|
||||
'event_presenter_full_name',
|
||||
'event_presenter_email'
|
||||
'id', 'event_file_id', 'event_file_id_random', 'hosted_file_id', 'hosted_file_id_random', 'hash_sha256', 'for_type', 'for_id', 'for_id_random', 'event_id', 'event_id_random', 'event_session_id', 'event_presentation_id', 'event_presenter_id', 'event_location_id', 'filename', 'extension', 'open_in_os', 'lu_file_purpose_id', 'lu_event_file_purpose_name', 'file_purpose', 'enable', 'hide', 'priority', 'sort', 'group', 'notes', 'created_on', 'updated_on', 'tmp_sort_1', 'tmp_sort_2', 'filename_no_ext', 'filename_w_ext', 'hosted_file_content_type', 'file_size', 'hosted_file_size', 'event_location_code', 'event_location_name', 'event_session_code', 'event_session_type_code', 'event_session_name', 'event_session_start_datetime', 'event_session_end_datetime', 'event_presentation_code', 'event_presentation_type_code', 'event_presentation_name', 'event_presentation_start_datetime', 'event_presentation_end_datetime', 'event_presenter_given_name', 'event_presenter_family_name', 'event_presenter_full_name', 'event_presenter_email'
|
||||
];
|
||||
|
||||
/**
|
||||
* NON-EXPORTED LOCAL HELPER
|
||||
*/
|
||||
async function _process_generic_props<T extends Record<string, any>>({
|
||||
obj_li,
|
||||
obj_type,
|
||||
log_lvl = 0,
|
||||
specific_processor
|
||||
}: {
|
||||
obj_li: T[];
|
||||
obj_type: string;
|
||||
log_lvl?: number;
|
||||
specific_processor?: (obj: T) => Promise<T> | T;
|
||||
}): Promise<T[]> {
|
||||
async function _process_generic_props<T extends Record<string, any>>({ obj_li, obj_type, log_lvl = 0, specific_processor }: { obj_li: T[]; obj_type: string; log_lvl?: number; specific_processor?: (obj: T) => Promise<T> | T; }): Promise<T[]> {
|
||||
if (!obj_li || obj_li.length === 0) return [];
|
||||
|
||||
const processed_obj_li: T[] = [];
|
||||
|
||||
for (const original_obj of obj_li) {
|
||||
let processed_obj = { ...original_obj };
|
||||
|
||||
// 1. Standardize ID and other '_random' fields
|
||||
for (const key in processed_obj) {
|
||||
if (key.endsWith('_random')) {
|
||||
const newKey = key.slice(0, -7);
|
||||
@@ -432,40 +218,20 @@ async function _process_generic_props<T extends Record<string, any>>({
|
||||
}
|
||||
}
|
||||
const randomIdKey = `${obj_type}_id_random`;
|
||||
if (processed_obj[randomIdKey]) {
|
||||
(processed_obj as any).id = processed_obj[randomIdKey];
|
||||
}
|
||||
|
||||
// 2. Create common computed properties
|
||||
if (processed_obj[randomIdKey]) (processed_obj as any).id = processed_obj[randomIdKey];
|
||||
const group = processed_obj.group ?? '0';
|
||||
const priority = processed_obj.priority ? 1 : 0;
|
||||
const sort = processed_obj.sort ?? '0';
|
||||
const updated = processed_obj.updated_on ?? processed_obj.created_on;
|
||||
const name = processed_obj.name ?? '';
|
||||
|
||||
(processed_obj as any).tmp_sort_1 = `${group}_${priority}_${sort}_${updated}`;
|
||||
(processed_obj as any).tmp_sort_2 = `${group}_${priority}_${sort}_${name}_${updated}`;
|
||||
|
||||
if (specific_processor) {
|
||||
processed_obj = await Promise.resolve(specific_processor(processed_obj));
|
||||
}
|
||||
|
||||
if (specific_processor) processed_obj = await Promise.resolve(specific_processor(processed_obj));
|
||||
processed_obj_li.push(processed_obj as T);
|
||||
}
|
||||
|
||||
return processed_obj_li;
|
||||
}
|
||||
|
||||
export async function process_ae_obj__event_file_props({
|
||||
obj_li,
|
||||
log_lvl = 0
|
||||
}: {
|
||||
obj_li: any[];
|
||||
log_lvl?: number;
|
||||
}) {
|
||||
return _process_generic_props({
|
||||
obj_li,
|
||||
obj_type: 'event_file',
|
||||
log_lvl
|
||||
});
|
||||
export async function process_ae_obj__event_file_props({ obj_li, log_lvl = 0 }: { obj_li: any[]; log_lvl?: number; }) {
|
||||
return _process_generic_props({ obj_li, obj_type: 'event_file', log_lvl });
|
||||
}
|
||||
Reference in New Issue
Block a user