From f120bde87d3cd81188bc54e0ea1262617caa14e3 Mon Sep 17 00:00:00 2001 From: oleg Date: Sun, 3 May 2015 18:23:01 +0300 Subject: [PATCH] patch dataio to notify resource clients from the server --- app.js | 2 +- dataio.js | 40 ++++++++++++++++++++++++++++++++++++++++ resources/builds.js | 15 +-------------- resources/index.js | 4 ++-- resources/projects.js | 23 +++++++++++++++++------ static/js/app/app.js | 8 ++++++-- 6 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 dataio.js diff --git a/app.js b/app.js index 20d2edc..628cdac 100644 --- a/app.js +++ b/app.js @@ -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, diff --git a/dataio.js b/dataio.js new file mode 100644 index 0000000..0259634 --- /dev/null +++ b/dataio.js @@ -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); +}; diff --git a/resources/builds.js b/resources/builds.js index 42999c7..d1c626d 100644 --- a/resources/builds.js +++ b/resources/builds.js @@ -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; }; diff --git a/resources/index.js b/resources/index.js index 332f4eb..bcb8067 100644 --- a/resources/index.js +++ b/resources/index.js @@ -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); }); }; diff --git a/resources/projects.js b/resources/projects.js index 425ccdd..5bd40ef 100644 --- a/resources/projects.js +++ b/resources/projects.js @@ -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; }; diff --git a/static/js/app/app.js b/static/js/app/app.js index 796b7e7..2007b32 100644 --- a/static/js/app/app.js +++ b/static/js/app/app.js @@ -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)); + }); });