2023-03-21 07:34:30 -07:00
|
|
|
import { jsonStringify, LoggerProxy as Logger } from 'n8n-workflow';
|
2023-02-10 06:02:47 -08:00
|
|
|
import type { IPushDataType } from '@/Interfaces';
|
|
|
|
|
|
|
|
export abstract class AbstractPush<T> {
|
|
|
|
protected connections: Record<string, T> = {};
|
|
|
|
|
|
|
|
protected abstract close(connection: T): void;
|
|
|
|
protected abstract sendToOne(connection: T, data: string): void;
|
|
|
|
|
|
|
|
protected add(sessionId: string, connection: T): void {
|
|
|
|
const { connections } = this;
|
|
|
|
Logger.debug('Add editor-UI session', { sessionId });
|
|
|
|
|
|
|
|
const existingConnection = connections[sessionId];
|
|
|
|
if (existingConnection) {
|
|
|
|
// Make sure to remove existing connection with the same id
|
|
|
|
this.close(existingConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
connections[sessionId] = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected remove(sessionId?: string): void {
|
|
|
|
if (sessionId !== undefined) {
|
|
|
|
Logger.debug('Remove editor-UI session', { sessionId });
|
|
|
|
delete this.connections[sessionId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
send<D>(type: IPushDataType, data: D, sessionId: string | undefined) {
|
2023-02-10 06:02:47 -08:00
|
|
|
const { connections } = this;
|
|
|
|
if (sessionId !== undefined && connections[sessionId] === undefined) {
|
|
|
|
Logger.error(`The session "${sessionId}" is not registered.`, { sessionId });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(`Send data of type "${type}" to editor-UI`, { dataType: type, sessionId });
|
|
|
|
|
2023-03-21 07:34:30 -07:00
|
|
|
const sendData = jsonStringify({ type, data }, { replaceCircularRefs: true });
|
2023-02-10 06:02:47 -08:00
|
|
|
|
|
|
|
if (sessionId === undefined) {
|
|
|
|
// Send to all connected clients
|
|
|
|
Object.values(connections).forEach((connection) => this.sendToOne(connection, sendData));
|
|
|
|
} else {
|
|
|
|
// Send only to a specific client
|
|
|
|
this.sendToOne(connections[sessionId], sendData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|