web: optionally allow unique messages

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-11-16 11:21:30 +01:00
parent 638e8d741f
commit 047030f901
2 changed files with 16 additions and 7 deletions

View File

@ -33,10 +33,13 @@ export class WebsocketClient {
this.messageSocket.addEventListener("close", (e) => { this.messageSocket.addEventListener("close", (e) => {
console.debug(`authentik/ws: closed ws connection: ${e}`); console.debug(`authentik/ws: closed ws connection: ${e}`);
if (this.retryDelay > 3000) { if (this.retryDelay > 3000) {
showMessage({ showMessage(
{
level: MessageLevel.error, level: MessageLevel.error,
message: t`Connection error, reconnecting...`, message: t`Connection error, reconnecting...`,
}); },
true,
);
} }
setTimeout(() => { setTimeout(() => {
console.debug(`authentik/ws: reconnecting ws in ${this.retryDelay}ms`); console.debug(`authentik/ws: reconnecting ws in ${this.retryDelay}ms`);

View File

@ -9,12 +9,12 @@ import { EVENT_WS_MESSAGE, WS_MSG_TYPE_MESSAGE } from "../../constants";
import "./Message"; import "./Message";
import { APIMessage } from "./Message"; import { APIMessage } from "./Message";
export function showMessage(message: APIMessage): void { export function showMessage(message: APIMessage, unique = false): void {
const container = document.querySelector<MessageContainer>("ak-message-container"); const container = document.querySelector<MessageContainer>("ak-message-container");
if (!container) { if (!container) {
throw new Error("failed to find message container"); throw new Error("failed to find message container");
} }
container.addMessage(message); container.addMessage(message, unique);
container.requestUpdate(); container.requestUpdate();
} }
@ -44,7 +44,13 @@ export class MessageContainer extends LitElement {
}) as EventListener); }) as EventListener);
} }
addMessage(message: APIMessage): void { addMessage(message: APIMessage, unique = false): void {
if (unique) {
const matchingMessages = this.messages.filter((m) => m.message == message.message);
if (matchingMessages.length > 0) {
return;
}
}
this.messages.push(message); this.messages.push(message);
} }