keepalive.js 831 B

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