mute-structs
Version:
NodeJS module providing an implementation of the LogootSplit CRDT algorithm
128 lines (124 loc) • 4.77 kB
JavaScript
/*
This file is part of MUTE-structs.
Copyright (C) 2017 Matthieu Nicolas, Victorien Elvinger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { isObject } from "../../data-validation";
import { IdentifierInterval } from "../../identifierinterval";
import { isInt32 } from "../../int32";
import { LogootSOperation } from "../logootsoperation";
var arrayConcat = Array.prototype.concat;
var LogootSDelV1 = /** @class */ (function () {
function LogootSDelV1() {
}
LogootSDelV1.fromPlain = function (o) {
if (isObject(o) &&
Array.isArray(o.lid) && o.lid.length > 0) {
var isOk = true;
var i = 0;
var lid = [];
while (isOk && i < o.lid.length) {
var idi = IdentifierInterval.fromPlain(o.lid[i]);
if (idi !== null) {
lid.push(idi);
}
else {
isOk = false;
}
i++;
}
if (isOk) {
return new LogootSDel(lid, -1);
}
}
return null;
};
return LogootSDelV1;
}());
/**
* Represents a LogootSplit delete operation.
*/
var LogootSDel = /** @class */ (function (_super) {
__extends(LogootSDel, _super);
/**
* @constructor
* @param {IdentifierInterval[]} lid - the list of identifier that localise the deletion in the logoot sequence.
* @param {number} author - the author of the operation.
*/
function LogootSDel(lid, author) {
var _this = this;
console.assert(lid.length > 0, "lid must not be empty");
console.assert(isInt32(author), "author ∈ int32");
_this = _super.call(this) || this;
_this.lid = lid;
_this.author = author;
return _this;
}
LogootSDel.fromPlain = function (o) {
if (isObject(o) &&
Array.isArray(o.lid) && o.lid.length > 0 && isInt32(o.author)) {
var isOk = true;
var i = 0;
var lid = [];
while (isOk && i < o.lid.length) {
var idi = IdentifierInterval.fromPlain(o.lid[i]);
if (idi !== null) {
lid.push(idi);
}
else {
isOk = false;
}
i++;
}
if (isOk) {
return new LogootSDel(lid, o.author);
}
}
// For backward compatibility
// Allow to replay and update previous log of operations
return LogootSDelV1.fromPlain(o);
};
LogootSDel.prototype.equals = function (aOther) {
return (this.lid.length === aOther.lid.length &&
this.lid.every(function (idInterval, index) {
var otherIdInterval = aOther.lid[index];
return idInterval.equals(otherIdInterval);
}));
};
/**
* Apply the current delete operation to a LogootSplit document.
* @param {LogootSRopes} doc - the LogootSplit document on which the deletions wil be performed.
* @return {TextDelete[]} the list of deletions to be applied on the sequence representing the document content.
*/
LogootSDel.prototype.execute = function (doc) {
var _this = this;
return arrayConcat.apply([], this.lid.map(function (aId) {
return doc.delBlock(aId, _this.author);
}));
};
return LogootSDel;
}(LogootSOperation));
export { LogootSDel };
//# sourceMappingURL=logootsdel.js.map