nci/static/js/app/stores/terminal.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-05-07 21:55:40 +00:00
'use strict';
define([
2015-10-15 05:36:04 +00:00
'underscore', 'reflux', 'app/actions/build', 'app/connect'
], function(
_, Reflux, BuildActions, connect
) {
2015-05-07 21:55:40 +00:00
var Store = Reflux.createStore({
listenables: BuildActions,
init: function() {
2015-06-14 21:10:42 +00:00
// the only purpose of this hash to reconnect all the time
// except first, see notes at using
this.connectedResourcesHash = {};
2015-05-07 21:55:40 +00:00
},
2015-05-14 20:37:44 +00:00
onReadTerminalOutput: function(build) {
var self = this,
output = '',
resourceName = 'build' + build.id;
2015-05-07 21:55:40 +00:00
var connectToBuildDataResource = function() {
2015-06-14 21:10:42 +00:00
// reconnect for get data below (at subscribe), coz
// data emitted only once during connect
if (self.connectedResourcesHash[resourceName]) {
connect.resource(resourceName).reconnect();
} else {
self.connectedResourcesHash[resourceName] = 1;
}
connect.resource(resourceName).subscribe('data', function(data) {
2015-10-15 05:36:04 +00:00
output += _(data.lines).pluck('text').join('');
self.trigger({
buildId: build.id,
name: 'Console for build #' + build.id,
data: output
});
2015-05-07 21:55:40 +00:00
});
};
// create data resource for completed build
2015-05-21 19:04:38 +00:00
if (build.completed) {
connect.resource('projects').sync(
'createBuildDataResource',
{buildId: build.id},
function(err) {
if (err) throw err;
connectToBuildDataResource();
}
);
} else {
connectToBuildDataResource();
}
2015-05-07 21:55:40 +00:00
}
});
return Store;
});