UNPKG

minska-react

Version:
233 lines (176 loc) 6.69 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) : typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) : (factory((global.MinskaReact = global.MinskaReact || {}),global.React)); }(this, (function (exports,React) { 'use strict'; React = 'default' in React ? React['default'] : React; var classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; 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; }; }(); 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 inherits = function (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 objectWithoutProperties = function (obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }; var possibleConstructorReturn = function (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; }; var defaultMapStateToProps = function defaultMapStateToProps(state) { return state; }; var storeShape = { send: React.PropTypes.func.isRequired, subscribe: React.PropTypes.func.isRequired, unsubscribe: React.PropTypes.func.isRequired }; var Connect = function Connect() { var mapStateToProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultMapStateToProps; return function (WrappedComponent) { var _class, _temp; return _temp = _class = function (_React$PureComponent) { inherits(_class, _React$PureComponent); function _class(props, context) { classCallCheck(this, _class); var _this = possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, props, context)); var wrappedName = WrappedComponent.displayName || WrappedComponent.name || 'Component'; var displayName = 'connect(' + wrappedName + ')'; _this.connectKey = displayName + ':' + Math.random().toString(36).substr(2, 8); _this.store = context.store || props.store; if (!_this.store) { throw new Error('Could not find \'store\' in either the context or props of component \'' + displayName + '\'. Either wrap the root component in <Provider>, or explicitly pass \'store\' as a prop to \'' + displayName + '\'.'); } _this.state = _extends({}, mapStateToProps(_this.store.state, props)); _this.onUpdate = _this.onUpdate.bind(_this); _this.send = _this.send.bind(_this); return _this; } createClass(_class, [{ key: 'componentWillMount', value: function componentWillMount() { this.store.subscribe('onChange', this.connectKey, this.onUpdate); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.store.unsubscribe(this.connectKey); } }, { key: 'onUpdate', value: function onUpdate(state) { this.setState(_extends({}, mapStateToProps(state, this.props))); } }, { key: 'send', value: function send() { var _store; (_store = this.store).send.apply(_store, arguments); } }, { key: 'render', value: function render() { var _props = this.props, store = _props.store, propsWithoutStore = objectWithoutProperties(_props, ['store']); return React.createElement(WrappedComponent, _extends({}, propsWithoutStore, this.state, { send: this.send })); } }]); return _class; }(React.PureComponent), _class.contextTypes = { store: React.PropTypes.shape(storeShape) }, _class.propTypes = { store: React.PropTypes.shape(storeShape) }, _temp; }; }; var Provider = function (_React$PureComponent) { inherits(Provider, _React$PureComponent); function Provider() { var _ref; var _temp, _this, _ret; classCallCheck(this, Provider); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = possibleConstructorReturn(this, (_ref = Provider.__proto__ || Object.getPrototypeOf(Provider)).call.apply(_ref, [this].concat(args))), _this), _this.state = { store: _this.props.store }, _temp), possibleConstructorReturn(_this, _ret); } createClass(Provider, [{ key: 'getChildContext', value: function getChildContext() { return { store: this.state.store }; } }, { key: 'render', value: function render() { return this.props.children; } }]); return Provider; }(React.PureComponent); Provider.propTypes = { children: React.PropTypes.node.isRequired, store: React.PropTypes.object.isRequired }; Provider.childContextTypes = { store: React.PropTypes.object }; exports.connect = Connect; exports.Provider = Provider; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=minska-react.js.map