mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-15 04:13:47 +00:00
refactor: use strict typing
This commit is contained in:
committed by
Carlos Valente
parent
4ed38340e0
commit
178640bfc4
@@ -102,7 +102,7 @@ class SocketServer implements IAdapter {
|
||||
|
||||
ws.on('message', (data) => {
|
||||
try {
|
||||
// @ts-expect-error -- ??
|
||||
// @ts-expect-error -- this works fine
|
||||
const message = JSON.parse(data);
|
||||
const { type, payload } = message;
|
||||
|
||||
@@ -120,7 +120,7 @@ class SocketServer implements IAdapter {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'client-name',
|
||||
payload: this.clients.get(clientId).name,
|
||||
payload: this.getOrCreateClient(clientId),
|
||||
}),
|
||||
);
|
||||
return;
|
||||
@@ -136,7 +136,7 @@ class SocketServer implements IAdapter {
|
||||
|
||||
if (type === 'set-client-type') {
|
||||
if (payload && typeof payload == 'string') {
|
||||
const previousData = this.clients.get(clientId);
|
||||
const previousData = this.getOrCreateClient(clientId);
|
||||
this.clients.set(clientId, { ...previousData, type: payload });
|
||||
}
|
||||
this.sendClientList();
|
||||
@@ -145,7 +145,7 @@ class SocketServer implements IAdapter {
|
||||
|
||||
if (type === 'set-client-path') {
|
||||
if (payload && typeof payload == 'string') {
|
||||
const previousData = this.clients.get(clientId);
|
||||
const previousData = this.getOrCreateClient(clientId);
|
||||
previousData.path = payload;
|
||||
this.clients.set(clientId, previousData);
|
||||
|
||||
@@ -166,13 +166,13 @@ class SocketServer implements IAdapter {
|
||||
|
||||
if (type === 'set-client-name') {
|
||||
if (payload) {
|
||||
const previousData = this.clients.get(clientId);
|
||||
const previousData = this.getOrCreateClient(clientId);
|
||||
logger.info(LogOrigin.Client, `Client ${previousData.name} renamed to ${payload}`);
|
||||
this.clients.set(clientId, { ...previousData, name: payload });
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'client-name',
|
||||
payload: this.clients.get(clientId).name,
|
||||
payload: this.getOrCreateClient(clientId).name,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -215,6 +215,19 @@ class SocketServer implements IAdapter {
|
||||
};
|
||||
}
|
||||
|
||||
private getOrCreateClient(clientId: string): Client {
|
||||
if (!this.clients.has(clientId)) {
|
||||
this.clients.set(clientId, {
|
||||
type: 'unknown',
|
||||
identify: false,
|
||||
name: getRandomName(),
|
||||
origin: '',
|
||||
path: '',
|
||||
});
|
||||
}
|
||||
return this.clients.get(clientId) as Client;
|
||||
}
|
||||
|
||||
private sendClientList(): void {
|
||||
const payload = Object.fromEntries(this.clients.entries());
|
||||
this.sendAsJson({ type: 'client-list', payload });
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* @param {string} value - value to assign
|
||||
* @returns {object | string | null} nested object or null if no object was created
|
||||
*/
|
||||
export const integrationPayloadFromPath = (path: string[], value?: unknown): object | string | null => {
|
||||
export function integrationPayloadFromPath(path: string[], value?: unknown): object | string | null {
|
||||
if (path.length === 1) {
|
||||
const key = path[0];
|
||||
return value === undefined ? key : { [key]: value };
|
||||
@@ -16,4 +16,4 @@ export const integrationPayloadFromPath = (path: string[], value?: unknown): obj
|
||||
const obj = shortenedPath.reduceRight((result, key) => ({ [key]: result }), parsedValue);
|
||||
|
||||
return typeof obj === 'object' ? obj : null;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user