Skip to content

Commit

Permalink
Added sockets plugin, which bridges eve -> socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonOehlman committed Oct 4, 2011
1 parent 5181b71 commit 99183d1
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 21 deletions.
14 changes: 14 additions & 0 deletions assets/client/bridge-deckjs.js
@@ -1,3 +1,17 @@
$(function() {
$.deck('.slide');

function relayEvents(evt, from, to) {
eve('deck.goto', DECKEM, to);
} // relayEvents

eve.on('deck.goto', function(index, remote) {
if (remote) {
$(document).unbind('deck.change');
$.deck('go', index);
$(document).bind('deck.change', relayEvents);
} // if
});

$(document).bind('deck.change', relayEvents);
});
34 changes: 34 additions & 0 deletions assets/plugins/sockets.js
@@ -0,0 +1,34 @@
DECKEM.Sockets = (function() {

var reSock = /^sock\:.*$/i;

/* exports */

function connect(server) {
if (typeof io != 'undefined') {
console.log('attempting connection to: ' + server);

var socket = io.connect(server);
socket.on('connect', function() {
eve.on('deck.*', function() {
// get the last argument
var lastArg = arguments.length ? arguments[arguments.length - 1] : null;

// if the last arg is a socket connection, then don't send it to the serve
// because we got it from the server...
if ((! lastArg) || (! reSock.test(lastArg))) {
socket.emit.apply(socket, ['event', eve.nt()].concat(Array.prototype.slice.call(arguments, 0)));
} // if
});

socket.on('event', function(evtName) {
eve.apply(eve, [evtName, DECKEM].concat(Array.prototype.slice.call(arguments, 1)));
});
});
}
} // connext

return {
connect: connect
};
})();
4 changes: 2 additions & 2 deletions assets/templates/default/layout.jade
Expand Up @@ -28,8 +28,8 @@ html

script(src='#{clientpath}deckem.js')

- plugins.forEach(function(plugin) {
script(src='#{clientpath}deckem-#{plugin}.js')
- scripts.forEach(function(pluginScript) {
script= pluginScript
- })

script(src='#{clientpath}bridge-#{bridge}.js')
Expand Down
6 changes: 6 additions & 0 deletions build.js
Expand Up @@ -17,6 +17,12 @@ interleave('src', {
config: config
});

interleave('src/plugins', {
multi: 'pass',
path: 'assets/plugins',
config: config
});

// build each of the themse
interleave('src/themes', {
multi: 'pass',
Expand Down
73 changes: 59 additions & 14 deletions lib/actions/build.js
Expand Up @@ -11,20 +11,22 @@ function compile(builder, deckPath, template, opts, callback) {

fs.readFile(targetFile, 'utf8', function(err, data) {
if (! err) {
var deck = jade.compile(data, {
filename: targetFile
}),
deckContent = deck(opts),
templateOutput = template(_.extend({
bridge: 'deckjs',
deck: deckContent,
title: 'Untitled Deck',
theme: 'default',
includes: [],
plugins: []
}, opts));

fs.writeFile(outputFile, templateOutput);
loadPlugins(builder, opts.plugins || {}, function(scripts, includes) {
var deck = jade.compile(data, {
filename: targetFile
}),
deckContent = deck(opts),
templateOutput = template(_.extend({
bridge: 'deckjs',
deck: deckContent,
title: 'Untitled Deck',
theme: 'default',
scripts: scripts || [],
includes: includes || []
}, opts));

fs.writeFile(outputFile, templateOutput);
});
} // if

callback();
Expand Down Expand Up @@ -72,6 +74,8 @@ function findDecks(builder, targetPath, config, callback) {
builder.out('!{red}Error parsing deck config @ {0}', configFile);
} // try..catch
} // if

// load the required plugins

// read the contents of the directory
fs.readdir(targetPath, function(err, files) {
Expand Down Expand Up @@ -105,6 +109,47 @@ function getClientPath(targetPath, basePath) {
return clientPath;
} // getClientPath

function loadPlugins(builder, plugins, callback) {

var scripts = [], includes = [];

function loadPlugin(plugin, loadCallback) {
var pluginLoader;

try {
pluginLoader = require(path.resolve(__dirname, '../plugins/' + plugin));
}
catch (e) {
builder.out('!{red}Could not load plugin: {0}', plugin);
} // try..catch

if (pluginLoader) {
pluginLoader.call(pluginLoader, builder, plugins[plugin], function(newScripts, newIncludes) {
if (newScripts) {
scripts = scripts.concat(newScripts);
} // if

if (newIncludes) {
includes = includes.concat(newIncludes);
} // if

loadCallback();
});
}
else {
loadCallback();
} // if..else
} // loadPlugin

console.log('loading plugins: ' + _(plugins).keys());

async.forEach(_(plugins).keys(), loadPlugin, function() {
if (callback) {
callback(scripts, includes);
} // if
});
} // loadPlugins

function loadTemplate(templatePath, callback) {
var layoutFile = path.join(templatePath, 'layout.jade');

Expand Down
20 changes: 20 additions & 0 deletions lib/plugins/sockets.js
@@ -0,0 +1,20 @@
var fs = require('fs'),
reTrailingSlash = /\/$/;

module.exports = function(builder, opts, callback) {
// initialise options
opts = opts || {};

// initialise the server to the default shared deckem server (which doesn't exist, btw)
opts.server = (opts.server || 'http://deckem.com').replace(reTrailingSlash, '');

// load the sockets plugin text
fs.readFile(builder.getAssetPath('plugins/sockets.js'), 'utf8', function(err, data) {
if (! err) {
callback([data, 'DECKEM.Sockets.connect(\'' + opts.server + '\')'], opts.server + '/socket.io/socket.io.js');
}
else {
callback();
} // if..else
});
};
14 changes: 14 additions & 0 deletions src/bridge-deckjs.js
@@ -1,3 +1,17 @@
$(function() {
$.deck('.slide');

function relayEvents(evt, from, to) {
eve('deck.goto', DECKEM, to);
} // relayEvents

eve.on('deck.goto', function(index, remote) {
if (remote) {
$(document).unbind('deck.change');
$.deck('go', index);
$(document).bind('deck.change', relayEvents);
} // if
});

$(document).bind('deck.change', relayEvents);
});
5 changes: 0 additions & 5 deletions src/deckem-sockets.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/plugins/sockets.js
@@ -0,0 +1,34 @@
DECKEM.Sockets = (function() {

var reSock = /^sock\:.*$/i;

/* exports */

function connect(server) {
if (typeof io != 'undefined') {
console.log('attempting connection to: ' + server);

var socket = io.connect(server);
socket.on('connect', function() {
eve.on('deck.*', function() {
// get the last argument
var lastArg = arguments.length ? arguments[arguments.length - 1] : null;

// if the last arg is a socket connection, then don't send it to the serve
// because we got it from the server...
if ((! lastArg) || (! reSock.test(lastArg))) {
socket.emit.apply(socket, ['event', eve.nt()].concat(Array.prototype.slice.call(arguments, 0)));
} // if
});

socket.on('event', function(evtName) {
eve.apply(eve, [evtName, DECKEM].concat(Array.prototype.slice.call(arguments, 1)));
});
});
}
} // connext

return {
connect: connect
};
})();

0 comments on commit 99183d1

Please sign in to comment.