workadventure/front/src/Connexion/AdminMessagesService.ts
David Négrier bf070c33b5 Migrating front protobuf encode/decode to ts-proto lib
The ts-proto lib has the huge advantage of producing code the "Typescript" way and not the "Java" way.
In particular, for "oneof" types in protobuf, it is generating "ADT" that can be used to check if we forgot or not to deal with a type.
2022-01-03 15:52:46 +01:00

36 lines
1.1 KiB
TypeScript

import { Subject } from "rxjs";
import type { BanUserMessage, SendUserMessage } from "../Messages/ts-proto-generated/messages";
export enum AdminMessageEventTypes {
admin = "message",
audio = "audio",
ban = "ban",
banned = "banned",
}
interface AdminMessageEvent {
type: AdminMessageEventTypes;
text: string;
//todo add optional properties for other event types
}
//this class is designed to easily allow communication between the RoomConnection objects (that receive the message)
//and the various objects that may render the message on screen
class AdminMessagesService {
private _messageStream: Subject<AdminMessageEvent> = new Subject();
public messageStream = this._messageStream.asObservable();
constructor() {
this.messageStream.subscribe((event) => console.log("message", event));
}
onSendusermessage(message: SendUserMessage | BanUserMessage) {
this._messageStream.next({
type: message.type as unknown as AdminMessageEventTypes,
text: message.message,
});
}
}
export const adminMessagesService = new AdminMessagesService();