WindowWaiter.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Waiter to handle events related to the window object.
  3. *
  4. * @author n1474335 [n1474335@gmail.com]
  5. * @copyright Crown Copyright 2016
  6. * @license Apache-2.0
  7. *
  8. * @constructor
  9. * @param {App} app - The main view object for CyberChef.
  10. */
  11. const WindowWaiter = function(app) {
  12. this.app = app;
  13. };
  14. /**
  15. * Handler for window resize events.
  16. * Resets the layout of CyberChef's panes after 200ms (so that continuous resizing doesn't cause
  17. * continuous resetting).
  18. */
  19. WindowWaiter.prototype.windowResize = function() {
  20. clearTimeout(this.resetLayoutTimeout);
  21. this.resetLayoutTimeout = setTimeout(this.app.resetLayout.bind(this.app), 200);
  22. };
  23. /**
  24. * Handler for window blur events.
  25. * Saves the current time so that we can calculate how long the window was unfocussed for when
  26. * focus is returned.
  27. */
  28. WindowWaiter.prototype.windowBlur = function() {
  29. this.windowBlurTime = new Date().getTime();
  30. };
  31. /**
  32. * Handler for window focus events.
  33. *
  34. * When a browser tab is unfocused and the browser has to run lots of dynamic content in other
  35. * tabs, it swaps out the memory for that tab.
  36. * If the CyberChef tab has been unfocused for more than a minute, we run a silent bake which will
  37. * force the browser to load and cache all the relevant JavaScript code needed to do a real bake.
  38. * This will stop baking taking a long time when the CyberChef browser tab has been unfocused for
  39. * a long time and the browser has swapped out all its memory.
  40. */
  41. WindowWaiter.prototype.windowFocus = function() {
  42. const unfocusedTime = new Date().getTime() - this.windowBlurTime;
  43. if (unfocusedTime > 60000) {
  44. this.app.silentBake();
  45. }
  46. };
  47. export default WindowWaiter;