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.2KB

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