您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  67. store.dispatch(waitForOwner());
  68. } else {
  69. store.dispatch(stopWaitForOwner());
  70. }
  71. break;
  72. case CONFERENCE_JOINED:
  73. if (_isWaitingForOwner(store)) {
  74. store.dispatch(stopWaitForOwner());
  75. }
  76. _hideLoginDialog(store);
  77. break;
  78. case CONFERENCE_LEFT:
  79. store.dispatch(stopWaitForOwner());
  80. break;
  81. case CONNECTION_ESTABLISHED:
  82. _hideLoginDialog(store);
  83. break;
  84. case CONNECTION_FAILED: {
  85. const { error } = action;
  86. error
  87. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  88. && store.dispatch(_openLoginDialog());
  89. break;
  90. }
  91. case STOP_WAIT_FOR_OWNER:
  92. _clearExistingWaitForOwnerTimeout(store);
  93. store.dispatch(hideDialog(WaitForOwnerDialog));
  94. break;
  95. case WAIT_FOR_OWNER: {
  96. _clearExistingWaitForOwnerTimeout(store);
  97. const { handler, timeoutMs } = action;
  98. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  99. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  100. // login dialog every few seconds.
  101. isDialogOpen(store, LoginDialog)
  102. || store.dispatch(_openWaitForOwnerDialog());
  103. break;
  104. }
  105. }
  106. return next(action);
  107. });
  108. /**
  109. * Will clear the wait for conference owner timeout handler if any is currently
  110. * set.
  111. *
  112. * @param {Object} store - The redux store.
  113. * @returns {void}
  114. */
  115. function _clearExistingWaitForOwnerTimeout(
  116. { getState }: { getState: Function }) {
  117. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  118. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  119. }
  120. /**
  121. * Hides {@link LoginDialog} if it's currently displayed.
  122. *
  123. * @param {Object} store - The redux store.
  124. * @returns {void}
  125. */
  126. function _hideLoginDialog({ dispatch }: { dispatch: Dispatch<*> }) {
  127. dispatch(hideDialog(LoginDialog));
  128. }
  129. /**
  130. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  131. *
  132. * @param {Object} store - The redux store.
  133. * @returns {boolean}
  134. */
  135. function _isWaitingForOwner({ getState }: { getState: Function }) {
  136. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  137. }