This commit is contained in:
19
react/features/power-monitor/actionTypes.ts
Normal file
19
react/features/power-monitor/actionTypes.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* The type of the Redux action which signals that a suspend was detected.
|
||||
*
|
||||
* {
|
||||
* type: SUSPEND_DETECTED
|
||||
* }
|
||||
* @public
|
||||
*/
|
||||
export const SUSPEND_DETECTED = 'SUSPEND_DETECTED';
|
||||
|
||||
/**
|
||||
* The type of the Redux action which signals that a transport needs to be stored.
|
||||
*
|
||||
* {
|
||||
* type: SET_TRANSPORT
|
||||
* }
|
||||
* @public
|
||||
*/
|
||||
export const SET_TRANSPORT = 'SET_TRANSPORT';
|
||||
37
react/features/power-monitor/actions.ts
Normal file
37
react/features/power-monitor/actions.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// @ts-expect-error
|
||||
import { Transport } from '../../../modules/transport';
|
||||
|
||||
import {
|
||||
SET_TRANSPORT,
|
||||
SUSPEND_DETECTED
|
||||
} from './actionTypes';
|
||||
|
||||
/**
|
||||
* Signals that suspend was detected.
|
||||
*
|
||||
* @public
|
||||
* @returns {{
|
||||
* type: SUSPEND_DETECTED
|
||||
* }}
|
||||
*/
|
||||
export function suspendDetected() {
|
||||
return {
|
||||
type: SUSPEND_DETECTED
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Signals setting of a transport.
|
||||
*
|
||||
* @param {Transport} transport - The transport to save in the state.
|
||||
* @returns {{
|
||||
* transport: Transport,
|
||||
* type: string
|
||||
* }}
|
||||
*/
|
||||
export function setTransport(transport?: Transport) {
|
||||
return {
|
||||
type: SET_TRANSPORT,
|
||||
transport
|
||||
};
|
||||
}
|
||||
68
react/features/power-monitor/middleware.ts
Normal file
68
react/features/power-monitor/middleware.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
// @ts-expect-error
|
||||
import { PostMessageTransportBackend, Transport } from '../../../modules/transport';
|
||||
import {
|
||||
CONFERENCE_JOINED,
|
||||
CONFERENCE_LEFT
|
||||
} from '../base/conference/actionTypes';
|
||||
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
|
||||
import { destroyLocalTracks } from '../base/tracks/actions';
|
||||
|
||||
import { SUSPEND_DETECTED } from './actionTypes';
|
||||
import {
|
||||
setTransport,
|
||||
suspendDetected
|
||||
} from './actions';
|
||||
|
||||
MiddlewareRegistry.register(store => next => action => {
|
||||
const result = next(action);
|
||||
const { dispatch, getState } = store;
|
||||
|
||||
switch (action.type) {
|
||||
case CONFERENCE_JOINED: {
|
||||
|
||||
// listens for messages about suspend from power-monitor
|
||||
const transport = new Transport({
|
||||
backend: new PostMessageTransportBackend({
|
||||
postisOptions: { scope: 'jitsi-power-monitor' }
|
||||
})
|
||||
});
|
||||
|
||||
transport.on('event', (event: { event: string; name: string; }) => {
|
||||
if (event && event.name === 'power-monitor' && event.event === 'suspend') {
|
||||
|
||||
dispatch(suspendDetected());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
dispatch(setTransport(transport));
|
||||
break;
|
||||
}
|
||||
|
||||
case CONFERENCE_LEFT: {
|
||||
const { transport } = getState()['features/power-monitor'];
|
||||
|
||||
if (transport) {
|
||||
transport.dispose();
|
||||
}
|
||||
|
||||
dispatch(setTransport());
|
||||
break;
|
||||
}
|
||||
|
||||
case SUSPEND_DETECTED: {
|
||||
dispatch(destroyLocalTracks());
|
||||
|
||||
// FIXME: when refactoring conference.js
|
||||
APP.conference.onSuspendDetected();
|
||||
|
||||
APP.API.notifySuspendDetected();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
56
react/features/power-monitor/reducer.ts
Normal file
56
react/features/power-monitor/reducer.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
// @ts-expect-error
|
||||
import { Transport } from '../../../modules/transport';
|
||||
import ReducerRegistry from '../base/redux/ReducerRegistry';
|
||||
import { set } from '../base/redux/functions';
|
||||
|
||||
import {
|
||||
SET_TRANSPORT,
|
||||
SUSPEND_DETECTED
|
||||
} from './actionTypes';
|
||||
|
||||
export interface IPowerMonitorState {
|
||||
suspendDetected?: boolean;
|
||||
transport?: Transport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces the redux actions of the feature power monitor.
|
||||
*/
|
||||
ReducerRegistry.register<IPowerMonitorState>('features/power-monitor', (state = {}, action): IPowerMonitorState => {
|
||||
switch (action.type) {
|
||||
case SET_TRANSPORT:
|
||||
return _setTransport(state, action.transport);
|
||||
|
||||
case SUSPEND_DETECTED:
|
||||
return _suspendDetected(state);
|
||||
|
||||
}
|
||||
|
||||
return state;
|
||||
});
|
||||
|
||||
/**
|
||||
* Reduces a specific redux action SET_TRANSPORT of the feature power monitor.
|
||||
*
|
||||
* @param {Object} state - The redux state of the feature power monitor.
|
||||
* @param {?Transport} transport - The transport to store in state.
|
||||
* @private
|
||||
* @returns {Object} The new state of the feature power monitor after the reduction of
|
||||
* the specified action.
|
||||
*/
|
||||
function _setTransport(state: IPowerMonitorState, transport?: Transport) {
|
||||
return set(state, 'transport', transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces a specific redux action SUSPEND_DETECTED of the feature overlay.
|
||||
*
|
||||
* @param {Object} state - The redux state of the feature overlay.
|
||||
* @private
|
||||
* @returns {Object} The new state of the feature overlay after the reduction of
|
||||
* the specified action.
|
||||
*/
|
||||
function _suspendDetected(state: IPowerMonitorState) {
|
||||
return set(state, 'suspendDetected', true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user