UNPKG

5.75 kBJavaScriptView Raw
1"use strict";
2var __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})();
15var __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};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.connect = void 0;
28var React = require("react");
29var rxjs_1 = require("rxjs");
30var provider_1 = require("./provider");
31var actions_1 = require("./actions");
32var operators_1 = require("rxjs/operators");
33/**
34 * Connects a Component's props to a set of props of the application state coming from a Store object.
35 */
36// TODO: earlier TS version could infer TOriginalProps, why is this not working anymore? Bug in TS?
37// possible candidate: https://github.com/Microsoft/TypeScript/issues/21734
38function connect(ComponentToConnect, connectCallback) {
39 var ConnectedComponent = /** @class */ (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 // TODO this hack is necesseary because we seem to have a bug in the destroy logic for clones
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 * We need to remove the remoteReacticeState properties from our input props; the remainder input props
86 * are passed down to the connected component
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 /** @class */ (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}
130exports.connect = connect;
131//# sourceMappingURL=connect.js.map
\No newline at end of file