UNPKG

minska-react

Version:
143 lines (98 loc) 3.34 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = _interopDefault(require('react')); 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 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; }; const defaultMapStateToProps = state => state; const storeShape = { send: React.PropTypes.func.isRequired, subscribe: React.PropTypes.func.isRequired, unsubscribe: React.PropTypes.func.isRequired }; const Connect = (mapStateToProps = defaultMapStateToProps) => WrappedComponent => { var _class, _temp; return _temp = _class = class extends React.PureComponent { constructor(props, context) { super(props, context); const wrappedName = WrappedComponent.displayName || WrappedComponent.name || 'Component'; const 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); } componentWillMount() { this.store.subscribe('onChange', this.connectKey, this.onUpdate); } componentWillUnmount() { this.store.unsubscribe(this.connectKey); } onUpdate(state) { this.setState(_extends({}, mapStateToProps(state, this.props))); } send(...args) { this.store.send(...args); } render() { var _props = this.props; const store = _props.store, propsWithoutStore = objectWithoutProperties(_props, ['store']); return React.createElement(WrappedComponent, _extends({}, propsWithoutStore, this.state, { send: this.send })); } }, _class.contextTypes = { store: React.PropTypes.shape(storeShape) }, _class.propTypes = { store: React.PropTypes.shape(storeShape) }, _temp; }; class Provider extends React.PureComponent { constructor(...args) { var _temp; return _temp = super(...args), this.state = { store: this.props.store }, _temp; } getChildContext() { return { store: this.state.store }; } render() { return this.props.children; } } Provider.propTypes = { children: React.PropTypes.node.isRequired, store: React.PropTypes.object.isRequired }; Provider.childContextTypes = { store: React.PropTypes.object }; exports.connect = Connect; exports.Provider = Provider; //# sourceMappingURL=index.js.map