UNPKG

react-component-decorators

Version:
141 lines (110 loc) 4.62 kB
'use strict'; exports.__esModule = true; exports.shouldUpdateFor = exports.shallowEqual = undefined; var _autobind = require('core-decorators/lib/autobind'); var _autobind2 = _interopRequireDefault(_autobind); var _shallowEqual = require('fbjs/lib/shallowEqual'); var _shallowEqual2 = _interopRequireDefault(_shallowEqual); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object['ke' + 'ys'](descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object['define' + 'Property'](target, property, desc); desc = null; } return desc; } /** * @TODO: * - [ ] method decorator * - [ ] optimize loops * * { * state: ['eh'] * props: { * eh: cb, * ha: undefined, * } * } */ function shouldUpdateFor(options) { var conditions = options; if (Array.isArray(options)) { conditions = { props: options }; } var properties = Object.keys(conditions); var keys = Object.values(conditions); // set up our initial values var previousValueFor = {}; properties.forEach(function (prop) { previousValueFor[prop] = {}; keys.forEach(function (key) { previousValueFor[prop][key] = undefined; }); }); // class decorator function extendTarget(target) { var _desc, _value, _class; return _class = function (_target) { _inherits(ShouldUpdateFor, _target); function ShouldUpdateFor() { var _temp, _this, _ret; _classCallCheck(this, ShouldUpdateFor); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _target.call.apply(_target, [this].concat(args))), _this), _this.previousValueFor = previousValueFor, _temp), _possibleConstructorReturn(_this, _ret); } ShouldUpdateFor.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps) { var _this2 = this; var eq = false; properties.forEach(function (prop) { return keys.forEach(function (key) { // variables var prev, current; if (prop === 'props') { prev = _this2.props[key]; current = nextProps[key]; } else { prev = _this2.previousValueFor[prop][key]; } // comparisons if (prev === _this2[prop][key]) { eq = true; } else if ((0, _shallowEqual2.default)(prev, current)) { eq = true; } else { eq = false; } }); }); return eq; }; return ShouldUpdateFor; }(target), (_applyDecoratedDescriptor(_class.prototype, 'shouldComponentUpdate', [_autobind2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'shouldComponentUpdate'), _class.prototype)), _class; } return extendTarget; } exports.default = shouldUpdateFor; exports.shallowEqual = _shallowEqual2.default; exports.shouldUpdateFor = shouldUpdateFor;