Created
October 25, 2022 08:24
-
-
Save sasin91/b7c0326723efe82df7565e5a1115540a to your computer and use it in GitHub Desktop.
ZomeTV Broadcast live stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
export default { | |
props: { | |
jwtToken: String, | |
}, | |
data() { | |
return { | |
intervalId: undefined, | |
liveViewers: 0, | |
stopsCount: 0, | |
}; | |
}, | |
render() { | |
return this.$scopedSlots.default({ | |
liveViewers: this.liveViewers, | |
stopsCount: this.stopsCount, | |
fetch: this.fetch, | |
}); | |
}, | |
mounted() { | |
this.intervalId = setInterval(() => { | |
if (!this.jwtToken) { | |
this.$emit("refetch"); | |
return; | |
} | |
this.fetch(); | |
}, 1000); | |
}, | |
beforeDestroy() { | |
clearInterval(this.intervalId); | |
}, | |
methods: { | |
async fetch() { | |
const response = await fetch(`https://stats.mux.com/counts?token=${this.jwtToken}`); | |
const data: { | |
data: Array<{ views: number; viewers: number; updated_at: Date }>; | |
} = await response.json(); | |
const viewers = data.data[0].viewers; | |
const stops = Math.abs(this.liveViewers - viewers); | |
this.liveViewers = viewers; | |
this.stopsCount = stops; | |
}, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment