Add new global pinia webcron store to handle webcron requests. The old implementation would add additional intervals for each page request.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import axios from 'axios';
|
||||
|
||||
export const webcronStore = defineStore('webcron', {
|
||||
state: () => {
|
||||
return {
|
||||
timeout: 1000 * 30,
|
||||
interval: 0,
|
||||
url: '/webcron'
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
start() {
|
||||
if (this.interval) return
|
||||
this.triggerWebcron(this.url)
|
||||
this.interval = setInterval(this.triggerWebcron, this.timeout, this.url)
|
||||
},
|
||||
|
||||
stop() {
|
||||
clearInterval(this.interval)
|
||||
this.interval = 0
|
||||
},
|
||||
triggerWebcron: async (url: string) => {
|
||||
await axios.get(url)
|
||||
.catch(function (response) {
|
||||
if (response.status >= 400) {
|
||||
console.error(response.message)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user