1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var rxjs_1 = require("rxjs");
|
5 | var operators_1 = require("rxjs/operators");
|
6 | var common_1 = require("./common");
|
7 | var Connector_1 = require("./components/Connector");
|
8 | function connect(component, options) {
|
9 | if (options === void 0) { options = {}; }
|
10 | var autoRender = options.autoRender === undefined ? true : options.autoRender;
|
11 | var ConnectWrapper = (function (_super) {
|
12 | tslib_1.__extends(ConnectWrapper, _super);
|
13 | function ConnectWrapper() {
|
14 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
15 | _this.state = {};
|
16 | _this.unmounted$ = new rxjs_1.Subject();
|
17 | _this.init = function (e) {
|
18 | _this.loaded = e;
|
19 | };
|
20 | return _this;
|
21 | }
|
22 | Object.defineProperty(ConnectWrapper.prototype, "store", {
|
23 | get: function () {
|
24 | if (this.props.store) {
|
25 | return this.props.store;
|
26 | }
|
27 | var store = this.loaded && this.loaded.store;
|
28 | var path = options.path;
|
29 | var result = path ? store.get(path) : store;
|
30 | return common_1.R.is(Object, result) ? result : undefined;
|
31 | },
|
32 | enumerable: true,
|
33 | configurable: true
|
34 | });
|
35 | ConnectWrapper.prototype.componentDidMount = function () {
|
36 | var _this = this;
|
37 | var store = this.store;
|
38 | this.setState({ store: store });
|
39 | if (store && autoRender) {
|
40 | store.state$.pipe(operators_1.takeUntil(this.unmounted$)).subscribe(function () {
|
41 | try {
|
42 | _this.forceUpdate();
|
43 | }
|
44 | catch (error) {
|
45 | _this.unmounted$.next();
|
46 | }
|
47 | });
|
48 | }
|
49 | };
|
50 | ConnectWrapper.prototype.componentWillUnmount = function () {
|
51 | this.unmounted$.next();
|
52 | };
|
53 | ConnectWrapper.prototype.render = function () {
|
54 | return (common_1.React.createElement(Connector_1.Connector, { onLoaded: this.init }, this.renderComponent()));
|
55 | };
|
56 | ConnectWrapper.prototype.renderComponent = function () {
|
57 | var store = this.state.store;
|
58 | if (!store) {
|
59 | return '';
|
60 | }
|
61 | var props = tslib_1.__assign({}, this.props, { store: store });
|
62 | return common_1.React.createElement(component, props);
|
63 | };
|
64 | return ConnectWrapper;
|
65 | }(common_1.React.PureComponent));
|
66 | return ConnectWrapper;
|
67 | }
|
68 | exports.connect = connect;
|
69 |
|
\ | No newline at end of file |