2021-06-30 10:15:55 +02:00
|
|
|
import type { ChatEvent } from "../Events/ChatEvent";
|
|
|
|
import { isUserInputChatEvent, UserInputChatEvent } from "../Events/UserInputChatEvent";
|
|
|
|
import { IframeApiContribution, sendToWorkadventure } from "./IframeApiContribution";
|
2021-06-21 11:48:39 +02:00
|
|
|
import { apiCallback } from "./registeredCallbacks";
|
2021-06-30 10:15:55 +02:00
|
|
|
import { Subject } from "rxjs";
|
2021-06-21 13:47:07 +02:00
|
|
|
|
|
|
|
const chatStream = new Subject<string>();
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-06-30 10:15:55 +02:00
|
|
|
export class WorkadventureChatCommands extends IframeApiContribution<WorkadventureChatCommands> {
|
|
|
|
callbacks = [
|
|
|
|
apiCallback({
|
|
|
|
callback: (event: UserInputChatEvent) => {
|
|
|
|
chatStream.next(event.message);
|
|
|
|
},
|
|
|
|
type: "userInputChat",
|
|
|
|
typeChecker: isUserInputChatEvent,
|
|
|
|
}),
|
|
|
|
];
|
2021-05-25 13:47:41 +02:00
|
|
|
|
|
|
|
sendChatMessage(message: string, author: string) {
|
|
|
|
sendToWorkadventure({
|
2021-06-30 10:15:55 +02:00
|
|
|
type: "chat",
|
2021-05-25 13:47:41 +02:00
|
|
|
data: {
|
2021-06-30 10:15:55 +02:00
|
|
|
message: message,
|
|
|
|
author: author,
|
|
|
|
},
|
|
|
|
});
|
2021-05-25 13:47:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to messages sent by the local user, in the chat.
|
|
|
|
*/
|
|
|
|
onChatMessage(callback: (message: string) => void) {
|
2021-06-21 13:47:07 +02:00
|
|
|
chatStream.subscribe(callback);
|
2021-05-25 13:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:15:55 +02:00
|
|
|
export default new WorkadventureChatCommands();
|