All files / lib sessions.js

47.61% Statements 10/21
0% Branches 0/4
50% Functions 2/4
52.63% Lines 10/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                          1x       1x   1x   1x 1x     1x   1x 2x 2x 2x                                          
import * as idb from '$lib/idb.svelte.js';
 
import { imageIdToFileId } from './images.js';
import { clearMetadataOptionsCache } from './metadata/storage.js';
import { withQueue } from './queue.svelte.js';
import { uiState } from './state.svelte.js';
import { undo } from './undo.svelte.js';
 
/**
 *
 * @param {string} sessionId
 */
export async function deleteSession(sessionId) {
	await idb.openTransaction(
		['Session', 'Observation', 'Image', 'ImageFile', 'ImagePreviewFile'],
		{ mode: 'readwrite' },
		async (tx) => {
			tx.objectStore('Session').delete(sessionId);
 
			const obs = await tx.objectStore('Observation').index('sessionId').getAll(sessionId);
 
			for (const o of obs) {
				tx.objectStore('Observation').delete(o.id);
			}
 
			const imgs = await tx.objectStore('Image').index('sessionId').getAll(sessionId);
 
			for (const img of imgs) {
				tx.objectStore('Image').delete(img.id);
				tx.objectStore('ImageFile').delete(imageIdToFileId(img.id));
				tx.objectStore('ImagePreviewFile').delete(imageIdToFileId(img.id));
			}
		}
	);
}
 
/**
 * @param {string | null} id id of the session to switch to, or null to close it
 */
export async function switchSession(id) {
	const previousProtocolId = uiState.currentProtocolId;
	await uiState.setCurrentSession(id);
	uiState.clearPreviewURLs();
	undo.clear();
	withQueue((q) => q.cancelAll('La session a été fermée'));
	await idb.tables.initialize(id, { sessionOnly: true });
	if (id) void idb.tables.Session.update(id, 'openedAt', new Date().toISOString());
	if (uiState.currentProtocolId !== previousProtocolId) {
		clearMetadataOptionsCache();
	}
}