mirror of
https://github.com/avatao-content/frontend-tutorial-framework
synced 2024-12-05 02:31:33 +00:00
Make message handling logic more decoupled from implementation
This commit is contained in:
parent
224da31981
commit
736f0eea71
@ -10,6 +10,7 @@ import { MessagesControlCommand } from '../message-types/messages-control-comman
|
||||
import { config } from '../config';
|
||||
import { CommandMessage } from '../message-types/command-message';
|
||||
import { MessageQueueMessage } from '../message-types/message-queue-message';
|
||||
import { Subject, Observer } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'app-messages',
|
||||
@ -17,6 +18,7 @@ import { MessageQueueMessage } from '../message-types/message-queue-message';
|
||||
styleUrls: ['./messages.component.scss']
|
||||
})
|
||||
export class MessagesComponent implements OnInit {
|
||||
newMessage: Subject<MessagesMessage> = new Subject<MessagesMessage>();
|
||||
messages: MessagesMessage[] = [];
|
||||
messageQueueAttender: MessageQueueAttender;
|
||||
showNextButton: boolean = config.messages.showNextButton;
|
||||
@ -30,13 +32,15 @@ export class MessagesComponent implements OnInit {
|
||||
private websocketService: WebSocketService,
|
||||
private changeDetectorRef: ChangeDetectorRef
|
||||
) {
|
||||
this.messageQueueAttender = new MessageQueueAttender(this.messages);
|
||||
this.messageQueueAttender = new MessageQueueAttender(this.newMessage);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.newMessage.subscribe(this.handleMessage.bind(this));
|
||||
|
||||
this.websocketService.connect();
|
||||
this.websocketService.observeKey<MessagesMessage>('message').subscribe(
|
||||
(event) => this.handleMessage(event.data)
|
||||
(event) => this.newMessage.next(event.data)
|
||||
);
|
||||
this.websocketService.observeKey<MessageQueueMessage>('queueMessages').subscribe(
|
||||
(event) => this.handleQueueMessage(event.data)
|
||||
@ -75,7 +79,7 @@ class MessageQueueAttender {
|
||||
private lastMessageLength = 0;
|
||||
private queue: MessagesMessage[] = [];
|
||||
|
||||
constructor(private destArray: Array<MessagesMessage>, wordPerMinute: number = 150) {
|
||||
constructor(private messageEmitter: Observer<MessagesMessage>, wordPerMinute: number = 150) {
|
||||
const charPerMinute = wordPerMinute * 5;
|
||||
this.charPerSecond = charPerMinute / 60;
|
||||
}
|
||||
@ -89,7 +93,7 @@ class MessageQueueAttender {
|
||||
if (this.queue.length > 0) {
|
||||
const lastMessage = this.queue.shift();
|
||||
this.lastMessageLength = lastMessage.message.length;
|
||||
this.destArray.push(lastMessage);
|
||||
this.messageEmitter.next(lastMessage);
|
||||
}
|
||||
if (this.queue.length === 0) {
|
||||
this.lastMessageLength = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user