jiti-meet/react/features/desktop-picker/reducer.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-03-30 16:58:31 +00:00
import { ReducerRegistry } from '../base/redux';
2017-04-06 16:45:36 +00:00
2017-03-30 16:58:31 +00:00
import {
RESET_DESKTOP_SOURCES,
UPDATE_DESKTOP_SOURCES
} from './actionTypes';
2017-04-06 16:45:36 +00:00
const DEFAULT_STATE = {
2017-03-30 16:58:31 +00:00
screen: [],
window: []
};
/**
* Listen for actions that mutate the known available DesktopCapturerSources.
*
* @param {Object[]} state - Current state.
* @param {Object} action - Action object.
* @param {string} action.type - Type of action.
* @param {Array} action.sources - DesktopCapturerSources.
* @returns {Object}
*/
ReducerRegistry.register(
2017-04-06 16:45:36 +00:00
'features/desktop-picker',
(state = DEFAULT_STATE, action) => {
2017-03-30 16:58:31 +00:00
switch (action.type) {
case RESET_DESKTOP_SOURCES:
2017-04-06 16:45:36 +00:00
return { ...DEFAULT_STATE };
2017-03-30 16:58:31 +00:00
case UPDATE_DESKTOP_SOURCES:
2017-04-06 16:45:36 +00:00
return _seperateSourcesByType(action.sources);
2017-03-30 16:58:31 +00:00
default:
return state;
}
});
/**
2017-04-06 16:45:36 +00:00
* Converts an array of DesktopCapturerSources to an object with types for keys
* and values being an array with sources of the key's type.
2017-03-30 16:58:31 +00:00
*
* @param {Array} sources - DesktopCapturerSources.
* @private
2017-04-06 16:45:36 +00:00
* @returns {Object} An object with the sources split into seperate arrays based
* on source type.
2017-03-30 16:58:31 +00:00
*/
2017-04-06 16:45:36 +00:00
function _seperateSourcesByType(sources = []) {
2017-03-30 16:58:31 +00:00
const sourcesByType = {
screen: [],
window: []
};
sources.forEach(source => {
2017-04-06 16:45:36 +00:00
const idParts = source.id.split(':');
const type = idParts[0];
2017-03-30 16:58:31 +00:00
2017-04-06 16:45:36 +00:00
if (sourcesByType[type]) {
sourcesByType[type].push(source);
2017-03-30 16:58:31 +00:00
}
});
return sourcesByType;
}