yjs/dest/lib/HistoryBuffer.js
2014-08-05 17:04:39 +02:00

93 lines
2.2 KiB
JavaScript

var HistoryBuffer;
HistoryBuffer = (function() {
function HistoryBuffer(user_id) {
this.user_id = user_id;
this.operation_counter = {};
this.buffer = {};
this.change_listeners = [];
}
HistoryBuffer.prototype.getUserId = function() {
return this.user_id;
};
HistoryBuffer.prototype.getOperationCounter = function() {
var ctn, res, user, _ref;
res = {};
_ref = this.operation_counter;
for (user in _ref) {
ctn = _ref[user];
res[user] = ctn;
}
return res;
};
HistoryBuffer.prototype._encode = function() {
var json, o, o_number, u_name, user, _ref;
json = [];
_ref = this.buffer;
for (u_name in _ref) {
user = _ref[u_name];
for (o_number in user) {
o = user[o_number];
if (!isNaN(parseInt(o_number))) {
json.push(o._encode());
}
}
}
return json;
};
HistoryBuffer.prototype.getNextOperationIdentifier = function(user_id) {
var uid;
if (user_id == null) {
user_id = this.user_id;
}
if (this.operation_counter[user_id] == null) {
this.operation_counter[user_id] = 0;
}
uid = {
'creator': user_id,
'op_number': this.operation_counter[user_id]
};
this.operation_counter[user_id]++;
return uid;
};
HistoryBuffer.prototype.getOperation = function(uid) {
var _ref;
if (uid instanceof Object) {
return (_ref = this.buffer[uid.creator]) != null ? _ref[uid.op_number] : void 0;
} else if (uid == null) {
} else {
throw new Error("This type of uid is not defined!");
}
};
HistoryBuffer.prototype.addOperation = function(o) {
if (this.buffer[o.creator] == null) {
this.buffer[o.creator] = {};
}
if (this.operation_counter[o.creator] == null) {
this.operation_counter[o.creator] = 0;
}
if (this.buffer[o.creator][o.op_number] != null) {
throw new Error("You must not overwrite operations!");
}
this.buffer[o.creator][o.op_number] = o;
if (typeof o.op_number === 'number' && o.creator !== this.getUserId()) {
this.operation_counter[o.creator]++;
}
return o;
};
return HistoryBuffer;
})();
module.exports = HistoryBuffer;
//# sourceMappingURL=HistoryBuffer.js.map