support for circular structures (e.g. with JSON)

This commit is contained in:
DadaMonad
2015-02-24 16:09:42 +00:00
parent fea6de3bf9
commit 3ba89edf7d
13 changed files with 434 additions and 939 deletions

View File

@@ -132,6 +132,22 @@ module.exports = function() {
return this;
};
Operation.prototype._encode = function(json) {
if (json == null) {
json = {};
}
json.type = this.type;
json.uid = this.getUid();
if (this.custom_type != null) {
if (this.custom_type.constructor === String) {
json.custom_type = this.custom_type;
} else {
json.custom_type = this.custom_type._name;
}
}
return json;
};
Operation.prototype.saveOperation = function(name, op) {
if (op == null) {
@@ -258,7 +274,7 @@ module.exports = function() {
this.deleted_by = [];
}
callLater = false;
if ((this.parent != null) && !this.isDeleted() && (o != null)) {
if ((this.parent != null) && !this.is_deleted && (o != null)) {
callLater = true;
}
if (o != null) {
@@ -273,12 +289,8 @@ module.exports = function() {
this.callOperationSpecificDeleteEvents(o);
}
if ((_ref = this.prev_cl) != null ? _ref.isDeleted() : void 0) {
this.prev_cl.applyDelete();
return this.prev_cl.applyDelete();
}
if (this.content instanceof ops.Operation) {
this.content.applyDelete();
}
return delete this.content;
};
Insert.prototype.cleanup = function() {
@@ -298,6 +310,13 @@ module.exports = function() {
}
this.prev_cl.next_cl = this.next_cl;
this.next_cl.prev_cl = this.prev_cl;
if (this.content instanceof ops.Operation && !deleted_earlyer) {
this.content.referenced_by--;
if (this.content.referenced_by <= 0 && !this.content.is_deleted) {
this.content.applyDelete();
}
}
delete this.content;
return Insert.__super__.cleanup.apply(this, arguments);
}
};
@@ -317,12 +336,16 @@ module.exports = function() {
};
Insert.prototype.execute = function() {
var distance_to_origin, i, o;
var distance_to_origin, i, o, _base;
if (!this.validateSavedOperations()) {
return false;
} else {
if (this.content instanceof ops.Operation) {
this.content.insert_parent = this;
if ((_base = this.content).referenced_by == null) {
_base.referenced_by = 0;
}
this.content.referenced_by++;
}
if (this.parent != null) {
if (this.prev_cl == null) {
@@ -425,15 +448,14 @@ module.exports = function() {
return position;
};
Insert.prototype._encode = function() {
var json, _ref;
json = {
'type': this.type,
'uid': this.getUid(),
'prev': this.prev_cl.getUid(),
'next': this.next_cl.getUid(),
'parent': this.parent.getUid()
};
Insert.prototype._encode = function(json) {
var _ref;
if (json == null) {
json = {};
}
json.prev = this.prev_cl.getUid();
json.next = this.next_cl.getUid();
json.parent = this.parent.getUid();
if (this.origin.type === "Delimiter") {
json.origin = "Delimiter";
} else if (this.origin !== this.prev_cl) {
@@ -444,7 +466,7 @@ module.exports = function() {
} else {
json['content'] = JSON.stringify(this.content);
}
return json;
return Insert.__super__._encode.call(this, json);
};
return Insert;
@@ -458,38 +480,6 @@ module.exports = function() {
}
return new this(null, content, uid, prev, next, origin, parent);
};
ops.ImmutableObject = (function(_super) {
__extends(ImmutableObject, _super);
function ImmutableObject(custom_type, uid, _at_content) {
this.content = _at_content;
ImmutableObject.__super__.constructor.call(this, custom_type, uid);
}
ImmutableObject.prototype.type = "ImmutableObject";
ImmutableObject.prototype.val = function() {
return this.content;
};
ImmutableObject.prototype._encode = function() {
var json;
json = {
'type': this.type,
'uid': this.getUid(),
'content': this.content
};
return json;
};
return ImmutableObject;
})(ops.Operation);
ops.ImmutableObject.parse = function(json) {
var content, uid;
uid = json['uid'], content = json['content'];
return new this(null, uid, content);
};
ops.Delimiter = (function(_super) {
__extends(Delimiter, _super);

View File

@@ -105,20 +105,6 @@ module.exports = function() {
return this._map[property_name];
};
MapManager.prototype._encode = function() {
var json;
json = {
'type': this.type,
'uid': this.getUid()
};
if (this.custom_type.constructor === String) {
json.custom_type = this.custom_type;
} else {
json.custom_type = this.custom_type._name;
}
return json;
};
return MapManager;
})(ops.Operation);
@@ -319,20 +305,6 @@ module.exports = function() {
return this;
};
ListManager.prototype._encode = function() {
var json;
json = {
'type': this.type,
'uid': this.getUid()
};
if (this.custom_type.constructor === String) {
json.custom_type = this.custom_type;
} else {
json.custom_type = this.custom_type._name;
}
return json;
};
return ListManager;
})(ops.Operation);
@@ -407,15 +379,13 @@ module.exports = function() {
return typeof o.val === "function" ? o.val() : void 0;
};
ReplaceManager.prototype._encode = function() {
var json;
json = {
'type': this.type,
'uid': this.getUid(),
'beginning': this.beginning.getUid(),
'end': this.end.getUid()
};
return json;
ReplaceManager.prototype._encode = function(json) {
if (json == null) {
json = {};
}
json.beginning = this.beginning.getUid();
json.end = this.end.getUid();
return ReplaceManager.__super__._encode.call(this, json);
};
return ReplaceManager;
@@ -424,46 +394,13 @@ module.exports = function() {
ops.Replaceable = (function(_super) {
__extends(Replaceable, _super);
function Replaceable(custom_type, content, parent, uid, prev, next, origin, is_deleted) {
function Replaceable(custom_type, content, parent, uid, prev, next, origin) {
this.saveOperation('parent', parent);
Replaceable.__super__.constructor.call(this, custom_type, content, uid, prev, next, origin);
this.is_deleted = is_deleted;
}
Replaceable.prototype.type = "Replaceable";
Replaceable.prototype.val = function() {
if ((this.content != null) && (this.content.getCustomType != null)) {
return this.content.getCustomType();
} else {
return this.content;
}
};
Replaceable.prototype.applyDelete = function() {
var res, _base, _base1, _base2;
res = Replaceable.__super__.applyDelete.apply(this, arguments);
if (this.content != null) {
if (this.next_cl.type !== "Delimiter") {
if (typeof (_base = this.content).deleteAllObservers === "function") {
_base.deleteAllObservers();
}
}
if (typeof (_base1 = this.content).applyDelete === "function") {
_base1.applyDelete();
}
if (typeof (_base2 = this.content).dontSync === "function") {
_base2.dontSync();
}
}
this.content = null;
return res;
};
Replaceable.prototype.cleanup = function() {
return Replaceable.__super__.cleanup.apply(this, arguments);
};
Replaceable.prototype.callOperationSpecificInsertEvents = function() {
var old_value;
if (this.next_cl.type === "Delimiter" && this.prev_cl.type !== "Delimiter") {
@@ -503,39 +440,23 @@ module.exports = function() {
}
};
Replaceable.prototype._encode = function() {
var json;
json = {
'type': this.type,
'parent': this.parent.getUid(),
'prev': this.prev_cl.getUid(),
'next': this.next_cl.getUid(),
'uid': this.getUid(),
'is_deleted': this.is_deleted
};
if (this.origin.type === "Delimiter") {
json.origin = "Delimiter";
} else if (this.origin !== this.prev_cl) {
json.origin = this.origin.getUid();
Replaceable.prototype._encode = function(json) {
if (json == null) {
json = {};
}
if (this.content instanceof ops.Operation) {
json['content'] = this.content.getUid();
} else {
if ((this.content != null) && (this.content.creator != null)) {
throw new Error("You must not set creator here!");
}
json['content'] = this.content;
}
return json;
return Replaceable.__super__._encode.call(this, json);
};
return Replaceable;
})(ops.Insert);
ops.Replaceable.parse = function(json) {
var content, custom_type, is_deleted, next, origin, parent, prev, uid;
content = json['content'], parent = json['parent'], uid = json['uid'], prev = json['prev'], next = json['next'], origin = json['origin'], is_deleted = json['is_deleted'], custom_type = json['custom_type'];
return new this(custom_type, content, parent, uid, prev, next, origin, is_deleted);
var content, custom_type, next, origin, parent, prev, uid;
content = json['content'], parent = json['parent'], uid = json['uid'], prev = json['prev'], next = json['next'], origin = json['origin'], custom_type = json['custom_type'];
if (typeof content === "string") {
content = JSON.parse(content);
}
return new this(custom_type, content, parent, uid, prev, next, origin);
};
return basic_ops;
};