UNPKG

mini-store

Version:

[![Travis](https://img.shields.io/travis/yesmeck/mini-store.svg?style=flat-square)](https://travis-ci.org/yesmeck/mini-store)

107 lines (106 loc) 4.78 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import * as React from 'react'; import shallowEqual from 'shallowequal'; import hoistStatics from 'hoist-non-react-statics'; import { MiniStoreContext } from './Provider'; function getDisplayName(WrappedComponent) { return WrappedComponent.displayName || WrappedComponent.name || 'Component'; } var defaultMapStateToProps = function () { return ({}); }; export function connect(mapStateToProps, options) { if (options === void 0) { options = {}; } var shouldSubscribe = !!mapStateToProps; var finalMapStateToProps = mapStateToProps || defaultMapStateToProps; return function wrapWithConnect(WrappedComponent) { var Connect = /** @class */ (function (_super) { __extends(Connect, _super); function Connect(props, context) { var _this = _super.call(this, props, context) || this; _this.unsubscribe = null; _this.handleChange = function () { if (!_this.unsubscribe) { return; } var nextState = finalMapStateToProps(_this.store.getState(), _this.props); _this.setState({ subscribed: nextState }); }; _this.store = _this.context; _this.state = { subscribed: finalMapStateToProps(_this.store.getState(), props), store: _this.store, props: props, }; return _this; } Connect.getDerivedStateFromProps = function (props, prevState) { // using ownProps if (mapStateToProps && mapStateToProps.length === 2 && props !== prevState.props) { return { subscribed: finalMapStateToProps(prevState.store.getState(), props), props: props, }; } return { props: props }; }; Connect.prototype.componentDidMount = function () { this.trySubscribe(); }; Connect.prototype.componentWillUnmount = function () { this.tryUnsubscribe(); }; Connect.prototype.shouldComponentUpdate = function (nextProps, nextState) { return (!shallowEqual(this.props, nextProps) || !shallowEqual(this.state.subscribed, nextState.subscribed)); }; Connect.prototype.trySubscribe = function () { if (shouldSubscribe) { this.unsubscribe = this.store.subscribe(this.handleChange); this.handleChange(); } }; Connect.prototype.tryUnsubscribe = function () { if (this.unsubscribe) { this.unsubscribe(); this.unsubscribe = null; } }; Connect.prototype.render = function () { var props = __assign(__assign(__assign({}, this.props), this.state.subscribed), { store: this.store }); return React.createElement(WrappedComponent, __assign({}, props, { ref: this.props.miniStoreForwardedRef })); }; Connect.displayName = "Connect(" + getDisplayName(WrappedComponent) + ")"; Connect.contextType = MiniStoreContext; return Connect; }(React.Component)); if (options.forwardRef) { var forwarded = React.forwardRef(function (props, ref) { return React.createElement(Connect, __assign({}, props, { miniStoreForwardedRef: ref })); }); return hoistStatics(forwarded, WrappedComponent); } return hoistStatics(Connect, WrappedComponent); }; }