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

middleware.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // @flow
  2. import { DeviceEventEmitter } from 'react-native';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../app';
  4. import { MiddlewareRegistry } from '../../base/redux';
  5. import { _setListeners } from './actions';
  6. import { _SET_PIP_LISTENERS, REQUEST_PIP_MODE } from './actionTypes';
  7. import { enterPictureInPictureMode } from './functions';
  8. /**
  9. * Middleware that handles Picture-in-Picture requests. Currently it enters
  10. * the native PiP mode on Android, when requested.
  11. *
  12. * @param {Store} store - Redux store.
  13. * @returns {Function}
  14. */
  15. MiddlewareRegistry.register(store => next => action => {
  16. switch (action.type) {
  17. case _SET_PIP_LISTENERS: {
  18. // Remove the current/old listeners.
  19. const { listeners } = store.getState()['features/pip'];
  20. if (listeners) {
  21. for (const listener of listeners) {
  22. listener.remove();
  23. }
  24. }
  25. break;
  26. }
  27. case APP_WILL_MOUNT:
  28. _appWillMount(store);
  29. break;
  30. case APP_WILL_UNMOUNT:
  31. store.dispatch(_setListeners(undefined));
  32. break;
  33. case REQUEST_PIP_MODE:
  34. _enterPictureInPicture(store);
  35. break;
  36. }
  37. return next(action);
  38. });
  39. /**
  40. * Notifies the feature pip that the action {@link APP_WILL_MOUNT} is being
  41. * dispatched within a specific redux {@code store}.
  42. *
  43. * @param {Store} store - The redux store in which the specified {@code action}
  44. * is being dispatched.
  45. * @param {Dispatch} next - The redux dispatch function to dispatch the
  46. * specified {@code action} to the specified {@code store}.
  47. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  48. * being dispatched in the specified {@code store}.
  49. * @private
  50. * @returns {*}
  51. */
  52. function _appWillMount({ dispatch, getState }) {
  53. const context = {
  54. dispatch,
  55. getState
  56. };
  57. const listeners = [
  58. // Android's onUserLeaveHint activity lifecycle callback
  59. DeviceEventEmitter.addListener('onUserLeaveHint', () => {
  60. _enterPictureInPicture(context);
  61. })
  62. ];
  63. dispatch(_setListeners(listeners));
  64. }
  65. /**
  66. * Helper function to enter PiP mode. This is triggered by user request
  67. * (either pressing the button in the toolbox or the home button on Android)
  68. * ans this triggers the PiP mode, iff it's available and we are in a
  69. * conference.
  70. *
  71. * @param {Object} store - Redux store.
  72. * @private
  73. * @returns {void}
  74. */
  75. function _enterPictureInPicture({ getState }) {
  76. const state = getState();
  77. const { app } = state['features/app'];
  78. const { conference, joining } = state['features/base/conference'];
  79. if (app.props.pipAvailable && (conference || joining)) {
  80. enterPictureInPictureMode().catch(e => {
  81. console.warn(`Error entering PiP mode: ${e}`);
  82. });
  83. }
  84. }