UNPKG

immutable-di-react

Version:

React bindings for immutable-di: simple, promise-based dependency injection container with some state handling functions

125 lines (92 loc) 5.36 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); exports['default'] = statefull; 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 _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; } var _react = require('react'); var _immutableDiDefine = require('immutable-di/define'); var _immutableDiAsserts = require('immutable-di/asserts'); var _immutableDiContainer = require('immutable-di/container'); var _immutableDiContainer2 = _interopRequireDefault(_immutableDiContainer); var _immutableDiUtilsGetFunctionName = require('immutable-di/utils/getFunctionName'); var _immutableDiUtilsGetFunctionName2 = _interopRequireDefault(_immutableDiUtilsGetFunctionName); var StatefullComponent = (function (_Component) { _inherits(StatefullComponent, _Component); _createClass(StatefullComponent, null, [{ key: 'contextTypes', value: { container: _react.PropTypes.instanceOf(_immutableDiContainer2['default']).isRequired }, enumerable: true }, { key: 'stateMap', value: {}, enumerable: true }]); function StatefullComponent(props, context) { _classCallCheck(this, StatefullComponent); _Component.call(this, props, context); function pass(state) { return state; } this.__setState = this.__setState.bind(this); var _constructor = this.constructor; var stateMap = _constructor.stateMap; var displayName = _constructor.displayName; var Getter = _immutableDiDefine.Facet(stateMap, displayName)(pass); this.__listener = _immutableDiDefine.Factory(stateMap, displayName)(this.__setState); this.state = _extends({}, props, context.container.get(Getter)); this.__isMounted = false; } StatefullComponent.prototype.__setState = function __setState(state) { if (!this.__isMounted) { throw new Error('setState invoked, but component is not mounted: ' + this.constructor.displayName); } else { this.setState(state); } }; StatefullComponent.prototype.componentWillReceiveProps = function componentWillReceiveProps(props) { this.__setState(props); }; StatefullComponent.prototype.componentDidMount = function componentDidMount() { this.__isMounted = true; this.context.container.mount(this.__listener); }; StatefullComponent.prototype.componentWillUnmount = function componentWillUnmount() { this.__isMounted = false; this.context.container.unmount(this.__listener); }; return StatefullComponent; })(_react.Component); function statefull() { var stateMap = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _immutableDiAsserts.IDeps(stateMap); return function wrapComponent(BaseComponent) { var dn = BaseComponent.displayName || _immutableDiUtilsGetFunctionName2['default'](BaseComponent); return (function (_StatefullComponent) { _inherits(StatefullComponentWrapper, _StatefullComponent); function StatefullComponentWrapper() { _classCallCheck(this, StatefullComponentWrapper); _StatefullComponent.apply(this, arguments); } StatefullComponentWrapper.prototype.render = function render() { return _react.createElement(BaseComponent, this.state); }; _createClass(StatefullComponentWrapper, null, [{ key: 'displayName', value: dn + '_statefull', enumerable: true }, { key: 'stateMap', value: stateMap, enumerable: true }]); return StatefullComponentWrapper; })(StatefullComponent); }; } module.exports = exports['default']; //# sourceMappingURL=statefull.js.map