All files / src/web bpmnEditorSessions.ts

61.29% Statements 38/62
52.08% Branches 25/48
60% Functions 6/10
62.71% Lines 37/59

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                  2x 2x 2x 2x       9x                 4x 4x 4x 4x 4x 4x 4x       1x 1x 1x 1x       1x 1x           1x 1x 1x       1x 1x 2x 1x 1x       1x 1x         2x 2x 1x 1x 1x 1x 1x                         1x 1x                                                
import * as vscode from 'vscode';
import { getActiveBpmnTextDocument, resolveActiveBpmnUri } from './bpmnEditorRouting';
 
interface BpmnEditorSession {
	id: string;
	document: vscode.TextDocument;
	webview: vscode.Webview;
}
 
const sessions = new Map<string, BpmnEditorSession>();
const documentSessionIds = new Map<string, string[]>();
const webviewSessionIds = new WeakMap<vscode.Webview, string>();
let sessionIdCounter = 0;
let lastActiveSessionId: string | undefined;
 
function getDocumentKey(document: vscode.TextDocument): string {
	return document.uri.toString();
}
 
function getFallbackSessionId(): string | undefined {
	const sessionIds = Array.from(sessions.keys());
	return sessionIds[sessionIds.length - 1];
}
 
export function registerBpmnEditorSession(document: vscode.TextDocument, webview: vscode.Webview): void {
	sessionIdCounter += 1;
	const id = `${getDocumentKey(document)}::${sessionIdCounter}`;
	sessions.set(id, { id, document, webview });
	webviewSessionIds.set(webview, id);
	const documentKey = getDocumentKey(document);
	documentSessionIds.set(documentKey, [...(documentSessionIds.get(documentKey) || []), id]);
	lastActiveSessionId = id;
}
 
export function markBpmnEditorSessionActive(document: vscode.TextDocument, webview?: vscode.Webview): void {
	const webviewSessionId = webview ? webviewSessionIds.get(webview) : undefined;
	Eif (webviewSessionId && sessions.has(webviewSessionId)) {
		lastActiveSessionId = webviewSessionId;
		return;
	}
 
	const sessionIds = documentSessionIds.get(getDocumentKey(document)) || [];
	const fallbackSessionId = sessionIds.find((sessionId) => sessions.has(sessionId));
	Iif (fallbackSessionId) {
		lastActiveSessionId = fallbackSessionId;
	}
}
 
export function unregisterBpmnEditorSession(document: vscode.TextDocument, webview: vscode.Webview): void {
	const sessionId = webviewSessionIds.get(webview);
	const session = sessionId ? sessions.get(sessionId) : undefined;
	Iif (!session || session.document.uri.toString() !== document.uri.toString()) {
		return;
	}
 
	sessions.delete(session.id);
	const documentKey = getDocumentKey(document);
	const remainingDocumentSessionIds = (documentSessionIds.get(documentKey) || []).filter((id) => id !== session.id && sessions.has(id));
	if (remainingDocumentSessionIds.length > 0) {
		documentSessionIds.set(documentKey, remainingDocumentSessionIds);
	} else E{
		documentSessionIds.delete(documentKey);
	}
	Eif (lastActiveSessionId === session.id) {
		lastActiveSessionId = remainingDocumentSessionIds[remainingDocumentSessionIds.length - 1] || getFallbackSessionId();
	}
}
 
export function getActiveBpmnEditorSession(): BpmnEditorSession | undefined {
	const activeUri = resolveActiveBpmnUri();
	if (activeUri) {
		const activeSessionIds = documentSessionIds.get(activeUri.toString()) || [];
		Eif (lastActiveSessionId && activeSessionIds.includes(lastActiveSessionId)) {
			const activeSession = sessions.get(lastActiveSessionId);
			Eif (activeSession) {
				return activeSession;
			}
		}
		for (const sessionId of activeSessionIds) {
			const activeSession = sessions.get(sessionId);
			if (activeSession) {
				return activeSession;
			}
		}
 
		return undefined;
	}
 
	Eif (lastActiveSessionId) {
		return sessions.get(lastActiveSessionId);
	}
 
	return undefined;
}
 
export function getActiveBpmnDocument(): vscode.TextDocument | undefined {
	const activeSessionDocument = getActiveBpmnEditorSession()?.document;
	if (activeSessionDocument) {
		return activeSessionDocument;
	}
 
	const activeTextDocument = getActiveBpmnTextDocument();
	if (activeTextDocument) {
		return activeTextDocument;
	}
 
	const activeUri = resolveActiveBpmnUri();
	if (!activeUri) {
		return undefined;
	}
 
	return vscode.workspace.textDocuments.find((document) => document.uri.toString() === activeUri.toString());
}