Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"presets": ["es2015", "stage-0", "react"],
"plugins": ["transform-decorators-legacy"],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties",
["transform-es2015-classes", {loose: true}],
],
"env": {
"development": {
"presets": ["react-hmre"]
}
}
}
}
133 changes: 61 additions & 72 deletions lib/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Object.defineProperty(exports, "__esModule", {

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 _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; }; }();

exports.default = local;

var _react = require('react');
Expand Down Expand Up @@ -99,8 +97,6 @@ function local() {
_inherits(ReduxReactLocal, _Component);

function ReduxReactLocal() {
var _Object$getPrototypeO;

var _temp, _this, _ret;

_classCallCheck(this, ReduxReactLocal);
Expand All @@ -109,7 +105,7 @@ function local() {
args[_key] = arguments[_key];
}

return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(ReduxReactLocal)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.store = _this.context.store, _this.state = function () {
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.store = _this.context.store, _this.state = function () {
var id = getId(_this.props),
storeState = _this.store.getState();

Expand All @@ -135,84 +131,77 @@ function local() {
}, _temp), _possibleConstructorReturn(_this, _ret);
}

_createClass(ReduxReactLocal, [{
key: 'componentWillMount',
value: function componentWillMount() {
var _this2 = this;
ReduxReactLocal.prototype.componentWillMount = function componentWillMount() {
var _this2 = this;

this.store.dispatch({
type: '$$local.register',
payload: {
ident: this.state.id,
initial: this.state.value,
reducer: reducer,
persist: persist
}
});
if (isBrowserLike) {
this.dispose = this.context.$$local(this.state.id, function (value) {
_this2.setState({ value: value });
});
}
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(this.props, nextProps) || this.state.id !== nextState.id || this.state.value !== nextState.value;
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(next) {
var id = getId(next);

if (id !== this.state.id) {
var init = getInitial(next);
this.store.dispatch({
type: '$$local.swap',
payload: {
ident: this.state.id,
next: id,
initial: init,
reducer: reducer,
persist: persist
}
});

this.setState({
id: id,
value: whenUndefined(this.store.getState().local.get(id), init)
});
this.store.dispatch({
type: '$$local.register',
payload: {
ident: this.state.id,
initial: this.state.value,
reducer: reducer,
persist: persist
}
});
if (isBrowserLike) {
this.dispose = this.context.$$local(this.state.id, function (value) {
_this2.setState({ value: value });
});
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
};

ReduxReactLocal.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(this.props, nextProps) || this.state.id !== nextState.id || this.state.value !== nextState.value;
};

ReduxReactLocal.prototype.componentWillReceiveProps = function componentWillReceiveProps(next) {
var id = getId(next);

if (id !== this.state.id) {
var init = getInitial(next);
this.store.dispatch({
type: '$$local.unmount',
type: '$$local.swap',
payload: {
ident: this.state.id,
next: id,
initial: init,
reducer: reducer,
persist: persist
}
});
if (this.dispose) {
this.dispose();
}

this.setState({
id: id,
value: whenUndefined(this.store.getState().local.get(id), init)
});
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(
Target,
_extends({}, this.props, {
$: this.$,
ident: this.state.id,
dispatch: this.store.dispatch,
state: this.state.value,
setState: this._setState }),
this.props.children
);
};

ReduxReactLocal.prototype.componentWillUnmount = function componentWillUnmount() {
this.store.dispatch({
type: '$$local.unmount',
payload: {
ident: this.state.id,
persist: persist
}
});
if (this.dispose) {
this.dispose();
}
}]);
};

ReduxReactLocal.prototype.render = function render() {
return _react2.default.createElement(
Target,
_extends({}, this.props, {
$: this.$,
ident: this.state.id,
dispatch: this.store.dispatch,
state: this.state.value,
setState: this._setState }),
this.props.children
);
};

return ReduxReactLocal;
}(_react.Component), _class.contextTypes = {
Expand Down
84 changes: 37 additions & 47 deletions lib/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ exports.default = undefined;

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

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 _class, _temp2;

var _react = require('react');
Expand All @@ -33,8 +31,6 @@ var Root = (_temp2 = _class = function (_Component) {
_inherits(Root, _Component);

function Root() {
var _Object$getPrototypeO;

var _temp, _this, _ret;

_classCallCheck(this, Root);
Expand All @@ -43,7 +39,7 @@ var Root = (_temp2 = _class = function (_Component) {
args[_key] = arguments[_key];
}

return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(Root)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.fns = {}, _this._local = function (ident, fn) {
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.fns = {}, _this._local = function (ident, fn) {
_this.fns[ident] = [].concat(_toConsumableArray(_this.fns[ident] || []), [fn]);
return function () {
return _this.fns[ident] = _this.fns[ident].filter(function (x) {
Expand All @@ -53,52 +49,46 @@ var Root = (_temp2 = _class = function (_Component) {
}, _temp), _possibleConstructorReturn(_this, _ret);
}

_createClass(Root, [{
key: 'getChildContext',
value: function getChildContext() {
return {
$$local: this._local
};
}
}, {
key: 'componentWillMount',
value: function componentWillMount() {
var _this2 = this;

if (isBrowserLike) {
this.dispose = this.context.store.subscribe(function () {
var state = _this2.context.store.getState().local,
changed = false;
T.entries(state.$$changed).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2);

var key = _ref2[0];
var value = _ref2[1];

changed = true;
(_this2.fns[key] || []).forEach(function (fn) {
return fn(value);
});
Root.prototype.getChildContext = function getChildContext() {
return {
$$local: this._local
};
};

Root.prototype.componentWillMount = function componentWillMount() {
var _this2 = this;

if (isBrowserLike) {
this.dispose = this.context.store.subscribe(function () {
var state = _this2.context.store.getState().local,
changed = false;
T.entries(state.$$changed).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2);

var key = _ref2[0];
var value = _ref2[1];

changed = true;
(_this2.fns[key] || []).forEach(function (fn) {
return fn(value);
});
if (changed) {
_this2.context.store.dispatch({ type: '$$local.flushed' });
}
});
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.dispose) {
this.dispose();
}
if (changed) {
_this2.context.store.dispatch({ type: '$$local.flushed' });
}
});
}
}, {
key: 'render',
value: function render() {
return this.props.children;
};

Root.prototype.componentWillUnmount = function componentWillUnmount() {
if (this.dispose) {
this.dispose();
}
}]);
};

Root.prototype.render = function render() {
return this.props.children;
};

return Root;
}(_react.Component), _class.contextTypes = {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"babel-core": "^6.5.2",
"babel-eslint": "^5.0.0",
"babel-loader": "^6.2.3",
"babel-plugin-transform-class-properties": "^6.9.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-classes": "^6.9.0",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.0",
Expand Down