2
0

session.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @typedef {module:shared-api-public.ConversePrivateGlobal} ConversePrivateGlobal
  3. */
  4. import log from "@converse/log";
  5. import { getOpenPromise } from '@converse/openpromise';
  6. import { settings_api } from '../shared/settings/api.js';
  7. import { getInitSettings } from '../shared/settings/utils.js';
  8. const settings = settings_api;
  9. /**
  10. * We distinguish between UniView and MultiView instances.
  11. *
  12. * UniView means that only one chat is visible, even though there might be multiple ongoing chats.
  13. * MultiView means that multiple chats may be visible simultaneously.
  14. */
  15. export function isUniView () {
  16. return ['fullscreen', 'embedded'].includes(settings.get("view_mode"));
  17. }
  18. export function isTestEnv () {
  19. return getInitSettings()['bosh_service_url'] === 'montague.lit/http-bind';
  20. }
  21. export function getUnloadEvent () {
  22. if ('onpagehide' in window) {
  23. // Pagehide gets thrown in more cases than unload. Specifically it
  24. // gets thrown when the page is cached and not just
  25. // closed/destroyed. It's the only viable event on mobile Safari.
  26. // https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
  27. return 'pagehide';
  28. } else if ('onbeforeunload' in window) {
  29. return 'beforeunload';
  30. }
  31. return 'unload';
  32. }
  33. /**
  34. * @param {ConversePrivateGlobal} _converse
  35. * @param {string} name
  36. */
  37. export function replacePromise (_converse, name) {
  38. const existing_promise = _converse.promises[name];
  39. if (!existing_promise) {
  40. throw new Error(`Tried to replace non-existing promise: ${name}`);
  41. }
  42. if (existing_promise.replace) {
  43. /** @type {import('../shared/types').ReplaceableOpenPromise} */
  44. const promise = getOpenPromise();
  45. promise.replace = existing_promise.replace;
  46. _converse.promises[name] = promise;
  47. } else {
  48. log.debug(`Not replacing promise "${name}"`);
  49. }
  50. }
  51. /**
  52. * @param {ConversePrivateGlobal} _converse
  53. * @returns {boolean}
  54. */
  55. export function shouldClearCache (_converse) {
  56. const { api } = _converse;
  57. return !_converse.state.config.get('trusted') ||
  58. api.settings.get('clear_cache_on_logout') ||
  59. isTestEnv();
  60. }
  61. /**
  62. * @param {ConversePrivateGlobal} _converse
  63. */
  64. export async function tearDown (_converse) {
  65. const { api } = _converse;
  66. await api.trigger('beforeTearDown', {'synchronous': true});
  67. api.trigger('afterTearDown');
  68. return _converse;
  69. }
  70. /**
  71. * @param {ConversePrivateGlobal} _converse
  72. */
  73. export function clearSession (_converse) {
  74. shouldClearCache(_converse) && _converse.api.user.settings.clear();
  75. _converse.initSession();
  76. /**
  77. * Synchronouse event triggered once the user session has been cleared,
  78. * for example when the user has logged out or when Converse has
  79. * disconnected for some other reason.
  80. * @event _converse#clearSession
  81. */
  82. return _converse.api.trigger('clearSession', {'synchronous': true});
  83. }