chore: marke change api call async

This commit is contained in:
arc-alex
2025-12-15 14:15:12 +01:00
committed by Carlos Valente
parent ba75d52d29
commit 6beda1575a
3 changed files with 10 additions and 18 deletions
+2 -2
View File
@@ -102,7 +102,7 @@ class SocketServer implements IAdapter {
this.sendClientList(); this.sendClientList();
}); });
ws.on('message', (data) => { ws.on('message', async (data) => {
try { try {
const message = JSON.parse(data.toString()) as WsPacketToServer; const message = JSON.parse(data.toString()) as WsPacketToServer;
const { tag, payload } = message; const { tag, payload } = message;
@@ -142,7 +142,7 @@ class SocketServer implements IAdapter {
tag satisfies never; tag satisfies never;
// Protocol specific stuff handled above // Protocol specific stuff handled above
try { try {
const reply = dispatchFromAdapter(tag, payload, 'ws'); const reply = await dispatchFromAdapter(tag, payload, 'ws');
if (reply) { if (reply) {
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
@@ -20,13 +20,11 @@ import { eventStore } from '../stores/EventStore.js';
import * as assert from '../utils/assert.js'; import * as assert from '../utils/assert.js';
import { parseProperty, isValidChangeProperty } from './integration.utils.js'; import { parseProperty, isValidChangeProperty } from './integration.utils.js';
import { socket } from '../adapters/WebsocketAdapter.js'; import { socket } from '../adapters/WebsocketAdapter.js';
import { throttle } from '../utils/throttle.js';
import { coerceEnum } from '../utils/coerceType.js'; import { coerceEnum } from '../utils/coerceType.js';
import { editEntry } from '../api-data/rundown/rundown.service.js'; import { editEntry } from '../api-data/rundown/rundown.service.js';
import { willCauseRegeneration } from '../api-data/rundown/rundown.utils.js'; import { willCauseRegeneration } from '../api-data/rundown/rundown.utils.js';
import { getCurrentRundown } from '../api-data/rundown/rundown.dao.js'; import { getCurrentRundown } from '../api-data/rundown/rundown.dao.js';
const throttledEditEvent = throttle(editEntry, 20);
let lastRequest: Date | null = null; let lastRequest: Date | null = null;
export function dispatchFromAdapter(tag: string, payload: unknown, _source?: 'osc' | 'ws' | 'http') { export function dispatchFromAdapter(tag: string, payload: unknown, _source?: 'osc' | 'ws' | 'http') {
@@ -45,7 +43,9 @@ export function getLastRequest() {
return lastRequest; return lastRequest;
} }
type ActionHandler = (payload: unknown) => { payload: unknown }; type ActionHandler =
| ((payload: unknown) => { payload: unknown })
| ((payload: unknown) => Promise<{ payload: unknown }>);
const actionHandlers: Record<ApiActionTag, ActionHandler> = { const actionHandlers: Record<ApiActionTag, ActionHandler> = {
/* General */ /* General */
@@ -53,7 +53,7 @@ const actionHandlers: Record<ApiActionTag, ActionHandler> = {
poll: () => ({ poll: () => ({
payload: eventStore.poll(), payload: eventStore.poll(),
}), }),
change: (payload) => { change: async (payload) => {
assert.isObject(payload); assert.isObject(payload);
if (Object.keys(payload).length === 0) { if (Object.keys(payload).length === 0) {
throw new Error('Payload is empty'); throw new Error('Payload is empty');
@@ -89,16 +89,8 @@ const actionHandlers: Record<ApiActionTag, ActionHandler> = {
Object.assign(patchEntry, newObjectProperty); Object.assign(patchEntry, newObjectProperty);
} }
}); });
//TODO: windowed edit function
if (shouldThrottle) { await editEntry(patchEntry);
if (throttledEditEvent(patchEntry)) {
return { payload: 'throttled' };
}
} else {
editEntry(patchEntry).catch((_error) => {
/** No error handling */
});
}
return { payload: 'success' }; return { payload: 'success' };
}, },
/* Message Service */ /* Message Service */
@@ -27,7 +27,7 @@ integrationRouter.get('/', (_req: Request, res: Response<{ message: string }>) =
/** /**
* All calls are sent to the dispatcher * All calls are sent to the dispatcher
*/ */
integrationRouter.get('/*splat', (req: Request, res: Response<ErrorResponse | { payload: unknown }>) => { integrationRouter.get('/*splat', async (req: Request, res: Response<ErrorResponse | { payload: unknown }>) => {
let action = req.path.substring(1); let action = req.path.substring(1);
if (!action) { if (!action) {
res.status(400).json({ message: 'No action found' }); res.status(400).json({ message: 'No action found' });
@@ -45,7 +45,7 @@ integrationRouter.get('/*splat', (req: Request, res: Response<ErrorResponse | {
} else { } else {
payload = query; payload = query;
} }
const reply = dispatchFromAdapter(action, payload, 'http'); const reply = await dispatchFromAdapter(action, payload, 'http');
res.status(202).json(reply); res.status(202).json(reply);
} catch (error) { } catch (error) {
const errorMessage = getErrorMessage(error); const errorMessage = getErrorMessage(error);