keepalive.js 769 B

1234567891011121314151617181920212223242526272829303132
  1. import { config } from 'grav-config';
  2. import { userFeedbackError } from './response';
  3. class KeepAlive {
  4. constructor() {
  5. this.active = false;
  6. }
  7. start() {
  8. let timeout = config.admin_timeout / 1.5 * 1000;
  9. this.timer = setInterval(() => this.fetch(), timeout);
  10. this.active = true;
  11. }
  12. stop() {
  13. clearInterval(this.timer);
  14. this.active = false;
  15. }
  16. fetch() {
  17. let data = new FormData();
  18. data.append('admin-nonce', config.admin_nonce);
  19. fetch(`${config.base_url_relative}/task${config.param_sep}keepAlive`, {
  20. credentials: 'same-origin',
  21. method: 'post',
  22. body: data
  23. }).catch(userFeedbackError);
  24. }
  25. }
  26. export default new KeepAlive();