1
0

update_checker.js 1.2 KB

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