Vue plugin for polling backend APIs
Observers would fire HTTP Get calls periodically to the backend APIs.
After you call Vue.use(VuePolling)
, you would have a $polling
object in your Vue.prototype. With $polling
you can manage your observers.
this.$polling.addObserver(url)
or with opts:
this.$polling.addObserver(url, {
interval: 5000, // 2000ms by default
maxFails: 10, // break after 10 (5 by defualt) continuous failures
headers: {
'Content-Type': 'application/json'
}
})
As soon as your call addObserver()
, the observer you just defined would start to fire the polling calls.
NOTE
- The url is the unique key of an observer. No duplicated observers of the same url in your whole Vue project.
this.$polling.removeObserver(url)
Listeners are normally defined and manipulated in Vue components.
The package provides the mixin that injects listeners
object in this.$options
.
this.$options.listeners['url1'] = (resp) => {
// do with HTTP response object
}
delete this.$options.listeners['url1']
This will delete all callbacks defined in this vm for the url 'url1'.
Explanation
- You can define multiple callbacks to one single url. That means if you execute the code in 'Define a listner' twice, you would get two callbacks for that url. And it's possible to define same callbacks for same url but in different Vue components.
- When you delete listeners in a Vue component with a url, you are deleting all callbacks you've defined in that Vue component with the same url. But it would not affect callbacks defined in other Vue components with the same url.