UNPKG

@atlaskit/editor-common

Version:

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

113 lines (110 loc) 5.59 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.RenderCountProfiler = exports.PROFILER_KEY = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var PROFILER_KEY = exports.PROFILER_KEY = '__editorRenderCountProfiler'; var RenderCountProfiler = exports.RenderCountProfiler = /*#__PURE__*/function () { /** * The singleton/cached instance of RenderCountProfiler that will be shared * betweenRenderCountProfiler.getInstance() calls */ function RenderCountProfiler(_ref) { var store = _ref.store; (0, _classCallCheck2.default)(this, RenderCountProfiler); this.store = store; } /** * Returns the singleton/cached instance of RenderCountProfiler that * currently exists. If it hasn't been instantiated yet, the singleton * instance will be created using the given params. Returns the latest * singleton/instance. */ (0, _createClass2.default)(RenderCountProfiler, [{ key: "getData", value: function getData(profilerKey) { var _this$store; return (_this$store = this.store) === null || _this$store === void 0 ? void 0 : _this$store[profilerKey]; } }, { key: "enable", value: function enable() { this.store[PROFILER_KEY] = _objectSpread(_objectSpread({}, this.store[PROFILER_KEY]), {}, { enabled: true }); } }, { key: "remove", value: function remove() { delete this.store[PROFILER_KEY]; } }, { key: "isEnabled", value: function isEnabled() { var _this$store2; return Boolean((_this$store2 = this.store) === null || _this$store2 === void 0 || (_this$store2 = _this$store2[PROFILER_KEY]) === null || _this$store2 === void 0 ? void 0 : _this$store2.enabled); } }, { key: "setRenderCount", value: function setRenderCount(_ref2) { var componentId = _ref2.componentId, renderCount = _ref2.renderCount, instanceId = _ref2.instanceId; var profilerData = this.store[PROFILER_KEY]; var instance = { count: renderCount }; var existingComponents = profilerData === null || profilerData === void 0 ? void 0 : profilerData.components; var existingInstances = existingComponents === null || existingComponents === void 0 ? void 0 : existingComponents[componentId]; var updatedComponent = _objectSpread(_objectSpread({}, existingInstances), {}, (0, _defineProperty2.default)({}, instanceId, instance)); this.store[PROFILER_KEY] = _objectSpread(_objectSpread({}, profilerData), {}, { components: _objectSpread(_objectSpread({}, existingComponents), {}, (0, _defineProperty2.default)({}, componentId, updatedComponent)) }); } }, { key: "getInstanceRenderCounters", value: function getInstanceRenderCounters(_ref3) { var _this$store$PROFILER_, _this$store3; var componentId = _ref3.componentId; var component = (_this$store$PROFILER_ = (_this$store3 = this.store) === null || _this$store3 === void 0 || (_this$store3 = _this$store3[PROFILER_KEY]) === null || _this$store3 === void 0 || (_this$store3 = _this$store3.components) === null || _this$store3 === void 0 ? void 0 : _this$store3[componentId]) !== null && _this$store$PROFILER_ !== void 0 ? _this$store$PROFILER_ : {}; var counters = []; for (var _instanceId in component) { var counter = { instanceId: _instanceId, count: component[_instanceId].count }; counters.push(counter); } return counters; } }, { key: "getRenderCount", value: function getRenderCount(_ref4) { var _this$store$PROFILER_2, _this$store4; var componentId = _ref4.componentId; var component = (_this$store$PROFILER_2 = (_this$store4 = this.store) === null || _this$store4 === void 0 || (_this$store4 = _this$store4[PROFILER_KEY]) === null || _this$store4 === void 0 || (_this$store4 = _this$store4.components) === null || _this$store4 === void 0 ? void 0 : _this$store4[componentId]) !== null && _this$store$PROFILER_2 !== void 0 ? _this$store$PROFILER_2 : {}; var total = 0; for (var _instanceId2 in component) { total += component[_instanceId2].count; } return total; } }], [{ key: "getInstance", value: function getInstance(params) { if (!RenderCountProfiler.instance) { RenderCountProfiler.instance = new RenderCountProfiler({ store: params.store }); } return RenderCountProfiler.instance; } }]); return RenderCountProfiler; }();