mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
91 lines • 3.04 kB
JavaScript
/**
* Checks the grid configuration and calculates various parameters based on the number of rows, columns, and active elements.
*
* @param {CheckGridOptions} options - The options for checking the grid.
* @param {number} options.rows - The number of rows in the grid.
* @param {number} options.cols - The number of columns in the grid.
* @param {number} options.actives - The number of active elements in the grid.
* @returns {Promise<[boolean, number, number, number, number, number, number] | void>} A promise that resolves to a tuple containing:
* - `removeAltGrid` (boolean): Indicates whether to remove the alternate grid.
* - `numtoadd` (number): The number of elements to add.
* - `numRows` (number): The number of rows.
* - `numCols` (number): The number of columns.
* - `remainingVideos` (number): The number of remaining videos.
* - `actualRows` (number): The actual number of rows.
* - `lastrowcols` (number): The number of columns in the last row.
*
* If an error occurs, it logs the error to the console.
*
* @example
* const options = {
* rows: 3,
* cols: 4,
* actives: 10,
* };
*
* checkGrid(options)
* .then(result => {
* console.log('Grid check result:', result);
* // Example output: [true, 2, 3, 4, 2, 3, 4]
* })
* .catch(error => {
* console.error('Error checking grid:', error);
* });
*/
export async function checkGrid({ rows, cols, actives }) {
try {
let numRows = 0;
let numCols = 0;
let lastrow = 0;
let lastrowcols = 0;
let remainingVideos = 0;
let numtoadd = 0;
let actualRows = 0;
let removeAltGrid = false;
if (rows * cols !== actives) {
if (rows * cols > actives) {
const res = actives - (rows - 1) * cols;
if (cols * 0.5 < res) {
lastrow = rows;
lastrowcols = res;
remainingVideos = lastrowcols;
}
else {
lastrowcols = res + cols;
lastrow = rows - 1;
remainingVideos = lastrowcols;
}
numRows = lastrow - 1;
numCols = cols;
numtoadd = (lastrow - 1) * numCols;
actualRows = lastrow;
removeAltGrid = false;
}
}
else {
// Perfect fit
numCols = cols;
numRows = rows;
lastrow = rows;
lastrowcols = cols;
remainingVideos = 0;
numtoadd = lastrow * numCols;
actualRows = lastrow;
removeAltGrid = true;
}
return [
removeAltGrid,
numtoadd,
numRows,
numCols,
remainingVideos,
actualRows,
lastrowcols,
];
}
catch (error) {
console.log('checkGrid error', error);
// throw error;
}
}
//# sourceMappingURL=checkGrid.js.map