Skip to content

Instantly share code, notes, and snippets.

@ahgood
Created April 7, 2017 01:40
Show Gist options
  • Save ahgood/ceb70a580cc5e54407fbbea36841ca25 to your computer and use it in GitHub Desktop.
Save ahgood/ceb70a580cc5e54407fbbea36841ca25 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/najewe
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
<script id="jsbin-javascript">
"use strict";
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; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var User = (function () {
function User() {
_classCallCheck(this, User);
this.state = { score: 0 };
}
_createClass(User, [{
key: "setState",
//fake setState
value: function setState(state, callback) {
console.log("state", state);
this.state = Object.assign({}, this.state, state);
if (callback) callback();
}
}]);
return User;
})();
var Justice = new User();
var updateQueue = [function (state) {
return { score: state.score + 1 };
}, function (state) {
return { score: state.score + 1 };
}, function (state) {
return { score: state.score + 1 };
}];
// recursively update the state in the order
function updateState(component, updateQueue) {
if (updateQueue.length === 1) {
return component.setState(updateQueue[0](component.state));
}
return component.setState(updateQueue[0](component.state), function () {
return updateState(component, updateQueue.slice(1));
});
}
updateState(Justice, updateQueue);
</script>
<script id="jsbin-source-javascript" type="text/javascript">
class User{
state = {score : 0};
//fake setState
setState(state, callback) {
console.log("state", state);
this.state = Object.assign({}, this.state, state);
if (callback) callback();
}
}
const Justice = new User();
const updateQueue = [
(state) => ({score : state.score + 1}),
(state) => ({score : state.score + 1}),
(state) => ({score : state.score + 1})
];
// recursively update the state in the order
function updateState(component, updateQueue) {
if (updateQueue.length === 1) {
return component.setState(updateQueue[0](component.state));
}
return component.setState(
updateQueue[0](component.state),
() =>
updateState( component, updateQueue.slice(1))
);
}
updateState(Justice, updateQueue);</script></body>
</html>
"use strict";
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; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var User = (function () {
function User() {
_classCallCheck(this, User);
this.state = { score: 0 };
}
_createClass(User, [{
key: "setState",
//fake setState
value: function setState(state, callback) {
console.log("state", state);
this.state = Object.assign({}, this.state, state);
if (callback) callback();
}
}]);
return User;
})();
var Justice = new User();
var updateQueue = [function (state) {
return { score: state.score + 1 };
}, function (state) {
return { score: state.score + 1 };
}, function (state) {
return { score: state.score + 1 };
}];
// recursively update the state in the order
function updateState(component, updateQueue) {
if (updateQueue.length === 1) {
return component.setState(updateQueue[0](component.state));
}
return component.setState(updateQueue[0](component.state), function () {
return updateState(component, updateQueue.slice(1));
});
}
updateState(Justice, updateQueue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment