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 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.connect = void 0;
|
28 | var React = require("react");
|
29 | var rxjs_1 = require("rxjs");
|
30 | var provider_1 = require("./provider");
|
31 | var actions_1 = require("./actions");
|
32 | var operators_1 = require("rxjs/operators");
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function connect(ComponentToConnect, connectCallback) {
|
39 | var ConnectedComponent = (function (_super) {
|
40 | __extends(ConnectedComponent, _super);
|
41 | function ConnectedComponent(props) {
|
42 | var _this = _super.call(this, props) || this;
|
43 | _this.subscription = new rxjs_1.Subscription();
|
44 | _this.actionProps = {};
|
45 | _this.inputProps = new rxjs_1.ReplaySubject(1);
|
46 | _this.state = {
|
47 | connectedProps: undefined,
|
48 | ready: false,
|
49 | };
|
50 | _this.inputProps.next(_this.getProps());
|
51 | _this.subscription.add(function () { return _this.inputProps.complete(); });
|
52 | if (_this.props.reactiveStateStore) {
|
53 | _this.store = _this.props.reactiveStateStore.clone();
|
54 |
|
55 | _this.parentDestroyed = _this.props.reactiveStateStore.destroyed;
|
56 | }
|
57 | _this.connect();
|
58 | return _this;
|
59 | }
|
60 | ConnectedComponent.prototype.connect = function () {
|
61 | if (this.store === undefined)
|
62 | return;
|
63 | this.connectResult = connectCallback(this.store, this.inputProps.asObservable());
|
64 | if (this.connectResult.actionMap) {
|
65 | this.actionProps = actions_1.assembleActionProps(this.connectResult.actionMap);
|
66 | }
|
67 | };
|
68 | ConnectedComponent.prototype.subscribeToStateChanges = function () {
|
69 | var _this = this;
|
70 | if (this.store === undefined)
|
71 | return;
|
72 | var connectResult = this.connectResult;
|
73 | if (connectResult.props) {
|
74 | this.subscription.add(connectResult.props.pipe(operators_1.takeUntil(this.parentDestroyed)).subscribe(function (connectedProps) {
|
75 | _this.setState(function (prevState) {
|
76 | return __assign(__assign({}, prevState), { connectedProps: connectedProps, ready: true });
|
77 | });
|
78 | }));
|
79 | }
|
80 | else {
|
81 | this.setState(function (prevState) { return ({ ready: true }); });
|
82 | }
|
83 | };
|
84 | |
85 |
|
86 |
|
87 |
|
88 | ConnectedComponent.prototype.getProps = function () {
|
89 | var props = __assign({}, this.props);
|
90 | delete props.reactiveStateStore;
|
91 | return props;
|
92 | };
|
93 | ConnectedComponent.prototype.componentWillUnmount = function () {
|
94 | if (this.store !== undefined) {
|
95 | this.store.destroy();
|
96 | }
|
97 | this.subscription.unsubscribe();
|
98 | };
|
99 | ConnectedComponent.prototype.componentDidMount = function () {
|
100 | this.subscribeToStateChanges();
|
101 | };
|
102 | ConnectedComponent.prototype.componentDidUpdate = function (prevProps) {
|
103 | if (prevProps !== this.props) {
|
104 | this.inputProps.next(this.getProps());
|
105 | }
|
106 | };
|
107 | ConnectedComponent.prototype.render = function () {
|
108 | var props = this.getProps();
|
109 | if (this.store === undefined || this.state.ready === true) {
|
110 | return (React.createElement(ComponentToConnect, __assign({}, this.state.connectedProps, this.actionProps, props)));
|
111 | }
|
112 | else {
|
113 | return null;
|
114 | }
|
115 | };
|
116 | return ConnectedComponent;
|
117 | }(React.Component));
|
118 | return (function (_super) {
|
119 | __extends(class_1, _super);
|
120 | function class_1(props) {
|
121 | return _super.call(this, props) || this;
|
122 | }
|
123 | class_1.prototype.render = function () {
|
124 | var _this = this;
|
125 | return (React.createElement(provider_1.StoreConsumer, null, function (value) { return React.createElement(ConnectedComponent, __assign({ reactiveStateStore: value }, _this.props)); }));
|
126 | };
|
127 | return class_1;
|
128 | }(React.Component));
|
129 | }
|
130 | exports.connect = connect;
|
131 |
|
\ | No newline at end of file |