UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

41 lines (39 loc) 2.13 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useComponentRenderTracking = useComponentRenderTracking; var _react = require("react"); var _v = _interopRequireDefault(require("uuid/v4")); var _compareProps = require("../../../compare-props"); // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead function useComponentRenderTracking(_ref) { var onRender = _ref.onRender, propsDiffingOptions = _ref.propsDiffingOptions, _ref$zeroBasedCount = _ref.zeroBasedCount, zeroBasedCount = _ref$zeroBasedCount === void 0 ? true : _ref$zeroBasedCount; var propsRef = (0, _react.useRef)(); var renderCountRef = (0, _react.useRef)(zeroBasedCount ? 0 : 1); // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead var _useRef = (0, _react.useRef)((0, _v.default)()), componentId = _useRef.current; (0, _react.useEffect)(function () { var lastProps = propsRef.current; var renderCount = renderCountRef.current; var propsDifference; if (propsDiffingOptions !== null && propsDiffingOptions !== void 0 && propsDiffingOptions.enabled && lastProps) { propsDifference = propsDiffingOptions !== null && propsDiffingOptions !== void 0 && propsDiffingOptions.useShallow ? (0, _compareProps.getShallowPropsDifference)(lastProps, propsDiffingOptions.props) : (0, _compareProps.getPropsDifference)(lastProps, propsDiffingOptions.props, 0, 2, propsDiffingOptions === null || propsDiffingOptions === void 0 ? void 0 : propsDiffingOptions.propsToIgnore); } var result = { renderCount: renderCount, propsDifference: propsDifference, componentId: componentId }; onRender(result); if (propsDiffingOptions !== null && propsDiffingOptions !== void 0 && propsDiffingOptions.enabled) { propsRef.current = propsDiffingOptions.props; } renderCountRef.current = renderCountRef.current + 1; }); // No dependencies run on each render }