From ef866e274c62b6b983343c8f7c1ca810895aae88 Mon Sep 17 00:00:00 2001 From: jeotic Date: Sun, 4 Oct 2015 21:10:04 -0400 Subject: [PATCH] Created based Unity class, with some testing --- .gitignore | 3 +++ bin/unity.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ child.js | 11 +++++++++++ index.js | 23 ++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 bin/unity.js create mode 100644 child.js create mode 100644 index.js diff --git a/.gitignore b/.gitignore index 123ae94..b9e5ca7 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules +*.workspace +*.tern-project +.tern-port diff --git a/bin/unity.js b/bin/unity.js new file mode 100644 index 0000000..6d714ac --- /dev/null +++ b/bin/unity.js @@ -0,0 +1,55 @@ +var r = require('responder'); +var util = require("util"); +var cp = require('child_process'); + +function Unity(){ + r.Responder.call(this); + + this.process = {}; + + this.process.on = function(child, message){ + var event = message.event; + var data = message.data; + + r.emit({ + event: event, + child: child + }, data); + }; + + this.process.emit = function(child){ + r.on({ event: '(.*)', child: child }, function(){ + var ev = this.event; + var data = Array.prototype.slice(arguments); + + data.pop();//last variable should be the callback; don't want that + + child.send({ + event: ev, + data: data + }); + }); + }; +} + +util.inherits(Unity, r.Responder); + +Unity.prototype.spawn = function(path){ + var n = cp.fork(path); + var u = this; + + n.on('message', function(){ + var args = Array.prototype.slice.call(arguments); + args.unshift(n); + + u.process.on.apply(u, args); + }); + + this.process.emit(n, n.send); + + return n; +}; + +module.exports = new Unity(); + +module.exports.Unity = Unity; \ No newline at end of file diff --git a/child.js b/child.js new file mode 100644 index 0000000..ccdee37 --- /dev/null +++ b/child.js @@ -0,0 +1,11 @@ +var r = require('responder'); + +r.on('/child/:childName', function(){ + console.log('Child ', arguments); +}); + +r.emit('/parent/awesome', function(){ + console.log('Child emit cb ', arguments); +}); + +console.log('I AM A CHILD'); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..00c65ba --- /dev/null +++ b/index.js @@ -0,0 +1,23 @@ +var unity = require('./bin/unity'); + +unity.on('/test/:name', function(name, next){ + console.log(this); + + next(name); + +}); + +unity.on('/parent/:name', function(name, next){ + console.log('PARENT ', name); + +}); + +var s = unity.spawn(__dirname + '/child.js'); + +setTimeout(function(){ + + unity.emit('/child/joe', function(){ + //console.log(arguments); + }); + +}, 1000); \ No newline at end of file