Cleaned up snapshot polling

This commit is contained in:
Joel Collins 2019-09-20 16:34:52 +01:00
parent 673d3fecf7
commit 5690914c0c

View file

@ -61,27 +61,36 @@ export default {
mounted() { mounted() {
// Try to load the microscope snapshot when mounted // Try to load the microscope snapshot when mounted
this.getSnapshotRequest() this.checkSnapshotAvailable()
}, },
beforeDestroy () { beforeDestroy () {
clearInterval(this.polling) // Clear the polling timer
if (this.polling !== null) {
clearInterval(this.polling)
}
}, },
methods: { methods: {
getSnapshotRequest: function() { checkSnapshotAvailable: function() {
// Fetches the list of routes from the host, and starts
// a snapshot timer if the snapshot route is available
// Send tag request // Send tag request
axios.get(this.snapshotURL) axios.get(this.routesURL)
.then(() => { .then((response) => {
// Tell the view that a snapshot is available // If the host has a snapshot route available
this.snapshotAvailable = true if ("/api/v1/snapshot" in response.data) {
// Set the snapshot image src to the snapshot URL // Tell the view that a snapshot is available
// Note, we add a timestamp argument to act as a cache-breaker this.snapshotAvailable = true
this.snapshotSrc = `${this.snapshotURL}?${Date.now()}` // Update the snapshot URL
// If not already polling, start polling this.updateSnapshotURL()
if (this.polling === null) { // If not already polling, start polling
this.updateSnapshotPoll() if (this.polling === null) {
this.updateSnapshotPoll()
}
} }
}) })
.catch(error => { .catch(error => {
// Tell the view that a snapshot is not available, and ignore // Tell the view that a snapshot is not available, and ignore
@ -89,17 +98,25 @@ export default {
}) })
}, },
updateSnapshotURL: function() {
// Set the snapshot image src to the snapshot URL,
// adding a timestamp argument to act as a cache-breaker
this.snapshotSrc = `${this.snapshotURL}?${Date.now()}`
console.log(`Updating snapshot URL to ${this.snapshotSrc}`)
},
updateSnapshotPoll: function() { updateSnapshotPoll: function() {
// Start a timer to call updateSnapshotURL periodically
this.polling = setInterval(() => { this.polling = setInterval(() => {
this.getSnapshotRequest() this.updateSnapshotURL()
}, 15000) }, 15000)
} }
}, },
created: function () {
},
computed: { computed: {
routesURL: function () {
return `http://${this.hostname}:${this.port}/routes`
},
snapshotURL: function () { snapshotURL: function () {
return `http://${this.hostname}:${this.port}/api/v1/snapshot` return `http://${this.hostname}:${this.port}/api/v1/snapshot`
}, },