1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __importStar = (this && this.__importStar) || function (mod) {
|
27 | if (mod && mod.__esModule) return mod;
|
28 | var result = {};
|
29 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
30 | result["default"] = mod;
|
31 | return result;
|
32 | };
|
33 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
34 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
35 | };
|
36 | Object.defineProperty(exports, "__esModule", { value: true });
|
37 | var React = __importStar(require("react"));
|
38 | var shallowequal_1 = __importDefault(require("shallowequal"));
|
39 | var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics"));
|
40 | var Provider_1 = require("./Provider");
|
41 | function getDisplayName(WrappedComponent) {
|
42 | return WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
43 | }
|
44 | var defaultMapStateToProps = function () { return ({}); };
|
45 | function connect(mapStateToProps, options) {
|
46 | if (options === void 0) { options = {}; }
|
47 | var shouldSubscribe = !!mapStateToProps;
|
48 | var finalMapStateToProps = mapStateToProps || defaultMapStateToProps;
|
49 | return function wrapWithConnect(WrappedComponent) {
|
50 | var Connect = (function (_super) {
|
51 | __extends(Connect, _super);
|
52 | function Connect(props, context) {
|
53 | var _this = _super.call(this, props, context) || this;
|
54 | _this.unsubscribe = null;
|
55 | _this.handleChange = function () {
|
56 | if (!_this.unsubscribe) {
|
57 | return;
|
58 | }
|
59 | var nextState = finalMapStateToProps(_this.store.getState(), _this.props);
|
60 | _this.setState({ subscribed: nextState });
|
61 | };
|
62 | _this.store = _this.context;
|
63 | _this.state = {
|
64 | subscribed: finalMapStateToProps(_this.store.getState(), props),
|
65 | store: _this.store,
|
66 | props: props,
|
67 | };
|
68 | return _this;
|
69 | }
|
70 | Connect.getDerivedStateFromProps = function (props, prevState) {
|
71 |
|
72 | if (mapStateToProps && mapStateToProps.length === 2 && props !== prevState.props) {
|
73 | return {
|
74 | subscribed: finalMapStateToProps(prevState.store.getState(), props),
|
75 | props: props,
|
76 | };
|
77 | }
|
78 | return { props: props };
|
79 | };
|
80 | Connect.prototype.componentDidMount = function () {
|
81 | this.trySubscribe();
|
82 | };
|
83 | Connect.prototype.componentWillUnmount = function () {
|
84 | this.tryUnsubscribe();
|
85 | };
|
86 | Connect.prototype.shouldComponentUpdate = function (nextProps, nextState) {
|
87 | return (!shallowequal_1.default(this.props, nextProps) ||
|
88 | !shallowequal_1.default(this.state.subscribed, nextState.subscribed));
|
89 | };
|
90 | Connect.prototype.trySubscribe = function () {
|
91 | if (shouldSubscribe) {
|
92 | this.unsubscribe = this.store.subscribe(this.handleChange);
|
93 | this.handleChange();
|
94 | }
|
95 | };
|
96 | Connect.prototype.tryUnsubscribe = function () {
|
97 | if (this.unsubscribe) {
|
98 | this.unsubscribe();
|
99 | this.unsubscribe = null;
|
100 | }
|
101 | };
|
102 | Connect.prototype.render = function () {
|
103 | var props = __assign(__assign(__assign({}, this.props), this.state.subscribed), { store: this.store });
|
104 | return React.createElement(WrappedComponent, __assign({}, props, { ref: this.props.miniStoreForwardedRef }));
|
105 | };
|
106 | Connect.displayName = "Connect(" + getDisplayName(WrappedComponent) + ")";
|
107 | Connect.contextType = Provider_1.MiniStoreContext;
|
108 | return Connect;
|
109 | }(React.Component));
|
110 | if (options.forwardRef) {
|
111 | var forwarded = React.forwardRef(function (props, ref) {
|
112 | return React.createElement(Connect, __assign({}, props, { miniStoreForwardedRef: ref }));
|
113 | });
|
114 | return hoist_non_react_statics_1.default(forwarded, WrappedComponent);
|
115 | }
|
116 | return hoist_non_react_statics_1.default(Connect, WrappedComponent);
|
117 | };
|
118 | }
|
119 | exports.connect = connect;
|