UNPKG

bigbluebutton-html-plugin-sdk

Version:

This repository contains the SDK for developing BigBlueButton plugins. Plugins are React components that can be loaded from external sources by the BigBlueButton HTML5 client to extend its functionalities.

55 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useProjectedValue = void 0; var react_1 = require("react"); var utils_1 = require("../utils"); var utils_2 = require("./utils"); // Implementation function useProjectedValue(queryResult, project) { if (!project) return queryResult; var isArray = Array.isArray(queryResult.data); // Store the previous projected data to compare against var previousProjectedDataRef = (0, react_1.useRef)(undefined); // Compute the new projected value from the data field var currentProjectedData = (0, react_1.useMemo)(function () { if (!queryResult.data) return undefined; if (isArray) { return queryResult.data.map(function (item) { return project(item); }); } return project(queryResult.data); }, [queryResult.data, project, isArray]); // Initialize state with the wrapper structure var _a = (0, react_1.useState)({ loading: queryResult.loading, data: currentProjectedData, error: queryResult.error, }), projectionResult = _a[0], setProjectionResult = _a[1]; (0, react_1.useEffect)(function () { // Perform deep equality check using sortedStringify var currentSerialized = (0, utils_1.sortedStringify)(currentProjectedData); var previousSerialized = (0, utils_1.sortedStringify)(previousProjectedDataRef.current); // Check if loading or error states changed var loadingChanged = queryResult.loading !== projectionResult.loading; var errorChanged = (0, utils_2.hasErrorChanged)(projectionResult.error, queryResult.error); // Only update if the projected data, loading, or error state has changed if (currentSerialized !== previousSerialized || loadingChanged || errorChanged) { previousProjectedDataRef.current = currentProjectedData; setProjectionResult({ loading: queryResult.loading, data: currentProjectedData, error: queryResult.error, }); } }, [ currentProjectedData, queryResult.loading, queryResult.error, projectionResult.loading, projectionResult.error, ]); return projectionResult; } exports.useProjectedValue = useProjectedValue; //# sourceMappingURL=hooks.js.map