static: cleanup messages, improve reconnect

This commit is contained in:
Jens Langhammer 2020-11-26 18:36:26 +01:00
parent 14ab9bbd05
commit f83087d04d
5 changed files with 32 additions and 25 deletions

View File

@ -2,12 +2,12 @@
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer from channels.layers import get_channel_layer
from django.contrib.messages.storage.base import Message from django.contrib.messages.storage.base import Message
from django.contrib.messages.storage.session import SessionStorage from django.contrib.messages.storage.fallback import FallbackStorage
from django.core.cache import cache from django.core.cache import cache
from django.http.request import HttpRequest from django.http.request import HttpRequest
class ChannelsStorage(SessionStorage): class ChannelsStorage(FallbackStorage):
"""Send contrib.messages over websocket""" """Send contrib.messages over websocket"""
def __init__(self, request: HttpRequest) -> None: def __init__(self, request: HttpRequest) -> None:

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ let ID = function (prefix: string) {
interface Message { interface Message {
levelTag: string; levelTag: string;
tags: string[]; tags: string;
message: string; message: string;
} }
@ -22,11 +22,7 @@ export class Messages extends LitElement {
@property() @property()
url: string = ""; url: string = "";
@property()
messages: string[] = [];
messageSocket?: WebSocket; messageSocket?: WebSocket;
retryDelay: number = 200; retryDelay: number = 200;
createRenderRoot() { createRenderRoot() {
@ -35,7 +31,11 @@ export class Messages extends LitElement {
constructor() { constructor() {
super(); super();
try {
this.connect(); this.connect();
} catch (error) {
console.warn(`passbook/messages: failed to connect to ws ${error}`);
}
} }
firstUpdated() { firstUpdated() {
@ -53,40 +53,46 @@ export class Messages extends LitElement {
this.messageSocket.addEventListener("close", (e) => { this.messageSocket.addEventListener("close", (e) => {
console.debug(`passbook/messages: closed ws connection: ${e}`); console.debug(`passbook/messages: closed ws connection: ${e}`);
setTimeout(() => { setTimeout(() => {
console.debug(`passbook/messages: reconnecting ws in ${this.retryDelay}`); console.debug(
`passbook/messages: reconnecting ws in ${this.retryDelay}`
);
this.connect(); this.connect();
}, this.retryDelay); }, this.retryDelay);
this.retryDelay = this.retryDelay * 2; this.retryDelay = this.retryDelay * 2;
}); });
this.messageSocket.addEventListener("message", (e) => { this.messageSocket.addEventListener("message", (e) => {
const container = <HTMLElement>(
this.querySelector(".pf-c-alert-group")!
);
const data = JSON.parse(e.data); const data = JSON.parse(e.data);
const messageElement = this.renderMessage(data); this.renderMessage(data);
container.appendChild(messageElement);
}); });
this.messageSocket.addEventListener("error", (e) => {
console.warn(`passbook/messages: error ${e}`);
this.retryDelay = this.retryDelay * 2;
})
} }
/* Fetch messages which were stored in the session. /* Fetch messages which were stored in the session.
* This mostly gets messages which were created when the user arrives/leaves the site * This mostly gets messages which were created when the user arrives/leaves the site
* and especially the login flow */ * and especially the login flow */
fetchMessages() { fetchMessages() {
console.debug(`passbook/messages: fetching messages over direct api`);
return fetch(this.url) return fetch(this.url)
.then((r) => r.json()) .then((r) => r.json())
.then((r) => (this.messages = r))
.then((r) => { .then((r) => {
const container = <HTMLElement>( r.forEach((m: any) => {
this.querySelector(".pf-c-alert-group")! const message = <Message>{
); levelTag: m.level_tag,
r.forEach((message: Message) => { tags: m.tags,
const messageElement = this.renderMessage(message); message: m.message,
container.appendChild(messageElement); };
this.renderMessage(message);
}); });
}); });
} }
renderMessage(message: Message): ChildNode { renderMessage(message: Message) {
const container = <HTMLElement>(
this.querySelector(".pf-c-alert-group")!
);
const id = ID("pb-message"); const id = ID("pb-message");
const el = document.createElement("template"); const el = document.createElement("template");
el.innerHTML = `<li id=${id} class="pf-c-alert-group__item"> el.innerHTML = `<li id=${id} class="pf-c-alert-group__item">
@ -104,7 +110,7 @@ export class Messages extends LitElement {
setTimeout(() => { setTimeout(() => {
this.querySelector(`#${id}`)?.remove(); this.querySelector(`#${id}`)?.remove();
}, 1500); }, 1500);
return el.content.firstChild!; container.appendChild(el.content.firstChild!);
} }
render() { render() {

View File

@ -54,6 +54,7 @@ export class FlowShellCard extends LitElement {
this.checkAutofocus(); this.checkAutofocus();
this.loadFormCode(); this.loadFormCode();
this.setFormSubmitHandlers(); this.setFormSubmitHandlers();
break;
default: default:
console.debug( console.debug(
`passbook/flows: unexpected data type ${data.type}` `passbook/flows: unexpected data type ${data.type}`