WIP: Rearrange code
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {BehaviorSubject} from 'rxjs';
|
||||
import {LogNotification} from './model/log-notification.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class EventService {
|
||||
private latestEventSubject = new BehaviorSubject<LogNotification>({message: 'No recent notifications...'});
|
||||
latestEvent$ = this.latestEventSubject.asObservable();
|
||||
|
||||
private eventHighlightSubject = new BehaviorSubject<boolean>(false);
|
||||
eventHighlight$ = this.eventHighlightSubject.asObservable();
|
||||
|
||||
private eventTimeout: any;
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
handleIncomingLog(logNotification: LogNotification): void {
|
||||
this.latestEventSubject.next(logNotification);
|
||||
this.eventHighlightSubject.next(true);
|
||||
|
||||
if (this.eventTimeout) {
|
||||
clearTimeout(this.eventTimeout);
|
||||
}
|
||||
|
||||
this.eventTimeout = setTimeout(() => {
|
||||
this.eventHighlightSubject.next(false);
|
||||
}, 5000);
|
||||
|
||||
setTimeout(() => {
|
||||
if (!this.eventHighlightSubject.value) {
|
||||
this.latestEventSubject.next({message: 'No recent notifications...'});
|
||||
}
|
||||
}, 12000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Book } from '../../../book/model/book.model';
|
||||
|
||||
export interface BookNotification {
|
||||
action: Action;
|
||||
addedBook?: Book;
|
||||
removedBookIds?: Set<number>;
|
||||
}
|
||||
|
||||
export enum Action {
|
||||
BOOK_ADDED = 'BOOK_ADDED',
|
||||
BOOKS_REMOVED = 'BOOKS_REMOVED',
|
||||
}
|
||||
|
||||
export function parseBookNotification(messageBody: string): BookNotification {
|
||||
const raw = JSON.parse(messageBody);
|
||||
return {
|
||||
action: raw.action,
|
||||
addedBook: raw.addedBook,
|
||||
removedBookIds: raw.removedBookIds ? new Set<number>(raw.removedBookIds) : undefined,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
export interface LogNotification {
|
||||
timestamp?: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function parseLogNotification(messageBody: string): LogNotification {
|
||||
const raw = JSON.parse(messageBody);
|
||||
const localTime = new Date(raw.timestamp).toLocaleTimeString();
|
||||
|
||||
return {
|
||||
timestamp: localTime,
|
||||
message: raw.message,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RxStompService } from './rx-stomp.service';
|
||||
import { rxStompConfig } from './rx-stomp.config';
|
||||
|
||||
export function rxStompServiceFactory() {
|
||||
const rxStomp = new RxStompService();
|
||||
rxStomp.configure(rxStompConfig);
|
||||
rxStomp.activate();
|
||||
return rxStomp;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { RxStompConfig } from '@stomp/rx-stomp';
|
||||
|
||||
export const rxStompConfig: RxStompConfig = {
|
||||
// Which server?
|
||||
brokerURL: 'ws://localhost:8080/ws',
|
||||
|
||||
// Headers
|
||||
// Typical keys: login, passcode, host
|
||||
connectHeaders: {
|
||||
login: 'guest',
|
||||
passcode: 'guest',
|
||||
},
|
||||
|
||||
// How often to heartbeat?
|
||||
// Interval in milliseconds, set to 0 to disable
|
||||
heartbeatIncoming: 0, // Typical value 0 - disabled
|
||||
heartbeatOutgoing: 20000, // Typical value 20000 - every 20 seconds
|
||||
|
||||
// Wait in milliseconds before attempting auto reconnect
|
||||
// Set to 0 to disable
|
||||
// Typical value 500 (500 milli seconds)
|
||||
reconnectDelay: 200,
|
||||
|
||||
// Will log diagnostics on console
|
||||
// It can be quite verbose, not recommended in production
|
||||
// Skip this key to stop logging to console
|
||||
debug: (msg: string): void => {
|
||||
//console.log(new Date(), msg);
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RxStomp } from '@stomp/rx-stomp';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class RxStompService extends RxStomp {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user