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