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 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 { enterPictureInPicture, _setEmitterSubscriptions } from './actions';
  6. import { _SET_EMITTER_SUBSCRIPTIONS } from './actionTypes';
  7. /**
  8. * Middleware that handles Picture-in-Picture requests. Currently it enters
  9. * the native PiP mode on Android, when requested.
  10. *
  11. * @param {Store} store - Redux store.
  12. * @returns {Function}
  13. */
  14. MiddlewareRegistry.register(store => next => action => {
  15. switch (action.type) {
  16. case APP_WILL_MOUNT:
  17. return _appWillMount(store, next, action);
  18. case APP_WILL_UNMOUNT:
  19. store.dispatch(_setEmitterSubscriptions(undefined));
  20. break;
  21. case _SET_EMITTER_SUBSCRIPTIONS: {
  22. // Remove the current/old EventEmitter subscriptions.
  23. const { emitterSubscriptions } = store.getState()['features/pip'];
  24. if (emitterSubscriptions) {
  25. for (const emitterSubscription of emitterSubscriptions) {
  26. // XXX We may be removing an EventEmitter subscription which is
  27. // in both the old and new Array of EventEmitter subscriptions!
  28. // Thankfully, we don't have such a practical use case at the
  29. // time of this writing.
  30. emitterSubscription.remove();
  31. }
  32. }
  33. break;
  34. }
  35. }
  36. return next(action);
  37. });
  38. /**
  39. * Notifies the feature pip that the action {@link APP_WILL_MOUNT} is being
  40. * dispatched within a specific redux {@code store}.
  41. *
  42. * @param {Store} store - The redux store in which the specified {@code action}
  43. * is being dispatched.
  44. * @param {Dispatch} next - The redux dispatch function to dispatch the
  45. * specified {@code action} to the specified {@code store}.
  46. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  47. * being dispatched in the specified {@code store}.
  48. * @private
  49. * @returns {*} The value returned by {@code next(action)}.
  50. */
  51. function _appWillMount({ dispatch }, next, action) {
  52. dispatch(_setEmitterSubscriptions([
  53. // Android's onUserLeaveHint activity lifecycle callback
  54. DeviceEventEmitter.addListener(
  55. 'onUserLeaveHint',
  56. () => dispatch(enterPictureInPicture()))
  57. ]));
  58. return next(action);
  59. }