rxact-react
Version:
React bindings for Rxact
173 lines (134 loc) • 6.51 kB
JavaScript
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import hoistStatics from 'hoist-non-react-statics';
import React, { Component } from 'react';
import { getObservable, isObservable } from 'rxact';
import noop from './utils/noop';
import isFunction from './utils/isFunction';
var setDisplayName = function setDisplayName(component) {
var displayName = component.displayName || component.name || 'Component';
return 'RxactObserver(' + displayName + ')';
};
var defaultMergeProps = function defaultMergeProps(state, props) {
return _extends({}, props, state);
};
var createObserver = function createObserver(state$) {
var Observable = getObservable();
if (!isObservable(state$)) {
throw new Error('Expect state$ to be instance of Observable');
}
var decorator = function decorator(mapStateToProps) {
var mergeProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultMergeProps;
return function (WrappedComponent) {
var Observer = function (_Component) {
_inherits(Observer, _Component);
function Observer(props) {
_classCallCheck(this, Observer);
var _this = _possibleConstructorReturn(this, (Observer.__proto__ || Object.getPrototypeOf(Observer)).call(this, props));
_this.subscription = null;
_this.streamSubscription = null;
_this.propSubscription = null;
_this.setProps = noop;
_this.props$ = noop;
_this.state = {
component: null
};
_this.component = null;
_this.initialSubscriptions = function () {
var stream$ = state$;
var vdom$ = new Observable(function (observer) {
var readyCount = 0;
var streamState = void 0;
var propState = void 0;
var onSubscribe = function onSubscribe() {
if (readyCount < 2) {
readyCount += 1;
}
if (readyCount > 1) {
var nextState = streamState;
if (typeof mapStateToProps === 'function') {
nextState = mapStateToProps(streamState, propState);
}
var componentProps = mergeProps(nextState, propState);
var nextVdom = React.createElement(WrappedComponent, componentProps);
observer.next(nextVdom);
}
};
_this.propSubscription = _this.props$.subscribe(function (state) {
propState = state;
onSubscribe();
});
_this.streamSubscription = stream$.subscribe(function (state) {
streamState = state;
onSubscribe();
});
return {
unsubscribe: function unsubscribe() {}
};
});
_this.subscription = vdom$.subscribe(function (component) {
_this.component = component;
if (_this.subscription) {
_this.forceUpdate();
}
});
};
_this.props$ = new Observable(function (observer) {
_this.setProps = function (props) {
return observer.next(props);
};
_this.setProps(_this.props);
return {
unsubscribe: function unsubscribe() {
_this.setProps = noop;
}
};
});
_this.initialSubscriptions();
return _this;
}
_createClass(Observer, [{
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.subscription) {
this.subscription = null;
}
var streamSub = this.streamSubscription;
if (streamSub && isFunction(streamSub.unsubscribe)) {
streamSub.unsubscribe();
}
var propSub = this.propSubscription;
if (propSub && isFunction(propSub.unsubscribe)) {
propSub.unsubscribe();
}
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.setProps(nextProps);
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate() {
return false;
}
}, {
key: 'render',
value: function render() {
return this.component;
}
}]);
return Observer;
}(Component);
Observer.displayName = setDisplayName(WrappedComponent);
return hoistStatics(Observer, WrappedComponent);
};
};
return {
decorator: decorator
};
};
export default createObserver;