Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

middleware.native.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { appNavigate } from '../app/actions';
  4. import {
  5. CONFERENCE_FAILED,
  6. CONFERENCE_JOINED,
  7. CONFERENCE_LEFT
  8. } from '../base/conference';
  9. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection';
  10. import { hideDialog, isDialogOpen } from '../base/dialog';
  11. import {
  12. JitsiConferenceErrors,
  13. JitsiConnectionErrors
  14. } from '../base/lib-jitsi-meet';
  15. import { MiddlewareRegistry } from '../base/redux';
  16. import {
  17. CANCEL_LOGIN,
  18. STOP_WAIT_FOR_OWNER,
  19. WAIT_FOR_OWNER
  20. } from './actionTypes';
  21. import {
  22. openLoginDialog,
  23. openWaitForOwnerDialog,
  24. stopWaitForOwner,
  25. waitForOwner } from './actions.native';
  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. // FIXME Like cancelWaitForOwner, dispatch conferenceLeft to notify
  55. // the external-api.
  56. dispatch(appNavigate(undefined));
  57. }
  58. break;
  59. }
  60. case CONFERENCE_FAILED: {
  61. const { error } = action;
  62. // XXX The feature authentication affords recovery from
  63. // CONFERENCE_FAILED caused by
  64. // JitsiConferenceErrors.AUTHENTICATION_REQUIRED.
  65. let recoverable;
  66. if (error.name === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  67. if (typeof error.recoverable === 'undefined') {
  68. error.recoverable = true;
  69. }
  70. recoverable = error.recoverable;
  71. }
  72. if (recoverable) {
  73. store.dispatch(waitForOwner());
  74. } else {
  75. store.dispatch(stopWaitForOwner());
  76. }
  77. break;
  78. }
  79. case CONFERENCE_JOINED:
  80. if (_isWaitingForOwner(store)) {
  81. store.dispatch(stopWaitForOwner());
  82. }
  83. _hideLoginDialog(store);
  84. break;
  85. case CONFERENCE_LEFT:
  86. store.dispatch(stopWaitForOwner());
  87. break;
  88. case CONNECTION_ESTABLISHED:
  89. _hideLoginDialog(store);
  90. break;
  91. case CONNECTION_FAILED: {
  92. const { error } = action;
  93. if (error
  94. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  95. && typeof error.recoverable === 'undefined') {
  96. error.recoverable = true;
  97. store.dispatch(openLoginDialog());
  98. }
  99. break;
  100. }
  101. case STOP_WAIT_FOR_OWNER:
  102. _clearExistingWaitForOwnerTimeout(store);
  103. store.dispatch(hideDialog(WaitForOwnerDialog));
  104. break;
  105. case WAIT_FOR_OWNER: {
  106. _clearExistingWaitForOwnerTimeout(store);
  107. const { handler, timeoutMs } = action;
  108. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  109. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  110. // login dialog every few seconds.
  111. isDialogOpen(store, LoginDialog)
  112. || store.dispatch(openWaitForOwnerDialog());
  113. break;
  114. }
  115. }
  116. return next(action);
  117. });
  118. /**
  119. * Will clear the wait for conference owner timeout handler if any is currently
  120. * set.
  121. *
  122. * @param {Object} store - The redux store.
  123. * @returns {void}
  124. */
  125. function _clearExistingWaitForOwnerTimeout(
  126. { getState }: { getState: Function }) {
  127. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  128. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  129. }
  130. /**
  131. * Hides {@link LoginDialog} if it's currently displayed.
  132. *
  133. * @param {Object} store - The redux store.
  134. * @returns {void}
  135. */
  136. function _hideLoginDialog({ dispatch }: { dispatch: Dispatch<any> }) {
  137. dispatch(hideDialog(LoginDialog));
  138. }
  139. /**
  140. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  141. *
  142. * @param {Object} store - The redux store.
  143. * @returns {boolean}
  144. */
  145. function _isWaitingForOwner({ getState }: { getState: Function }) {
  146. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  147. }