update_checker.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.lastCheck = Date.now();
  11. $.on(window, "focus", () => this.onFocus());
  12. if (app.serviceWorker != null) {
  13. app.serviceWorker.on("updateready", () => this.onUpdateReady());
  14. }
  15. setTimeout(() => this.checkDocs(), 0);
  16. }
  17. check() {
  18. if (app.serviceWorker) {
  19. app.serviceWorker.update();
  20. } else {
  21. ajax({
  22. url: $('script[src*="application"]').getAttribute("src"),
  23. dataType: "application/javascript",
  24. error: (_, xhr) => {
  25. if (xhr.status === 404) {
  26. return this.onUpdateReady();
  27. }
  28. },
  29. });
  30. }
  31. }
  32. onUpdateReady() {
  33. new app.views.Notif("UpdateReady", { autoHide: null });
  34. }
  35. checkDocs() {
  36. if (!app.settings.get("manualUpdate")) {
  37. app.docs.updateInBackground();
  38. } else {
  39. app.docs.checkForUpdates((i) => {
  40. if (i > 0) {
  41. return this.onDocsUpdateReady();
  42. }
  43. });
  44. }
  45. }
  46. onDocsUpdateReady() {
  47. new app.views.Notif("UpdateDocs", { autoHide: null });
  48. }
  49. onFocus() {
  50. if (Date.now() - this.lastCheck > 21600e3) {
  51. this.lastCheck = Date.now();
  52. this.check();
  53. }
  54. }
  55. };