Skip to content

Commit

Permalink
fix(obrserver): Fix problem with observers
Browse files Browse the repository at this point in the history
  • Loading branch information
Thram committed Feb 20, 2017
1 parent 2499ac9 commit ef28c46
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 60 deletions.
4 changes: 3 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"presets": [
"es2015"
"es2015",
"react",
"stage-0"
]
}
35 changes: 14 additions & 21 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
});
exports.connect = undefined;

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 _forEach = require("lodash/forEach");

var _forEach2 = _interopRequireDefault(_forEach);
Expand Down Expand Up @@ -42,31 +40,26 @@ var connect = exports.connect = function connect(stateKey, ReactComponent) {
var _this = _possibleConstructorReturn(this, (ThruxComponent.__proto__ || Object.getPrototypeOf(ThruxComponent)).call(this, props));

_this.observers = {};
_this.state = (0, _assign2.default)(_this.state || {}, (0, _thrux.state)([].concat(stateKey)));
(0, _isArray2.default)(stateKey) ? (0, _forEach2.default)(stateKey, _this.addObserver) : _this.addObserver(stateKey);
return _this;
}

_createClass(ThruxComponent, [{
key: "addObserver",
value: function addObserver(key) {
var _this2 = this;

this.observers[key] = function (state) {
_this.addObserver = function (key) {
_this.observers[key] = function (state) {
var newState = {};
newState[key] = state;
_this2.setState(newState);
_this.setState(newState);
};
(0, _thrux.observe)(key, this.observers[key]);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
(0, _forEach2.default)(this.observers, function (observer, key) {
(0, _thrux.observe)(key, _this.observers[key]);
};

_this.componentWillUnmount = function () {
return (0, _forEach2.default)(_this.observers, function (observer, key) {
return (0, _thrux.removeObserver)(key, observer);
});
}
}]);
};

_this.state = (0, _assign2.default)(_this.state || {}, (0, _thrux.state)([].concat(stateKey)));
(0, _isArray2.default)(stateKey) ? (0, _forEach2.default)(stateKey, _this.addObserver) : _this.addObserver(stateKey);
return _this;
}

return ThruxComponent;
}(ReactComponent);
Expand Down
35 changes: 14 additions & 21 deletions dist/react-thrux.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/react-thrux.umd.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/react-thrux.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react-thrux.umd.min.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"babel-jest": "^18.0.0",
"babel-loader": "^6.3.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"babel-preset-stage-0": "^6.22.0",
"babel-register": "^6.23.0",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^1.2.0",
Expand Down
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {observe, state, removeObserver} from "thrux";
export const connect = (stateKey, ReactComponent) => {
class ThruxComponent extends ReactComponent {

observers = {};

constructor(props) {
super(props);
this.observers = {};
this.state = assign(this.state || {}, state([].concat(stateKey)));
this.state = assign(this.state || {}, state([].concat(stateKey)));
isArray(stateKey) ? forEach(stateKey, this.addObserver) : this.addObserver(stateKey);
}

addObserver(key) {
addObserver = (key) => {
this.observers[key] = (state) => {
let newState = {};
newState[key] = state;
Expand All @@ -25,9 +26,7 @@ export const connect = (stateKey, ReactComponent) => {
observe(key, this.observers[key]);
};

componentWillUnmount() {
forEach(this.observers, (observer, key) => removeObserver(key, observer))
}
componentWillUnmount = () => forEach(this.observers, (observer, key) => removeObserver(key, observer));
}
return ThruxComponent;
};
Loading

0 comments on commit ef28c46

Please sign in to comment.