patch dataio to notify resource clients from the server

This commit is contained in:
oleg 2015-05-03 18:23:01 +03:00
parent 5a51b4b171
commit f120bde87d
6 changed files with 67 additions and 25 deletions

2
app.js
View File

@ -20,7 +20,7 @@ var server = http.createServer(function(req, res, next) {
});
var socketio = require('socket.io')(server);
var dataio = require('data.io')(socketio);
var dataio = require('./dataio')(socketio);
var app = {
server: server,

40
dataio.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
var dataio = require('data.io'),
Server = require('./node_modules/data.io/lib/server'),
Resource = require('./node_modules/data.io/lib/resource'),
Sync = require('./node_modules/data.io/lib/sync');
/*
* Patch server and resource to provide ability to send data to all clients
* of the resource
*/
Server.prototype.resource = function(name, resource) {
var self = this;
if (resource === undefined) {
resource = this.resources[name];
if (resource) return resource;
resource = new Resource();
}
this.resources[name] = resource;
this.namespace(name).on('connection', function(client) {
self.connect(resource, client);
});
// save link to the namespace at resource
resource.namespace = this.namespace(name);
return resource;
};
Resource.prototype.clientEmitSync = function(action, data) {
this.namespace.emit('sync', action, data);
};
module.exports = function() {
return dataio.apply(dataio, arguments);
};

View File

@ -1,20 +1,7 @@
'use strict';
module.exports = function(app) {
var builds = [{
project: {
name: 'foo'
},
start: Date.now(),
step: 1,
completed: false,
status: 'inprogress'
}];
var resource = app.dataio.resource('builds');
resource.use('readAll', function(req, res) {
console.log('readAll');
res.send(builds);
});
return resource;
};

View File

@ -2,8 +2,8 @@
var _ = require('underscore');
module.exports = function(data) {
module.exports = function(app) {
_(['builds', 'projects']).each(function(resource) {
require('./' + resource)(data);
require('./' + resource)(app);
});
};

View File

@ -4,10 +4,6 @@ var _ = require('underscore'),
project = require('../lib/project'),
Distributor = require('../lib/distributor').Distributor;
var distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}]
});
var projects, projectsHash;
project.loadAll('projects', function(err, loadedProjects) {
@ -23,6 +19,19 @@ project.loadAll('projects', function(err, loadedProjects) {
});
module.exports = function(app) {
var distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}],
onBuildUpdate: function(build, callback) {
var buildsResource = app.dataio.resource('builds');
buildsResource.clientEmitSync(
build.status === 'waiting' ? 'create' : 'update',
build
);
callback(null, build);
}
});
var resource = app.dataio.resource('projects');
resource.use('read', function(req, res) {
@ -34,8 +43,10 @@ module.exports = function(app) {
project = projectsHash[projectName];
console.log('Run the project: %j', project || projectName);
distributor.run(project.config, {}, function(err, build) {
console.log('>>> err, build = ', err && err.stack || err, build)
res.send({err: err, build: build});
console.log('>>> err, build = ', err && err.stack || err, build);
});
res.send();
});
return resource;
};

View File

@ -8,14 +8,14 @@ define([
var connect = dataio(socketio.connect());
var projects = connect.resource('projects');
var builds = connect.resource('builds');
var projectsTemplate = _($('#projects-template').html()).template();
$('#content').on('click', '.js-projects .js-run', function() {
var projectName = $(this).parent('.js-project').data('name');
projects.sync('run', {projectName: projectName}, function(err, result) {
$('#content').append(
(err && err.message) ||
JSON.stringify(result)
(err && err.message)
);
});
});
@ -26,4 +26,8 @@ define([
projectsTemplate({projects: projects})
);
});
builds.subscribe(function(data, action) {
$('#content').append(action.action + ': ' + JSON.stringify(data));
});
});