Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: sse connection element

Christian Fei6df41ee5c8c9
diff --git a/client/components/sse-connection.js b/client/components/sse-connection.js
new file mode 100644
index 0000000..a6a67ad
--- /dev/null
+++ b/client/components/sse-connection.js
@@ -0,0 +1,21 @@
+/* global HTMLElement, customElements */
+class SSEConnection extends HTMLElement {
+ connectedCallback () {
+ this.render(window.eventSource && window.eventSource.readyState === 'OPEN')
+ }
+
+ static get observedAttributes () {
+ return ['connected']
+ }
+
+ attributeChangedCallback (name, _, newValue) {
+ this.render(window.eventSource && window.eventSource.readyState === 'OPEN')
+ }
+
+ render (connected = false) {
+ if (connected) this.classList.add('connected')
+ console.log('SSE Connection: ', window.eventSource)
+ this.innerHTML = /* html */''
+ }
+}
+customElements.define('sse-connection', SSEConnection)
diff --git a/client/index.html b/client/index.html
index 928f6ed..897f00c 100644
--- a/client/index.html
+++ b/client/index.html
@@ -23,6 +23,7 @@
<main></main>
<aside id="notifications-container"></aside>
+ <sse-connection></sse-connection>
<details class="state">
<summary>state & logs <span class="count"></span></summary>
@@ -129,6 +130,7 @@
<script type="module" src="/components/search-videos.js"></script>
<script type="module" src="/components/channels-list.js"></script>
<script type="module" src="/components/video-element.js"></script>
+ <script type="module" src="/components/sse-connection.js"></script>
<script type="module" src="/components/add-channel-form.js"></script>
<script type="module" src="/components/manage-channels-form.js"></script>
<script type="module" src="/components/video-quality-form.js"></script>
diff --git a/client/main.css b/client/main.css
index e586597..dc74432 100644
--- a/client/main.css
+++ b/client/main.css
@@ -638,3 +638,24 @@ empty-state {
padding: 2px 3px;
opacity: 0.8;
}
+
+
+sse-connection {
+ position: fixed;
+ top: 5rem;
+ left: 2rem;
+ width: 1rem;
+ height: 1rem;
+ z-index: 9999;
+ border-radius: 50%;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+}
+
+sse-connection[data-connected='true'] {
+ background: green;
+}
+sse-connection[data-connected='false'] {
+ background: red;
+}
\ No newline at end of file
diff --git a/client/main.js b/client/main.js
index b06dc9f..d1068eb 100644
--- a/client/main.js
+++ b/client/main.js
@@ -4,11 +4,24 @@ import { createVideoElement } from '../../../../../lib/utils.js'
const store = new Store()
window.state = {
downloading: {},
- summarizing: {}
+ summarizing: {},
+ sseConnected: false
}
-const eventSource = new EventSource('/')
-eventSource.onmessage = (message) => {
+window.eventSource = new EventSource('/')
+window.eventSource.onopen = () => {
+ console.log('[sse] connection opened')
+ window.state.sseConnected = true
+ if (document.querySelector('sse-connection')) document.querySelector('sse-connection').dataset.connected = true
+}
+window.eventSource.onerror = (err) => {
+ console.log('[sse] connection error', err)
+ window.state.sseConnected = false
+ if (document.querySelector('sse-connection')) document.querySelector('sse-connection').dataset.connected = false
+}
+window.eventSource.onmessage = (message) => {
+ if (document.querySelector('sse-connection')) document.querySelector('sse-connection').dataset.connected = true
+ window.state.sseConnected = true
if (!message || !message.data) return console.error('skipping empty message')
try {
const data = JSON.parse(message.data, {})
diff --git a/lib/router/app.js b/lib/router/app.js
index 7842d0e..287d673 100644
--- a/lib/router/app.js
+++ b/lib/router/app.js
@@ -14,6 +14,7 @@ export default function apiHandler (req, res) {
if (url.pathname === '/components/add-channel-form.js') { return fileHandler('client/components/add-channel-form.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/manage-channels-form.js') { return fileHandler('client/components/manage-channels-form.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/video-quality-form.js') { return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res) }
+ if (url.pathname === '/components/sse-connection.js') { return fileHandler('client/components/sse-connection.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/manage-disk-space-form.js') { return fileHandler('client/components/manage-disk-space-form.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/transcode-videos-form.js') { return fileHandler('client/components/transcode-videos-form.js', 'application/javascript')(req, res) }
if (url.pathname === '/components/excluded-terms-form.js') { return fileHandler('client/components/excluded-terms-form.js', 'application/javascript')(req, res) }