You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // @flow
  2. import { appNavigate } from '../app';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT
  7. } from '../base/conference';
  8. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection';
  9. import { hideDialog, isDialogOpen } from '../base/dialog';
  10. import {
  11. JitsiConferenceErrors,
  12. JitsiConnectionErrors
  13. } from '../base/lib-jitsi-meet';
  14. import { MiddlewareRegistry } from '../base/redux';
  15. import {
  16. _openLoginDialog,
  17. _openWaitForOwnerDialog,
  18. stopWaitForOwner,
  19. waitForOwner
  20. } from './actions';
  21. import {
  22. CANCEL_LOGIN,
  23. CANCEL_WAIT_FOR_OWNER,
  24. STOP_WAIT_FOR_OWNER,
  25. WAIT_FOR_OWNER
  26. } from './actionTypes';
  27. import { LoginDialog, WaitForOwnerDialog } from './components';
  28. /**
  29. * Middleware that captures connection or conference failed errors and controls
  30. * {@link WaitForOwnerDialog} and {@link LoginDialog}.
  31. *
  32. * FIXME Some of the complexity was introduced by the lack of dialog stacking.
  33. *
  34. * @param {Store} store - Redux store.
  35. * @returns {Function}
  36. */
  37. MiddlewareRegistry.register(store => next => action => {
  38. switch (action.type) {
  39. case CANCEL_LOGIN: {
  40. const { thenableWithCancel }
  41. = store.getState()['features/authentication'];
  42. thenableWithCancel && thenableWithCancel.cancel();
  43. // The LoginDialog can be opened on top of "wait for owner". The app
  44. // should navigate only if LoginDialog was open without the
  45. // WaitForOwnerDialog.
  46. if (!isDialogOpen(store, WaitForOwnerDialog)) {
  47. if (_isWaitingForOwner(store)) {
  48. // Instead of hiding show the new one.
  49. const result = next(action);
  50. store.dispatch(_openWaitForOwnerDialog());
  51. return result;
  52. }
  53. // Go back to the app's entry point.
  54. _hideLoginDialog(store);
  55. store.dispatch(appNavigate(undefined));
  56. }
  57. break;
  58. }
  59. case CANCEL_WAIT_FOR_OWNER: {
  60. const result = next(action);
  61. store.dispatch(stopWaitForOwner());
  62. store.dispatch(appNavigate(undefined));
  63. return result;
  64. }
  65. case CONFERENCE_FAILED:
  66. if (action.error.name
  67. === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  68. store.dispatch(waitForOwner());
  69. } else {
  70. store.dispatch(stopWaitForOwner());
  71. }
  72. break;
  73. case CONFERENCE_JOINED:
  74. if (_isWaitingForOwner(store)) {
  75. store.dispatch(stopWaitForOwner());
  76. }
  77. _hideLoginDialog(store);
  78. break;
  79. case CONFERENCE_LEFT:
  80. store.dispatch(stopWaitForOwner());
  81. break;
  82. case CONNECTION_ESTABLISHED:
  83. _hideLoginDialog(store);
  84. break;
  85. case CONNECTION_FAILED: {
  86. const { error } = action;
  87. error
  88. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  89. && store.dispatch(_openLoginDialog());
  90. break;
  91. }
  92. case STOP_WAIT_FOR_OWNER:
  93. _clearExistingWaitForOwnerTimeout(store);
  94. store.dispatch(hideDialog(WaitForOwnerDialog));
  95. break;
  96. case WAIT_FOR_OWNER: {
  97. _clearExistingWaitForOwnerTimeout(store);
  98. const { handler, timeoutMs } = action;
  99. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  100. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  101. // login dialog every few seconds.
  102. isDialogOpen(store, LoginDialog)
  103. || store.dispatch(_openWaitForOwnerDialog());
  104. break;
  105. }
  106. }
  107. return next(action);
  108. });
  109. /**
  110. * Will clear the wait for conference owner timeout handler if any is currently
  111. * set.
  112. *
  113. * @param {Object} store - The redux store.
  114. * @returns {void}
  115. */
  116. function _clearExistingWaitForOwnerTimeout(
  117. { getState }: { getState: Function }) {
  118. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  119. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  120. }
  121. /**
  122. * Hides {@link LoginDialog} if it's currently displayed.
  123. *
  124. * @param {Object} store - The redux store.
  125. * @returns {void}
  126. */
  127. function _hideLoginDialog({ dispatch }: { dispatch: Dispatch<*> }) {
  128. dispatch(hideDialog(LoginDialog));
  129. }
  130. /**
  131. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  132. *
  133. * @param {Object} store - The redux store.
  134. * @returns {boolean}
  135. */
  136. function _isWaitingForOwner({ getState }: { getState: Function }) {
  137. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  138. }