Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

✨ ntfy widget close on done, and catch err

Alicia Sykesd3a07eb6284c
diff --git a/docs/widgets.md b/docs/widgets.md
index f9df1a63..65cabbd1 100644
--- a/docs/widgets.md
+++ b/docs/widgets.md
@@ -2473,7 +2473,7 @@ Subscribes to topics on a **private** [ntfy](https://docs.ntfy.sh/) server, and
- **CORS**: Disabled
- **Auth**: Optional
- **Price**: Free
-- **Host**: [Self-hosted](https://https://docs.ntfy.sh/install/)
+- **Host**: [Self-hosted](https://docs.ntfy.sh/install/)
- **Privacy**: [ntfy Privacy Policy](https://docs.ntfy.sh/privacy/)
---
diff --git a/src/components/Widgets/NtfyStream.vue b/src/components/Widgets/NtfyStream.vue
index 6ffe8e34..43b9b0d1 100644
--- a/src/components/Widgets/NtfyStream.vue
+++ b/src/components/Widgets/NtfyStream.vue
@@ -25,6 +25,12 @@ export default {
messages: [],
};
},
+ beforeUnmount() {
+ if (this.eventSource) {
+ this.eventSource.close();
+ this.eventSource = null;
+ }
+ },
computed: {
serverUrl() {
if (!this.options.server_url) this.error('The server URL is required.');
@@ -40,19 +46,33 @@ export default {
title() {
return this.parseAsEnvVar(this.options.title) || '';
},
-
+ limit() {
+ return parseInt(this.options.limit, 10) || 50;
+ },
},
methods: {
fetchData() {
// @see https://docs.ntfy.sh/subscribe/api/#subscribe-as-sse-stream
this.finishLoading();
+ if (this.eventSource) {
+ this.eventSource.close();
+ }
let url = `${this.serverUrl}/${this.topic}/sse`;
if (this.auth !== '') {
- url = `${url}?auth=${this.auth}`;
+ url = `${url}?auth=${encodeURIComponent(this.auth)}`;
}
- const eventSource = new EventSource(url);
- eventSource.onmessage = (e) => {
+ this.eventSource = new EventSource(url);
+ this.eventSource.onmessage = (e) => {
this.messages.unshift(JSON.parse(e.data));
+ if (this.messages.length > this.limit) {
+ this.messages = this.messages.slice(0, this.limit);
+ }
+ };
+ this.eventSource.onerror = (e) => {
+ const msg = this.eventSource.readyState === EventSource.CLOSED ?
+ 'ntfy connection closed by the server (check URL, topic and auth).'
+ : 'Unable to reach the ntfy server, reconnecting...';
+ this.error(msg, e);
};
},
},