Files
Automata/packages/cli/src/push/sse.push.ts
कारतोफ्फेलस्क्रिप्ट™ 538984dc2f feat(core): Add support for WebSockets as an alternative to Server-Sent Events (#5443)
Co-authored-by: Matthijs Knigge <matthijs@volcano.nl>
2023-02-10 15:02:47 +01:00

33 lines
852 B
TypeScript

import SSEChannel from 'sse-channel';
import { AbstractPush } from './abstract.push';
import type { PushRequest, PushResponse } from './types';
type Connection = { req: PushRequest; res: PushResponse };
export class SSEPush extends AbstractPush<Connection> {
readonly channel = new SSEChannel();
readonly connections: Record<string, Connection> = {};
constructor() {
super();
this.channel.on('disconnect', (channel, { req }) => {
this.remove(req?.query?.sessionId);
});
}
add(sessionId: string, connection: Connection) {
super.add(sessionId, connection);
this.channel.addClient(connection.req, connection.res);
}
protected close({ res }: Connection): void {
res.end();
this.channel.removeClient(res);
}
protected sendToOne(connection: Connection, data: string): void {
this.channel.send(data, [connection.res]);
}
}