update_checker.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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) => {
  27. if (xhr.status === 404) {
  28. return this.onUpdateReady();
  29. }
  30. },
  31. });
  32. }
  33. }
  34. onUpdateReady() {
  35. new app.views.Notif("UpdateReady", { autoHide: null });
  36. }
  37. checkDocs() {
  38. if (!app.settings.get("manualUpdate")) {
  39. app.docs.updateInBackground();
  40. } else {
  41. app.docs.checkForUpdates((i) => {
  42. if (i > 0) {
  43. return this.onDocsUpdateReady();
  44. }
  45. });
  46. }
  47. }
  48. onDocsUpdateReady() {
  49. new app.views.Notif("UpdateDocs", { autoHide: null });
  50. }
  51. onFocus() {
  52. if (Date.now() - this.lastCheck > 21600e3) {
  53. this.lastCheck = Date.now();
  54. this.check();
  55. }
  56. }
  57. };