All files / lib sessions.js

62.5% Statements 10/16
100% Branches 0/0
50% Functions 2/4
66.66% Lines 10/15

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                        1x       1x   1x   1x 1x     1x   1x 2x 2x 2x                                
import * as idb from '$lib/idb.svelte.js';
 
import { imageIdToFileId } from './images.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) {
	uiState.setCurrentSession(id);
	uiState.clearPreviewURLs();
	undo.clear();
	withQueue((q) => q.cancelAll('La session a été fermée'));
	await idb.tables.initialize(id);
}