mirror of
https://github.com/avatao-content/frontend-tutorial-framework
synced 2025-04-03 11:52:40 +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 { config } from '../config';
|
||||||
import { CommandMessage } from '../message-types/command-message';
|
import { CommandMessage } from '../message-types/command-message';
|
||||||
import { MessageQueueMessage } from '../message-types/message-queue-message';
|
import { MessageQueueMessage } from '../message-types/message-queue-message';
|
||||||
|
import { Subject, Observer } from 'rxjs';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-messages',
|
selector: 'app-messages',
|
||||||
@ -17,6 +18,7 @@ import { MessageQueueMessage } from '../message-types/message-queue-message';
|
|||||||
styleUrls: ['./messages.component.scss']
|
styleUrls: ['./messages.component.scss']
|
||||||
})
|
})
|
||||||
export class MessagesComponent implements OnInit {
|
export class MessagesComponent implements OnInit {
|
||||||
|
newMessage: Subject<MessagesMessage> = new Subject<MessagesMessage>();
|
||||||
messages: MessagesMessage[] = [];
|
messages: MessagesMessage[] = [];
|
||||||
messageQueueAttender: MessageQueueAttender;
|
messageQueueAttender: MessageQueueAttender;
|
||||||
showNextButton: boolean = config.messages.showNextButton;
|
showNextButton: boolean = config.messages.showNextButton;
|
||||||
@ -30,13 +32,15 @@ export class MessagesComponent implements OnInit {
|
|||||||
private websocketService: WebSocketService,
|
private websocketService: WebSocketService,
|
||||||
private changeDetectorRef: ChangeDetectorRef
|
private changeDetectorRef: ChangeDetectorRef
|
||||||
) {
|
) {
|
||||||
this.messageQueueAttender = new MessageQueueAttender(this.messages);
|
this.messageQueueAttender = new MessageQueueAttender(this.newMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
this.newMessage.subscribe(this.handleMessage.bind(this));
|
||||||
|
|
||||||
this.websocketService.connect();
|
this.websocketService.connect();
|
||||||
this.websocketService.observeKey<MessagesMessage>('message').subscribe(
|
this.websocketService.observeKey<MessagesMessage>('message').subscribe(
|
||||||
(event) => this.handleMessage(event.data)
|
(event) => this.newMessage.next(event.data)
|
||||||
);
|
);
|
||||||
this.websocketService.observeKey<MessageQueueMessage>('queueMessages').subscribe(
|
this.websocketService.observeKey<MessageQueueMessage>('queueMessages').subscribe(
|
||||||
(event) => this.handleQueueMessage(event.data)
|
(event) => this.handleQueueMessage(event.data)
|
||||||
@ -75,7 +79,7 @@ class MessageQueueAttender {
|
|||||||
private lastMessageLength = 0;
|
private lastMessageLength = 0;
|
||||||
private queue: MessagesMessage[] = [];
|
private queue: MessagesMessage[] = [];
|
||||||
|
|
||||||
constructor(private destArray: Array<MessagesMessage>, wordPerMinute: number = 150) {
|
constructor(private messageEmitter: Observer<MessagesMessage>, wordPerMinute: number = 150) {
|
||||||
const charPerMinute = wordPerMinute * 5;
|
const charPerMinute = wordPerMinute * 5;
|
||||||
this.charPerSecond = charPerMinute / 60;
|
this.charPerSecond = charPerMinute / 60;
|
||||||
}
|
}
|
||||||
@ -89,7 +93,7 @@ class MessageQueueAttender {
|
|||||||
if (this.queue.length > 0) {
|
if (this.queue.length > 0) {
|
||||||
const lastMessage = this.queue.shift();
|
const lastMessage = this.queue.shift();
|
||||||
this.lastMessageLength = lastMessage.message.length;
|
this.lastMessageLength = lastMessage.message.length;
|
||||||
this.destArray.push(lastMessage);
|
this.messageEmitter.next(lastMessage);
|
||||||
}
|
}
|
||||||
if (this.queue.length === 0) {
|
if (this.queue.length === 0) {
|
||||||
this.lastMessageLength = 0;
|
this.lastMessageLength = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user