From 2c4e10428ec1e3031dbd62c7b7aedf3a791baa48 Mon Sep 17 00:00:00 2001 From: "R. Richard" Date: Mon, 26 Aug 2019 10:40:54 +0200 Subject: [PATCH] Move message queue logic to backend --- src/app/message-types/bot-messages.ts | 5 +- src/app/message-types/deploy-message.ts | 5 ++ src/app/messages/messages.component.html | 2 +- src/app/messages/messages.component.ts | 73 +++--------------------- 4 files changed, 14 insertions(+), 71 deletions(-) create mode 100644 src/app/message-types/deploy-message.ts diff --git a/src/app/message-types/bot-messages.ts b/src/app/message-types/bot-messages.ts index 33f15af..e8177ba 100644 --- a/src/app/message-types/bot-messages.ts +++ b/src/app/message-types/bot-messages.ts @@ -7,6 +7,7 @@ export interface MessageConfig extends WebSocketMessage { export interface MessageMetadata { originator?: string; timestamp?: Date; + typing?: boolean; } export interface MessageData extends MessageMetadata { @@ -14,7 +15,3 @@ export interface MessageData extends MessageMetadata { } export interface Message extends MessageData, WebSocketMessage {} - -export interface MessageQueue extends WebSocketMessage { - value: Array; -} diff --git a/src/app/message-types/deploy-message.ts b/src/app/message-types/deploy-message.ts new file mode 100644 index 0000000..a2b85c0 --- /dev/null +++ b/src/app/message-types/deploy-message.ts @@ -0,0 +1,5 @@ +import { WebSocketMessage } from './websocket-message'; + +export interface DeployMessage extends WebSocketMessage { + status: string; +} diff --git a/src/app/messages/messages.component.html b/src/app/messages/messages.component.html index 6cfe1bc..14096c8 100644 --- a/src/app/messages/messages.component.html +++ b/src/app/messages/messages.component.html @@ -9,7 +9,7 @@
-
+
diff --git a/src/app/messages/messages.component.ts b/src/app/messages/messages.component.ts index e6f8f9f..0eea2dc 100644 --- a/src/app/messages/messages.component.ts +++ b/src/app/messages/messages.component.ts @@ -1,5 +1,5 @@ import { ChangeDetectorRef, Component, OnInit, EventEmitter, Output } from '@angular/core'; -import { MessageConfig, MessageData, Message, MessageQueue } from '../message-types/bot-messages'; +import { MessageConfig, MessageData, Message } from '../message-types/bot-messages'; import { MarkdownService } from '../services/markdown.service'; import { WebSocketService } from '../services/websocket.service'; @@ -14,42 +14,31 @@ import { Subject, Observer, BehaviorSubject } from 'rxjs'; export class MessagesComponent implements OnInit { @Output() newMessageEvent: EventEmitter = new EventEmitter(); newMessage: Subject = new Subject(); - messageInQueue = true; + showTypingIndicator = false; originator = 'avataobot'; messages: MessageData[] = []; - messageQueueAttender: MessageQueueAttender; - - command_handlers = { - 'message.queue': this.handleQueueMessage.bind(this), - 'message.config': this.configureMessages.bind(this), - 'message.send': () => {} - }; constructor( private markdownService: MarkdownService, private websocketService: WebSocketService, private changeDetectorRef: ChangeDetectorRef - ) { - this.messageQueueAttender = new MessageQueueAttender(this.newMessage); - } + ) {} ngOnInit() { this.newMessage.subscribe( message => { + this.showTypingIndicator = true; this.writeMessage(message); this.newMessageEvent.emit(); }); - this.messageQueueAttender.messageInQueue.subscribe( - (value) => this.messageInQueue = value - ); this.websocketService.connect(); this.websocketService.observeKey('message.send').subscribe( message => this.newMessage.next(message) ); - this.websocketService.observeKey('message').subscribe( - message => this.command_handlers[message.key](message) + this.websocketService.observeKey('message.config').subscribe( + message => this.configureMessages(message) ); } @@ -67,10 +56,7 @@ export class MessagesComponent implements OnInit { if (!message.timestamp) { message.timestamp = new Date(); } - } - - handleQueueMessage(message: MessageQueue) { - this.messageQueueAttender.queueMessages(message.value); + this.showTypingIndicator = message.typing; } configureMessages(message: MessageConfig) { @@ -83,48 +69,3 @@ export class MessagesComponent implements OnInit { return this.markdownService.convertToHtml(text); } } - - -class MessageQueueAttender { - public messageInQueue = new BehaviorSubject(false); - - private readonly charPerSecond: number; - private lastMessageLength = 0; - private queue: MessageData[] = []; - - constructor(private messageEmitter: Observer, wordPerMinute: number = config.messages.messageQueueWPM) { - const charPerMinute = wordPerMinute * 5; - this.charPerSecond = charPerMinute / 60; - } - - queueMessages(messages: Array) { - this.queue = this.queue.concat(messages); - this.attendQueue(); - } - - private processMessage() { - if (this.queue.length > 0) { - const lastMessage = this.queue.shift(); - this.lastMessageLength = lastMessage.message.length; - this.messageEmitter.next(lastMessage); - } - if (this.queue.length === 0) { - this.lastMessageLength = 0; - this.messageInQueue.next(false); - } - } - - private attendQueue() { - if (this.queue.length > 0) { - this.messageInQueue.next(true); - const timeoutSeconds = this.lastMessageLength / this.charPerSecond; - setTimeout( - () => { - this.processMessage(); - this.attendQueue(); - }, - timeoutSeconds * 1000 - ); - } - } -}