UNPKG

@atlaskit/editor-common

Version:

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

37 lines (36 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useComponentRenderTracking = useComponentRenderTracking; var _react = require("react"); var _adfSchema = require("@atlaskit/adf-schema"); var _compareProps = require("../../../compare-props"); 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); var _useRef = (0, _react.useRef)(_adfSchema.uuid.generate()), 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 }