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();
});
ws.on('message', (data) => {
ws.on('message', async (data) => {
try {
const message = JSON.parse(data.toString()) as WsPacketToServer;
const { tag, payload } = message;
@@ -142,7 +142,7 @@ class SocketServer implements IAdapter {
tag satisfies never;
// Protocol specific stuff handled above
try {
const reply = dispatchFromAdapter(tag, payload, 'ws');
const reply = await dispatchFromAdapter(tag, payload, 'ws');
if (reply) {
ws.send(
JSON.stringify({
@@ -20,13 +20,11 @@ import { eventStore } from '../stores/EventStore.js';
import * as assert from '../utils/assert.js';
import { parseProperty, isValidChangeProperty } from './integration.utils.js';
import { socket } from '../adapters/WebsocketAdapter.js';
import { throttle } from '../utils/throttle.js';
import { coerceEnum } from '../utils/coerceType.js';
import { editEntry } from '../api-data/rundown/rundown.service.js';
import { willCauseRegeneration } from '../api-data/rundown/rundown.utils.js';
import { getCurrentRundown } from '../api-data/rundown/rundown.dao.js';
const throttledEditEvent = throttle(editEntry, 20);
let lastRequest: Date | null = null;
export function dispatchFromAdapter(tag: string, payload: unknown, _source?: 'osc' | 'ws' | 'http') {
@@ -45,7 +43,9 @@ export function getLastRequest() {
return lastRequest;
}
type ActionHandler = (payload: unknown) => { payload: unknown };
type ActionHandler =
| ((payload: unknown) => { payload: unknown })
| ((payload: unknown) => Promise<{ payload: unknown }>);
const actionHandlers: Record<ApiActionTag, ActionHandler> = {
/* General */
@@ -53,7 +53,7 @@ const actionHandlers: Record<ApiActionTag, ActionHandler> = {
poll: () => ({
payload: eventStore.poll(),
}),
change: (payload) => {
change: async (payload) => {
assert.isObject(payload);
if (Object.keys(payload).length === 0) {
throw new Error('Payload is empty');
@@ -89,16 +89,8 @@ const actionHandlers: Record<ApiActionTag, ActionHandler> = {
Object.assign(patchEntry, newObjectProperty);
}
});
if (shouldThrottle) {
if (throttledEditEvent(patchEntry)) {
return { payload: 'throttled' };
}
} else {
editEntry(patchEntry).catch((_error) => {
/** No error handling */
});
}
//TODO: windowed edit function
await editEntry(patchEntry);
return { payload: 'success' };
},
/* Message Service */
@@ -27,7 +27,7 @@ integrationRouter.get('/', (_req: Request, res: Response<{ message: string }>) =
/**
* 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);
if (!action) {
res.status(400).json({ message: 'No action found' });
@@ -45,7 +45,7 @@ integrationRouter.get('/*splat', (req: Request, res: Response<ErrorResponse | {
} else {
payload = query;
}
const reply = dispatchFromAdapter(action, payload, 'http');
const reply = await dispatchFromAdapter(action, payload, 'http');
res.status(202).json(reply);
} catch (error) {
const errorMessage = getErrorMessage(error);