HL7-1: add comments to code

This commit is contained in:
Markus Thielker 2025-07-30 13:06:17 +02:00
parent ad48df97e5
commit 3898715915
4 changed files with 37 additions and 8 deletions

View file

@ -19,13 +19,13 @@
import { dev } from '$app/environment';
// connection state
let ws = $state<WebSocket | undefined>(undefined);
let ws = $state<WebSocket | undefined>(undefined); // websocket client
let stationId = $state<string | undefined>(undefined); // stationId assigned by server
let connectionState = $state<ConnectionState>(ConnectionState.disconnected);
// client state
let composedMessage = $state('');
let sentMessages = $state<ReceiveHl7v2Message[]>([]); // storing sent messages with client-timestamp for now
let composedMessage = $state(''); // content of text-box
let sentMessages = $state<ReceiveHl7v2Message[]>([]); // sent messages stored as type ReceiveHl7v2Message because of the timestamp
let receivedMessages = $state<ReceiveHl7v2Message[]>([]);
let isSending = $state(false);
let copySuccess = $state(false);
@ -60,6 +60,7 @@
};
const segmentTypes = Object.keys(segmentTemplates) as Array<keyof typeof segmentTemplates>;
// handles the connection process and message handling
function connectToServer() {
console.log('Connecting to server...');
@ -67,16 +68,20 @@
const socket = new WebSocket(`${dev ? 'ws' : 'wss'}://${env.PUBLIC_SERVER}`);
// store websocket on successful connection
socket.onopen = () => {
console.log('WebSocket connection established.');
ws = socket;
};
// register message handlers
socket.onmessage = (event) => {
// parse every message as type Message
const message = JSON.parse(event.data) as Message;
console.log('Message received from server:', message);
// react based on message type
switch (message.type) {
// initial message from server assigning ID
@ -99,12 +104,14 @@
}
};
// reset connection state and websocket client on disconnect
socket.onclose = () => {
console.log('WebSocket connection closed.');
connectionState = ConnectionState.disconnected;
ws = undefined;
};
// reset connection state and websocket client on error
socket.onerror = (error) => {
console.error('WebSocket error:', error);
connectionState = ConnectionState.disconnected;
@ -112,7 +119,7 @@
};
}
// clean up on close
// reset connection state and websocket client on tab closed
$effect(() => {
return () => {
if (ws && ws.readyState === WebSocket.OPEN) {
@ -122,21 +129,26 @@
};
});
// adds the template of the provided segment type at the bottom of the textbox
function addSegment(type: keyof typeof segmentTemplates) {
const template = segmentTemplates[type].template();
composedMessage += `\r\n${template}`;
}
// handles sending a message to the websocket server
function handleSendMessage(message: string) {
// check for active connection
if (!ws || ws.readyState !== WebSocket.OPEN || isSending || !stationId) {
console.log('Socket not ready');
return;
}
// set UI state
isSending = true;
deliveryError = '';
// construct Message object and send it
const messageToSend = {
type: 'send_hl7v2',
payload: { message },
@ -148,9 +160,12 @@
payload: { message, timestamp: new Date().toISOString() },
} as ReceiveHl7v2Message, ...sentMessages];
composedMessage = segmentTemplates.MSH.template();
// reset UI state
isSending = false;
}
// copies the stationId to the clipboard and handles UI state
function copyStationId() {
if (stationId) {
navigator.clipboard.writeText(stationId).then(() => {
@ -163,6 +178,7 @@
</script>
<!-- 'component' for displaying a message in the message lists (sent and received) -->
{#snippet message(msg: ReceiveHl7v2Message)}
<div class="bg-foreground/10 p-3 rounded-md space-y-1">
<p class="text-xs">{new Date(msg.payload.timestamp).toLocaleString()}</p>
@ -296,4 +312,4 @@
</div>
</main>
</div>
</div>
</div>