|
|
@ -6,6 +6,8 @@ define([
|
|
|
|
], function (Util, Constants, Messages, Crypto) {
|
|
|
|
], function (Util, Constants, Messages, Crypto) {
|
|
|
|
var Cursor = {};
|
|
|
|
var Cursor = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var DEGRADED = 3; // XXX Number of users before switching to degraded mode
|
|
|
|
|
|
|
|
|
|
|
|
var convertToUint8 = function (obj) {
|
|
|
|
var convertToUint8 = function (obj) {
|
|
|
|
var l = Object.keys(obj).length;
|
|
|
|
var l = Object.keys(obj).length;
|
|
|
|
var u = new Uint8Array(l);
|
|
|
|
var u = new Uint8Array(l);
|
|
|
@ -21,6 +23,7 @@ define([
|
|
|
|
if (!client || !client.cursor) { return; }
|
|
|
|
if (!client || !client.cursor) { return; }
|
|
|
|
var chan = ctx.channels[client.channel];
|
|
|
|
var chan = ctx.channels[client.channel];
|
|
|
|
if (!chan) { return; }
|
|
|
|
if (!chan) { return; }
|
|
|
|
|
|
|
|
if (chan.degraded) { return; }
|
|
|
|
if (!chan.sendMsg) { return; } // Store not synced yet, we're running with the cache
|
|
|
|
if (!chan.sendMsg) { return; } // Store not synced yet, we're running with the cache
|
|
|
|
var data = {
|
|
|
|
var data = {
|
|
|
|
id: client.id,
|
|
|
|
id: client.id,
|
|
|
@ -34,6 +37,7 @@ define([
|
|
|
|
|
|
|
|
|
|
|
|
// Send all our cursors data when someone remote joins the channel
|
|
|
|
// Send all our cursors data when someone remote joins the channel
|
|
|
|
var sendOurCursors = function (ctx, chan) {
|
|
|
|
var sendOurCursors = function (ctx, chan) {
|
|
|
|
|
|
|
|
if (chan.degraded) { return; }
|
|
|
|
chan.clients.forEach(function (c) {
|
|
|
|
chan.clients.forEach(function (c) {
|
|
|
|
var client = ctx.clients[c];
|
|
|
|
var client = ctx.clients[c];
|
|
|
|
if (!client) { return; }
|
|
|
|
if (!client) { return; }
|
|
|
@ -77,6 +81,7 @@ define([
|
|
|
|
// ==> Send the cursor position of the other tabs
|
|
|
|
// ==> Send the cursor position of the other tabs
|
|
|
|
chan.clients.forEach(function (cl) {
|
|
|
|
chan.clients.forEach(function (cl) {
|
|
|
|
var clientObj = ctx.clients[cl];
|
|
|
|
var clientObj = ctx.clients[cl];
|
|
|
|
|
|
|
|
if (chan.degraded) { return; }
|
|
|
|
if (!clientObj) { return; }
|
|
|
|
if (!clientObj) { return; }
|
|
|
|
ctx.emit('MESSAGE', {
|
|
|
|
ctx.emit('MESSAGE', {
|
|
|
|
id: clientObj.id,
|
|
|
|
id: clientObj.id,
|
|
|
@ -90,6 +95,11 @@ define([
|
|
|
|
return void cb();
|
|
|
|
return void cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var updateDegraded = function (ctx, wc, chan) {
|
|
|
|
|
|
|
|
var m = wc.members;
|
|
|
|
|
|
|
|
chan.degraded = (m.length-1) >= DEGRADED;
|
|
|
|
|
|
|
|
ctx.emit('DEGRADED', { degraded: chan.degraded }, chan.clients);
|
|
|
|
|
|
|
|
};
|
|
|
|
var onOpen = function (wc) {
|
|
|
|
var onOpen = function (wc) {
|
|
|
|
|
|
|
|
|
|
|
|
ctx.channels[channel] = ctx.channels[channel] || {};
|
|
|
|
ctx.channels[channel] = ctx.channels[channel] || {};
|
|
|
@ -113,11 +123,14 @@ define([
|
|
|
|
|
|
|
|
|
|
|
|
wc.on('join', function () {
|
|
|
|
wc.on('join', function () {
|
|
|
|
sendOurCursors(ctx, chan);
|
|
|
|
sendOurCursors(ctx, chan);
|
|
|
|
|
|
|
|
updateDegraded(ctx, wc, chan);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
wc.on('leave', function (peer) {
|
|
|
|
wc.on('leave', function (peer) {
|
|
|
|
ctx.emit('MESSAGE', {leave: true, id: peer}, chan.clients);
|
|
|
|
ctx.emit('MESSAGE', {leave: true, id: peer}, chan.clients);
|
|
|
|
|
|
|
|
updateDegraded(ctx, wc, chan);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
wc.on('message', function (cryptMsg) {
|
|
|
|
wc.on('message', function (cryptMsg) {
|
|
|
|
|
|
|
|
if (chan.degraded) { return; }
|
|
|
|
var msg = chan.encryptor.decrypt(cryptMsg, secret.keys && secret.keys.validateKey);
|
|
|
|
var msg = chan.encryptor.decrypt(cryptMsg, secret.keys && secret.keys.validateKey);
|
|
|
|
var parsed;
|
|
|
|
var parsed;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -129,6 +142,7 @@ define([
|
|
|
|
} catch (e) { console.error(e); }
|
|
|
|
} catch (e) { console.error(e); }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chan.wc = wc;
|
|
|
|
chan.wc = wc;
|
|
|
|
chan.sendMsg = function (msg, cb) {
|
|
|
|
chan.sendMsg = function (msg, cb) {
|
|
|
|
cb = cb || function () {};
|
|
|
|
cb = cb || function () {};
|
|
|
@ -144,6 +158,8 @@ define([
|
|
|
|
chan.clients = [client];
|
|
|
|
chan.clients = [client];
|
|
|
|
first = false;
|
|
|
|
first = false;
|
|
|
|
cb();
|
|
|
|
cb();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateDegraded(ctx, wc, chan);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
network.join(channel).then(onOpen, function (err) {
|
|
|
|
network.join(channel).then(onOpen, function (err) {
|
|
|
@ -223,10 +239,10 @@ define([
|
|
|
|
delete ctx.clients[clientId];
|
|
|
|
delete ctx.clients[clientId];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Cursor.init = function (store, emit) {
|
|
|
|
Cursor.init = function (cfg, waitFor, emit) {
|
|
|
|
var cursor = {};
|
|
|
|
var cursor = {};
|
|
|
|
var ctx = {
|
|
|
|
var ctx = {
|
|
|
|
store: store,
|
|
|
|
store: cfg.store,
|
|
|
|
emit: emit,
|
|
|
|
emit: emit,
|
|
|
|
channels: {},
|
|
|
|
channels: {},
|
|
|
|
clients: {}
|
|
|
|
clients: {}
|
|
|
|