ppx
Version:
ppx flux redux react
132 lines (98 loc) • 4.85 kB
JavaScript
;
exports.__esModule = true;
exports.default = undefined;
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; };
var _class, _temp; /**
* Created by tianwu on 2017/3/17.
*/
var _store2 = require('./store');
var _store3 = _interopRequireDefault(_store2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } 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; }
/**
* 目前只包了react版本
*/
var Provider = (_temp = _class = function (_React$Component) {
_inherits(Provider, _React$Component);
function Provider(props, context) {
_classCallCheck(this, Provider);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
var models = props.models,
force = props.force;
if (models) {
_this.models = models instanceof Array ? models : [models];
_this.namespace = _store3.default.model(_this.models, force);
}
_this.subscribe();
_this.state = _this.filterState();
return _this;
}
Provider.prototype.getChildContext = function getChildContext() {
var dispatch = _store3.default.dispatch;
return {
dispatch: dispatch.bind(_store3.default)
};
};
Provider.prototype.filterState = function filterState() {
var _store = _store3.default._store;
var filterStore = _extends({}, _store);
if (this.namespace) {
this.namespace.map(function (modelKey) {
if (_store[modelKey]) {
filterStore[modelKey] = _store[modelKey];
}
});
}
// 过滤state;
var state = {};
for (var i in filterStore) {
state[i] = filterStore[i].state;
}
return state;
};
/**
* 监听store 变化
*/
Provider.prototype.subscribe = function subscribe() {
var _this2 = this;
this.unSubscribe = _store3.default.subscribe(function (action, data) {
if (_this2.namespace) {
// 只对关注的models监听
var _action$type$split = action.type.split('/'),
namespace = _action$type$split[0],
reduce = _action$type$split[1];
if (_this2.namespace.indexOf(namespace) > -1) {
_this2.setState(data);
}
return;
}
// 空关注全监听
if (data) {
_this2.setState(data);
}
});
};
Provider.prototype.componentWillUnmount = function componentWillUnmount() {
this.unSubscribe();
};
Provider.prototype.render = function render() {
var _props = this.props,
children = _props.children,
others = _objectWithoutProperties(_props, ['children']);
var state = _objectWithoutProperties(this.state, []);
var dispatch = _store3.default.dispatch;
return _react2.default.cloneElement(children, _extends({}, others, { dispatch: dispatch.bind(_store3.default) }, state));
};
return Provider;
}(_react2.default.Component), _class.childContextTypes = {
dispatch: _propTypes2.default.func
}, _temp);
exports.default = Provider;
module.exports = exports['default'];