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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @flow
  2. import { PostMessageTransportBackend, Transport } from '@jitsi/js-utils/transport';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import { CONFERENCE_JOINED } from '../base/conference';
  5. import { PARTICIPANT_LEFT } from '../base/participants';
  6. import { MiddlewareRegistry } from '../base/redux';
  7. import {
  8. clearRequest, setReceiverTransport, setRemoteControlActive, stopController, stopReceiver
  9. } from './actions';
  10. import { REMOTE_CONTROL_MESSAGE_NAME } from './constants';
  11. import { onRemoteControlAPIEvent } from './functions';
  12. import './subscriber';
  13. /**
  14. * The redux middleware for the remote control feature.
  15. *
  16. * @param {Store} store - The redux store.
  17. * @returns {Function}
  18. */
  19. MiddlewareRegistry.register(store => next => async action => {
  20. switch (action.type) {
  21. case APP_WILL_MOUNT: {
  22. const { dispatch } = store;
  23. dispatch(setReceiverTransport(new Transport({
  24. backend: new PostMessageTransportBackend({
  25. postisOptions: { scope: 'jitsi-remote-control' }
  26. })
  27. })));
  28. break;
  29. }
  30. case APP_WILL_UNMOUNT: {
  31. const { getState, dispatch } = store;
  32. const { transport } = getState()['features/remote-control'].receiver;
  33. if (transport) {
  34. transport.dispose();
  35. dispatch(setReceiverTransport());
  36. }
  37. break;
  38. }
  39. case CONFERENCE_JOINED: {
  40. const result = next(action);
  41. const { getState } = store;
  42. const { transport } = getState()['features/remote-control'].receiver;
  43. if (transport) {
  44. // We expect here that even if we receive the supported event earlier
  45. // it will be cached and we'll receive it.
  46. transport.on('event', event => {
  47. if (event.name === REMOTE_CONTROL_MESSAGE_NAME) {
  48. onRemoteControlAPIEvent(event, store);
  49. return true;
  50. }
  51. return false;
  52. });
  53. }
  54. return result;
  55. }
  56. case PARTICIPANT_LEFT: {
  57. const { getState, dispatch } = store;
  58. const state = getState();
  59. const { id } = action.participant;
  60. const { receiver, controller } = state['features/remote-control'];
  61. const { requestedParticipant, controlled } = controller;
  62. if (id === controlled) {
  63. dispatch(stopController());
  64. }
  65. if (id === requestedParticipant) {
  66. dispatch(clearRequest());
  67. dispatch(setRemoteControlActive(false));
  68. }
  69. if (receiver?.controller === id) {
  70. dispatch(stopReceiver(false, true));
  71. }
  72. break;
  73. }
  74. }
  75. return next(action);
  76. });