Skip to content

Commit

Permalink
Add bot connection indicator
Browse files Browse the repository at this point in the history
Signed-off-by: Knut Ahlers <knut@ahlers.me>
  • Loading branch information
Luzifer committed Jun 17, 2024
1 parent e8c2c60 commit d10c78a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/components/_headNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
>
<ul class="navbar-nav me-auto mb-2 mb-lg-0" />
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li
v-if="!socketConnected"
class="nav-item d-flex align-content-center"
>
<span class="navbar-text me-2">
<i class="fas fa-cloud fa-fw text-warning" />
</span>
</li>
<li
v-if="isLoggedIn"
class="nav-item dropdown"
Expand Down Expand Up @@ -58,6 +66,7 @@
</template>

<script lang="ts">
import BusEventTypes from '../helpers/busevents'
import { defineComponent } from 'vue'
import { Dropdown } from 'bootstrap'
Expand All @@ -68,13 +77,27 @@ export default defineComponent({
},
},
data() {
return {
socketConnected: false,
}
},
methods: {
logout() {
this.bus.emit('logout')
},
},
mounted() {
this.bus.on(BusEventTypes.NotifySocketConnected, () => {
this.socketConnected = true
})
this.bus.on(BusEventTypes.NotifySocketDisconnected, () => {
this.socketConnected = false
})
if (this.isLoggedIn) {
new Dropdown(this.$refs.userMenuToggle as Element)
}
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/busevents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ enum BusEventTypes {
FetchError = 'fetchError',
LoadingData = 'loadingData',
LoginProcessing = 'loginProcessing',
NotifySocketConnected = 'notifySocketConnected',
NotifySocketDisconnected = 'notifySocketDisconnected',
RaffleChanged = 'raffleChanged',
RaffleEntryChanged = 'raffleEntryChanged',
Toast = 'toast',
Expand Down
10 changes: 7 additions & 3 deletions src/helpers/configNotify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import BusEventTypes from './busevents'

class ConfigNotifyListener {
private backoff: number = 100

private listener: Function
private eventListener: Function

private socket: WebSocket | null = null

constructor(listener: Function) {
this.listener = listener
this.eventListener = listener
this.connect()
}

Expand All @@ -21,6 +23,7 @@ class ConfigNotifyListener {

this.socket.onopen = () => {
console.debug('[notify] Socket connected')
this.eventListener(BusEventTypes.NotifySocketConnected)
}

this.socket.onmessage = evt => {
Expand All @@ -30,12 +33,13 @@ class ConfigNotifyListener {
this.backoff = 100 // We've received a message, reset backoff

if (msg.msg_type !== 'ping') {
this.listener(msg.msg_type)
this.eventListener(msg.msg_type)
}
}

this.socket.onclose = evt => {
console.debug(`[notify] Socket was closed wasClean=${evt.wasClean}`)
this.eventListener(BusEventTypes.NotifySocketDisconnected)
this.updateBackoffAndReconnect()
}
}
Expand Down

0 comments on commit d10c78a

Please sign in to comment.