added support to use existing user ids! Fixes #23

This commit is contained in:
Kevin Jahns
2015-05-28 15:44:13 +02:00
parent a9c2ec6ba0
commit dc3c6a5d42
14 changed files with 39358 additions and 508 deletions

View File

@@ -154,7 +154,7 @@ module.exports = {
this.send(user, {
sync_step: "getHB",
send_again: "true",
data: []
data: this.getStateVector()
});
if (!this.sent_hb_to_all_users) {
this.sent_hb_to_all_users = true;
@@ -184,7 +184,7 @@ module.exports = {
this.send(user, {
sync_step: "getHB",
send_again: "true",
data: []
data: this.getStateVector()
});
hb = this.getHB([]).hb;
_hb = [];
@@ -221,8 +221,14 @@ module.exports = {
return null;
}
},
whenReceivedStateVector: function(f) {
if (this.when_received_state_vector_listeners == null) {
this.when_received_state_vector_listeners = [];
}
return this.when_received_state_vector_listeners.push(f);
},
receiveMessage: function(sender, res) {
var _hb, data, f, hb, i, j, len, len1, o, ref, results, sendApplyHB, send_again;
var _hb, data, f, hb, i, j, k, len, len1, len2, o, ref, ref1, results, sendApplyHB, send_again;
if (res.sync_step == null) {
ref = this.receive_handlers;
results = [];
@@ -236,6 +242,14 @@ module.exports = {
return;
}
if (res.sync_step === "getHB") {
if (this.when_received_state_vector_listeners != null) {
ref1 = this.when_received_state_vector_listeners;
for (j = 0, len1 = ref1.length; j < len1; j++) {
f = ref1[j];
f.call(this, res.data);
}
}
delete this.when_received_state_vector_listeners;
data = this.getHB(res.data);
hb = data.hb;
_hb = [];
@@ -252,8 +266,8 @@ module.exports = {
};
})(this);
}
for (j = 0, len1 = hb.length; j < len1; j++) {
o = hb[j];
for (k = 0, len2 = hb.length; k < len2; k++) {
o = hb[k];
_hb.push(o);
if (_hb.length > 10) {
sendApplyHB({
@@ -271,10 +285,10 @@ module.exports = {
send_again = (function(_this) {
return function(sv) {
return function() {
var k, len2;
var l, len3;
hb = _this.getHB(sv).hb;
for (k = 0, len2 = hb.length; k < len2; k++) {
o = hb[k];
for (l = 0, len3 = hb.length; l < len3; l++) {
o = hb[l];
_hb.push(o);
if (_hb.length > 10) {
_this.send(sender, {

View File

@@ -16,30 +16,26 @@ HistoryBuffer = (function() {
setTimeout(this.emptyGarbage, this.garbageCollectTimeout);
}
HistoryBuffer.prototype.resetUserId = function(id) {
var o, o_name, own;
own = this.buffer[this.user_id];
if (own != null) {
for (o_name in own) {
o = own[o_name];
if (o.uid.creator != null) {
o.uid.creator = id;
}
if (o.uid.alt != null) {
o.uid.alt.creator = id;
}
}
if (this.buffer[id] != null) {
throw new Error("You are re-assigning an old user id - this is not (yet) possible!");
}
this.buffer[id] = own;
delete this.buffer[this.user_id];
HistoryBuffer.prototype.setUserId = function(user_id1, state_vector) {
var base, buff, counter_diff, name, o, o_name, ref;
this.user_id = user_id1;
if ((base = this.buffer)[name = this.user_id] == null) {
base[name] = [];
}
if (this.operation_counter[this.user_id] != null) {
this.operation_counter[id] = this.operation_counter[this.user_id];
delete this.operation_counter[this.user_id];
buff = this.buffer[this.user_id];
counter_diff = state_vector[this.user_id] || 0;
if (this.buffer._temp != null) {
ref = this.buffer._temp;
for (o_name in ref) {
o = ref[o_name];
o.uid.creator = this.user_id;
o.uid.op_number += counter_diff;
buff[o.uid.op_number] = o;
}
}
return this.user_id = id;
this.operation_counter[this.user_id] = (this.operation_counter._temp || 0) + counter_diff;
delete this.operation_counter._temp;
return delete this.buffer._temp;
};
HistoryBuffer.prototype.emptyGarbage = function() {
@@ -243,15 +239,13 @@ HistoryBuffer = (function() {
if ((base = this.operation_counter)[name = o.uid.creator] == null) {
base[name] = 0;
}
if (o.uid.creator !== this.getUserId()) {
if (o.uid.op_number === this.operation_counter[o.uid.creator]) {
this.operation_counter[o.uid.creator]++;
}
while (this.buffer[o.uid.creator][this.operation_counter[o.uid.creator]] != null) {
this.operation_counter[o.uid.creator]++;
}
return void 0;
if (o.uid.op_number === this.operation_counter[o.uid.creator]) {
this.operation_counter[o.uid.creator]++;
}
while (this.buffer[o.uid.creator][this.operation_counter[o.uid.creator]] != null) {
this.operation_counter[o.uid.creator]++;
}
return void 0;
};
return HistoryBuffer;

View File

@@ -10,15 +10,15 @@ adaptConnector = require("./ConnectorAdapter");
createY = function(connector) {
var HB, ct, engine, model, ops, ops_manager, user_id;
user_id = null;
if (connector.user_id != null) {
user_id = connector.user_id;
} else {
user_id = "_temp";
connector.on_user_id_set = function(id) {
user_id = id;
return HB.resetUserId(id);
};
connector.when_received_state_vector_listeners = [
function(state_vector) {
return HB.setUserId(this.user_id, state_vector);
}
];
}
HB = new HistoryBuffer(user_id);
ops_manager = structured_ops_uninitialized(HB, this.constructor);