Comparing version 0.2.1 to 0.3.0
33
canop.js
@@ -28,2 +28,5 @@ (function (root, factory) { | ||
var PROTOCOL_WARN_UNKNOWN_BASE = 0; | ||
var warnNamesFromCode = [ | ||
"UnknownBaseError", | ||
]; | ||
@@ -413,2 +416,3 @@ // The last incrementable integer in IEEE754. | ||
this.nextClientId = 1; | ||
this.clientCount = 0; // Number of clients connected. | ||
this.signalFromClient = Object.create(null); | ||
@@ -426,2 +430,3 @@ | ||
self.clientState = STATE_LOADING; | ||
self.clientCount++; | ||
} catch(e) { | ||
@@ -436,3 +441,10 @@ self.emit('unsyncable', e); | ||
}); | ||
self.once('syncing', initiateLoading); | ||
this.once('syncing', initiateLoading); | ||
this.on('signal', function(event) { | ||
if (event.data.connected !== undefined) { | ||
if (event.data.connected) { | ||
self.clientCount++; | ||
} else { self.clientCount--; } | ||
} | ||
}); | ||
} | ||
@@ -631,3 +643,5 @@ } | ||
// local changes are applied on top of if. | ||
self.emit('unsyncable'); | ||
var error = new Error(error[1]); | ||
error.name = warnNamesFromCode[error[0]]; | ||
self.emit('unsyncable', error); | ||
} | ||
@@ -884,2 +898,3 @@ }); | ||
self.clients[newClient.id] = newClient; | ||
self.clientCount++; | ||
@@ -934,3 +949,2 @@ newClient.onReceive(function receiveFromClient(message) { | ||
var data = protocol[2]; | ||
self.signalFromClient[clientId] = self.signalFromClient[clientId] || {}; | ||
if (data !== undefined) { | ||
@@ -951,2 +965,6 @@ for (var key in data) { | ||
}); | ||
// Send the connection signal. | ||
this.signalFromClient[newClient.id] = this.signalFromClient[newClient.id] || | ||
Object.create(null); | ||
this.signalFromClient[newClient.id].connected = true; | ||
}, | ||
@@ -957,5 +975,8 @@ | ||
for (var clientId in this.clients) { | ||
if (this.signalFromClient[clientId] !== undefined) { | ||
if (client.id !== +clientId) { | ||
client.send(JSON.stringify([PROTOCOL_SIGNAL, +clientId, | ||
this.signalFromClient[clientId]])); | ||
var otherClient = this.clients[clientId]; | ||
otherClient.send(JSON.stringify([PROTOCOL_SIGNAL, client.id, | ||
this.signalFromClient[client.id]])); | ||
} | ||
@@ -971,5 +992,7 @@ } | ||
var aClient = this.clients[aClientId]; | ||
aClient.send(JSON.stringify([PROTOCOL_SIGNAL, +clientId])); | ||
aClient.send(JSON.stringify([PROTOCOL_SIGNAL, +clientId, | ||
{connected: false}])); | ||
} | ||
delete this.signalFromClient[clientId]; | ||
self.clientCount--; | ||
}, | ||
@@ -976,0 +999,0 @@ |
{ | ||
"name": "canop", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Convergent algorithm for collaborative text.", | ||
@@ -5,0 +5,0 @@ "main": "canop.js", |
@@ -29,3 +29,5 @@ `canop` | ||
// cursor (which moves the selection with shift+arrow). | ||
// Also, connected is signaled when a node joins or leaves. | ||
client.signal({ name: 'Grace', focus: ['some'], sel: [[9,9]] }); | ||
client.clientCount // Number of clients currently connected. | ||
@@ -139,3 +141,2 @@ // This event has the following keys: | ||
- Signal a list of all currently connected clients and of disconnections | ||
- JSON-compatible protocol | ||
@@ -142,0 +143,0 @@ - Array index rebasing |
@@ -127,1 +127,13 @@ var canop = require('../canop.js'); | ||
assert.equal(result, 'a', 'Inserting a character immediately removed'); | ||
// localId | ||
var star = new Star(''); | ||
assert.equal(star.clients[0].localId, 1, 'Client 0 localId'); | ||
assert.equal(star.clients[1].localId, 2, 'Client 1 localId'); | ||
// clientCount | ||
var star = new Star(''); | ||
sendChange(star, 0); | ||
assert.equal(star.server.clientCount, 2, 'Server clientCount'); | ||
assert.equal(star.clients[0].clientCount, 2, 'Client 0 clientCount'); | ||
assert.equal(star.clients[1].clientCount, 2, 'Client 1 clientCount'); |
196332
1460
145