1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const React = tslib_1.__importStar(require("react"));
|
5 | const shallowEqual_1 = require("./shallowEqual");
|
6 | const initialValue = Symbol.for('initialValue');
|
7 | class FromStream extends React.Component {
|
8 | constructor(props) {
|
9 | super(props);
|
10 | this.mutableMounted = false;
|
11 | this.state = {
|
12 | value: initialValue,
|
13 | };
|
14 | this.subscribe();
|
15 | this.mutableMounted = true;
|
16 | }
|
17 | componentWillUnmount() {
|
18 | this.mutableMounted = false;
|
19 | this.unsubscribe();
|
20 | }
|
21 | componentDidUpdate(prevProps) {
|
22 | const props = this.props;
|
23 | if ((prevProps.props != undefined && props.props == undefined) ||
|
24 | (prevProps.props == undefined && props.props != undefined) ||
|
25 | (prevProps.props != undefined &&
|
26 | props.props != undefined &&
|
27 | (prevProps.props.length !== props.props.length ||
|
28 | prevProps.props.some((propA, idx) => !shallowEqual_1.shallowEqual(propA, props.props[idx]))))) {
|
29 | this.subscribe();
|
30 | }
|
31 | }
|
32 | render() {
|
33 | const { value } = this.state;
|
34 | if (value === initialValue) {
|
35 | return null;
|
36 | }
|
37 | return this.props.children(value);
|
38 | }
|
39 | subscribe() {
|
40 | this.unsubscribe();
|
41 | let stateSet = false;
|
42 | this.mutableSubscription = this.props.createStream().subscribe({
|
43 | next: (value) => {
|
44 | stateSet = true;
|
45 | this._setValue(value);
|
46 | },
|
47 | });
|
48 | if (!stateSet) {
|
49 | this._setValue(initialValue);
|
50 | }
|
51 | }
|
52 | _setValue(value) {
|
53 | if (this.mutableMounted) {
|
54 | this.setState({ value });
|
55 | }
|
56 | else {
|
57 | this.state = { value };
|
58 | }
|
59 | }
|
60 | unsubscribe() {
|
61 | if (this.mutableSubscription !== undefined) {
|
62 | this.mutableSubscription.unsubscribe();
|
63 | this.mutableSubscription = undefined;
|
64 | }
|
65 | }
|
66 | }
|
67 | exports.FromStream = FromStream;
|
68 |
|
69 |
|