Skip to content

Commit

Permalink
Added channel support (courtesy of podviaznikov)
Browse files Browse the repository at this point in the history
  • Loading branch information
scttnlsn committed Mar 4, 2012
1 parent fa03e56 commit 1e6d820
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,26 @@
};

var buildBackend = function(collection) {
var name = collection.backend;
var options = collection.backend;
var promise = new Promise(collection);
var channel = undefined;

if (typeof options === 'string') {
var name = options;
} else {
var name = options.name;
var channel = options.channel;
}

var backend = {
name: name,
channel: channel,
socket: socket.of(name),
options: null,
ready: promise.add
};

backend.socket.emit('listen', function(options) {
backend.socket.emit('listen', backend.channel, function(options) {
backend.options = options;

backend.socket.on('synced', function(method, resp) {
Expand Down
20 changes: 17 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,30 @@ exports.listen = function(server, backends, options) {
io.of(backend).on('connection', function(socket) {
var sync = new Sync(backend, socket, options);

socket.on('listen', function(callback) {
callback(options);
socket.on('listen', function(channel, callback) {
socket.set('channel', channel, function() {
if (channel) {
socket.join(channel);
}

callback(options);
});
});

socket.on('sync', function(req, callback) {
sync.handle(backends[backend], req, function(err, result) {
callback(err, result);

if (!err && req.method !== 'read') {
socket.broadcast.emit('synced', req.method, result);
socket.get('channel', function(err, channel) {
if (err) return;

if (channel) {
socket.broadcast.to(channel).emit('synced', req.method, result);
} else {
socket.broadcast.emit('synced', req.method, result);
}
});
}
});
});
Expand Down

0 comments on commit 1e6d820

Please sign in to comment.