update_checker.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS207: Consider shorter variations of null checks
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  7. */
  8. app.UpdateChecker = class UpdateChecker {
  9. constructor() {
  10. this.checkDocs = this.checkDocs.bind(this);
  11. this.onFocus = this.onFocus.bind(this);
  12. this.lastCheck = Date.now();
  13. $.on(window, 'focus', this.onFocus);
  14. if (app.serviceWorker != null) {
  15. app.serviceWorker.on('updateready', this.onUpdateReady);
  16. }
  17. setTimeout(this.checkDocs, 0);
  18. }
  19. check() {
  20. if (app.serviceWorker) {
  21. app.serviceWorker.update();
  22. } else {
  23. ajax({
  24. url: $('script[src*="application"]').getAttribute('src'),
  25. dataType: 'application/javascript',
  26. error: (_, xhr) => { if (xhr.status === 404) { return this.onUpdateReady(); } }
  27. });
  28. }
  29. }
  30. onUpdateReady() {
  31. new app.views.Notif('UpdateReady', {autoHide: null});
  32. }
  33. checkDocs() {
  34. if (!app.settings.get('manualUpdate')) {
  35. app.docs.updateInBackground();
  36. } else {
  37. app.docs.checkForUpdates(i => { if (i > 0) { return this.onDocsUpdateReady(); } });
  38. }
  39. }
  40. onDocsUpdateReady() {
  41. new app.views.Notif('UpdateDocs', {autoHide: null});
  42. }
  43. onFocus() {
  44. if ((Date.now() - this.lastCheck) > 21600e3) {
  45. this.lastCheck = Date.now();
  46. this.check();
  47. }
  48. }
  49. };