Comparing version 0.1.2 to 0.2.0
@@ -87,14 +87,14 @@ require("source-map-support").install(); | ||
var _errors = __webpack_require__(7); | ||
var _channelManager = __webpack_require__(6); | ||
var _channelManager2 = _interopRequireDefault(_channelManager); | ||
var _errors = __webpack_require__(8); | ||
var ERROR = _interopRequireWildcard(_errors); | ||
var _statuses = __webpack_require__(5); | ||
var _statuses = __webpack_require__(4); | ||
var STATUS = _interopRequireWildcard(_statuses); | ||
var _Relation = __webpack_require__(4); | ||
var Relation = _interopRequireWildcard(_Relation); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
@@ -106,2 +106,4 @@ | ||
var DEFAULT_CHANNEL = "default"; | ||
var Component = function () { | ||
@@ -114,6 +116,7 @@ function Component(componentFunction) { | ||
} | ||
this.connectedChildrenComponents = []; | ||
this.connectedParentComponents = []; | ||
this.connectedParentChannels = []; | ||
this.status = STATUS.INIT; | ||
this.channelManager = new _channelManager2.default(); | ||
this.channelManager.createChannel(DEFAULT_CHANNEL); | ||
this.finalComponentFlag = false; | ||
@@ -149,3 +152,3 @@ this.rootComponent = new _root2.default(); | ||
if (typeof this.componentFunction === "function") { | ||
this.status = STATUS.PROCESS; | ||
this.channelManager.setStatusProcess(); | ||
this.componentFunction(request, this._getResponseObject()); | ||
@@ -165,6 +168,18 @@ } else { | ||
value: function _onParentReady() { | ||
if (Relation.hasComponentsStatus(this.connectedParentComponents, STATUS.DONE)) { | ||
if (this._isParentChannelsDone()) { | ||
this._runComponentFunction(this._getParentsOutput()); | ||
} | ||
} | ||
}, { | ||
key: "_isParentChannelsDone", | ||
value: function _isParentChannelsDone() { | ||
var doneCount = this.connectedParentChannels.reduce(function (doneCount, channel) { | ||
if (channel.getStatus() === STATUS.DONE) { | ||
return ++doneCount; | ||
} else { | ||
return doneCount; | ||
} | ||
}, 0); | ||
return doneCount === this.connectedParentChannels.length; | ||
} | ||
@@ -178,7 +193,7 @@ /** | ||
value: function _getParentsOutput() { | ||
if (this.connectedParentComponents.length === 1) { | ||
return { input: this.connectedParentComponents[0].output }; | ||
if (this.connectedParentChannels.length === 1) { | ||
return { input: this.connectedParentChannels[0].output }; | ||
} else { | ||
return { input: this.connectedParentComponents.map(function (component) { | ||
return component.output; | ||
return { input: this.connectedParentChannels.map(function (channel) { | ||
return channel.output; | ||
}) }; | ||
@@ -199,11 +214,12 @@ } | ||
return { | ||
send: function send(output) { | ||
send: function send(output, channelName) { | ||
channelName = channelName || DEFAULT_CHANNEL; | ||
var channel = _this2.getChannel(channelName); | ||
if (_this2.rootComponent.status === STATUS.PROCESS) { | ||
_this2.status = STATUS.DONE; | ||
_this2.output = output; | ||
_this2.channelManager.setStatusDone(channel); | ||
channel.output = output; | ||
if (_this2.finalComponentFlag === false) { | ||
_this2.connectedChildrenComponents.forEach(function (component) { | ||
channel.getComponentList().forEach(function (component) { | ||
return component._onParentReady(); | ||
}); | ||
_this2.rootComponent.onAnyDone(); | ||
} else { | ||
@@ -216,4 +232,2 @@ _this2.rootComponent.finish(output); | ||
if (_this2.rootComponent.status === STATUS.PROCESS) { | ||
_this2.status = STATUS.DONE; | ||
_this2.output = output; | ||
_this2.rootComponent.finish(output); | ||
@@ -224,2 +238,7 @@ } | ||
} | ||
}, { | ||
key: "getChannel", | ||
value: function getChannel(channelName) { | ||
return this.channelManager.getChannel(channelName); | ||
} | ||
@@ -238,4 +257,5 @@ /** | ||
/** | ||
* child component add parent component | ||
* bind parent component with this component | ||
* @param component parent component | ||
* @param channel parent channel name | ||
*/ | ||
@@ -245,19 +265,10 @@ | ||
key: "bind", | ||
value: function bind(component) { | ||
this.connectedParentComponents.push(component); | ||
component._bindChild(this); | ||
value: function bind(component, channelName) { | ||
channelName = channelName || DEFAULT_CHANNEL; | ||
var parentChannel = component.getChannel(channelName); | ||
this.connectedParentChannels.push(parentChannel); | ||
parentChannel.addComponent(this); | ||
component.rootComponent.merge(this.rootComponent); | ||
this.rootComponent = component.rootComponent; | ||
} | ||
/** | ||
* Allow parent component to add child component, should be triggered only by this.connect | ||
* @param component child component | ||
*/ | ||
}, { | ||
key: "_bindChild", | ||
value: function _bindChild(component) { | ||
this.connectedChildrenComponents.push(component); | ||
this.rootComponent.merge(component.rootComponent); | ||
component.rootComponent = this.rootComponent; | ||
} | ||
}]); | ||
@@ -282,11 +293,7 @@ | ||
var _Relation = __webpack_require__(4); | ||
var _statuses = __webpack_require__(4); | ||
var Relation = _interopRequireWildcard(_Relation); | ||
var _statuses = __webpack_require__(5); | ||
var STATUS = _interopRequireWildcard(_statuses); | ||
var _bluebird = __webpack_require__(6); | ||
var _bluebird = __webpack_require__(5); | ||
@@ -309,20 +316,3 @@ var _bluebird2 = _interopRequireDefault(_bluebird); | ||
/** | ||
* root component function, it is triggered by any child component after finish | ||
*/ | ||
_createClass(Root, [{ | ||
key: "onAnyDone", | ||
value: function onAnyDone() { | ||
/** | ||
* if all components are done then finish promise | ||
* | ||
* Now chain is finished when component run method finish | ||
*/ | ||
//if(Relation.hasComponentsStatus(this.components, [STATUS.DONE, STATUS.INIT])) { | ||
// this.finish(); | ||
//} | ||
} | ||
}, { | ||
key: "addComponent", | ||
@@ -377,2 +367,21 @@ value: function addComponent(component) { | ||
/* 4 */ | ||
/***/ function(module, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var INIT = exports.INIT = "init"; | ||
var PROCESS = exports.PROCESS = "processing"; | ||
var DONE = exports.DONE = "done"; | ||
/***/ }, | ||
/* 5 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("bluebird"); | ||
/***/ }, | ||
/* 6 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
@@ -385,29 +394,64 @@ | ||
}); | ||
exports.hasComponentsStatus = hasComponentsStatus; | ||
var _statuses = __webpack_require__(5); | ||
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 STATUS = _interopRequireWildcard(_statuses); | ||
var _channel = __webpack_require__(7); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
var _channel2 = _interopRequireDefault(_channel); | ||
function hasComponentsStatus(components, statuses) { | ||
if (statuses.length === undefined) { | ||
statuses = [statuses]; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var ChannelManager = function () { | ||
function ChannelManager() { | ||
_classCallCheck(this, ChannelManager); | ||
this.channels = {}; | ||
} | ||
var doneCount = components.reduce(function (doneCount, component) { | ||
if (statuses.indexOf(component.status) !== -1) { | ||
return ++doneCount; | ||
} else { | ||
return doneCount; | ||
_createClass(ChannelManager, [{ | ||
key: "createChannel", | ||
value: function createChannel(name) { | ||
this.channels[name] = new _channel2.default(); | ||
} | ||
}, 0); | ||
return doneCount === components.length; | ||
} /** | ||
* help to calculate relatives components state | ||
*/ | ||
}, { | ||
key: "setStatusProcess", | ||
value: function setStatusProcess() { | ||
var _this = this; | ||
Object.keys(this.channels).forEach(function (key) { | ||
var channel = _this.channels[key]; | ||
channel.setStatusProcess(); | ||
}); | ||
} | ||
}, { | ||
key: "setStatusDone", | ||
value: function setStatusDone(doneChannel) { | ||
var _this2 = this; | ||
Object.keys(this.channels).forEach(function (key) { | ||
var channel = _this2.channels[key]; | ||
if (channel === doneChannel) { | ||
channel.setStatusDone(); | ||
} else { | ||
channel.setStatusInit(); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: "getChannel", | ||
value: function getChannel(name) { | ||
return this.channels[name]; | ||
} | ||
}]); | ||
return ChannelManager; | ||
}(); | ||
exports.default = ChannelManager; | ||
/***/ }, | ||
/* 5 */ | ||
/***/ function(module, exports) { | ||
/* 7 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
@@ -419,14 +463,69 @@ "use strict"; | ||
}); | ||
var INIT = exports.INIT = "init"; | ||
var PROCESS = exports.PROCESS = "processing"; | ||
var DONE = exports.DONE = "done"; | ||
/***/ }, | ||
/* 6 */ | ||
/***/ function(module, exports) { | ||
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; }; }(); | ||
module.exports = require("bluebird"); | ||
var _statuses = __webpack_require__(4); | ||
var STATUS = _interopRequireWildcard(_statuses); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Channel = function () { | ||
function Channel() { | ||
_classCallCheck(this, Channel); | ||
this._status = STATUS.INIT; | ||
this._connectedChildrenComponents = []; | ||
this._output = null; | ||
} | ||
_createClass(Channel, [{ | ||
key: "addComponent", | ||
value: function addComponent(component) { | ||
this._connectedChildrenComponents.push(component); | ||
} | ||
}, { | ||
key: "getComponentList", | ||
value: function getComponentList() { | ||
return this._connectedChildrenComponents; | ||
} | ||
}, { | ||
key: "getStatus", | ||
value: function getStatus() { | ||
return this._status; | ||
} | ||
}, { | ||
key: "setStatusInit", | ||
value: function setStatusInit() { | ||
this._status = STATUS.INIT; | ||
} | ||
}, { | ||
key: "setStatusProcess", | ||
value: function setStatusProcess() { | ||
this._status = STATUS.PROCESS; | ||
} | ||
}, { | ||
key: "setStatusDone", | ||
value: function setStatusDone() { | ||
this._status = STATUS.DONE; | ||
} | ||
}, { | ||
key: "output", | ||
get: function get() { | ||
return this._output; | ||
}, | ||
set: function set(value) { | ||
this._output = value; | ||
} | ||
}]); | ||
return Channel; | ||
}(); | ||
exports.default = Channel; | ||
/***/ }, | ||
/* 7 */ | ||
/* 8 */ | ||
/***/ function(module, exports) { | ||
@@ -433,0 +532,0 @@ |
{ | ||
"name": "horpyna", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "promised modules flow control core library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
import Root from "./root"; | ||
import ChannelManager from "./channelManager"; | ||
import * as ERROR from "../constants/errors"; | ||
import * as STATUS from "../constants/statuses"; | ||
import * as Relation from "../helpers/Relation"; | ||
const DEFAULT_CHANNEL = "default"; | ||
class Component { | ||
constructor(componentFunction) { | ||
@@ -12,6 +13,7 @@ if(typeof componentFunction === "function") { | ||
} | ||
this.connectedChildrenComponents = []; | ||
this.connectedParentComponents = []; | ||
this.connectedParentChannels = []; | ||
this.status = STATUS.INIT; | ||
this.channelManager = new ChannelManager(); | ||
this.channelManager.createChannel(DEFAULT_CHANNEL); | ||
this.finalComponentFlag = false; | ||
@@ -36,3 +38,3 @@ this.rootComponent = new Root(); | ||
if(typeof this.componentFunction === "function") { | ||
this.status = STATUS.PROCESS; | ||
this.channelManager.setStatusProcess(); | ||
this.componentFunction(request, this._getResponseObject()); | ||
@@ -49,3 +51,3 @@ } else { | ||
_onParentReady() { | ||
if (Relation.hasComponentsStatus(this.connectedParentComponents, STATUS.DONE)) { | ||
if (this._isParentChannelsDone()) { | ||
this._runComponentFunction(this._getParentsOutput()); | ||
@@ -55,2 +57,14 @@ } | ||
_isParentChannelsDone() { | ||
let doneCount = this.connectedParentChannels.reduce((doneCount, channel) => { | ||
if(channel.getStatus() === STATUS.DONE) { | ||
return ++doneCount; | ||
} else { | ||
return doneCount; | ||
} | ||
}, 0); | ||
return doneCount === this.connectedParentChannels.length; | ||
} | ||
/** | ||
@@ -60,6 +74,6 @@ * gather all parents outputs | ||
_getParentsOutput() { | ||
if(this.connectedParentComponents.length === 1) { | ||
return { input: this.connectedParentComponents[0].output }; | ||
if(this.connectedParentChannels.length === 1) { | ||
return { input: this.connectedParentChannels[0].output }; | ||
} else { | ||
return { input: this.connectedParentComponents.map(component => component.output) }; | ||
return { input: this.connectedParentChannels.map(channel => channel.output) }; | ||
} | ||
@@ -74,9 +88,10 @@ } | ||
return { | ||
send: output => { | ||
send: (output, channelName) => { | ||
channelName = channelName || DEFAULT_CHANNEL; | ||
let channel = this.getChannel(channelName); | ||
if(this.rootComponent.status === STATUS.PROCESS) { | ||
this.status = STATUS.DONE; | ||
this.output = output; | ||
this.channelManager.setStatusDone(channel); | ||
channel.output = output; | ||
if(this.finalComponentFlag === false) { | ||
this.connectedChildrenComponents.forEach(component => component._onParentReady()); | ||
this.rootComponent.onAnyDone(); | ||
channel.getComponentList().forEach(component => component._onParentReady()); | ||
} else { | ||
@@ -89,4 +104,2 @@ this.rootComponent.finish(output); | ||
if(this.rootComponent.status === STATUS.PROCESS) { | ||
this.status = STATUS.DONE; | ||
this.output = output; | ||
this.rootComponent.finish(output); | ||
@@ -98,2 +111,6 @@ } | ||
getChannel(channelName) { | ||
return this.channelManager.getChannel(channelName); | ||
} | ||
/** | ||
@@ -108,21 +125,16 @@ * If this method is triggered before component is done, it is flagged as final component, | ||
/** | ||
* child component add parent component | ||
* bind parent component with this component | ||
* @param component parent component | ||
* @param channel parent channel name | ||
*/ | ||
bind(component) { | ||
this.connectedParentComponents.push(component); | ||
component._bindChild(this); | ||
bind(component, channelName) { | ||
channelName = channelName || DEFAULT_CHANNEL; | ||
let parentChannel = component.getChannel(channelName); | ||
this.connectedParentChannels.push(parentChannel); | ||
parentChannel.addComponent(this); | ||
component.rootComponent.merge(this.rootComponent); | ||
this.rootComponent = component.rootComponent; | ||
} | ||
/** | ||
* Allow parent component to add child component, should be triggered only by this.connect | ||
* @param component child component | ||
*/ | ||
_bindChild(component) { | ||
this.connectedChildrenComponents.push(component); | ||
this.rootComponent.merge(component.rootComponent); | ||
component.rootComponent = this.rootComponent; | ||
} | ||
} | ||
export default Component; |
@@ -1,2 +0,1 @@ | ||
import * as Relation from "../helpers/Relation"; | ||
import * as STATUS from "../constants/statuses"; | ||
@@ -11,16 +10,2 @@ import Promise from "bluebird"; | ||
/** | ||
* root component function, it is triggered by any child component after finish | ||
*/ | ||
onAnyDone() { | ||
/** | ||
* if all components are done then finish promise | ||
* | ||
* Now chain is finished when component run method finish | ||
*/ | ||
//if(Relation.hasComponentsStatus(this.components, [STATUS.DONE, STATUS.INIT])) { | ||
// this.finish(); | ||
//} | ||
} | ||
addComponent(component) { | ||
@@ -27,0 +12,0 @@ this.components.push(component); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
77657
29
999