UNPKG

mute-structs

Version:

NodeJS module providing an implementation of the LogootSplit CRDT algorithm

111 lines (107 loc) 4.26 kB
/* 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 { Identifier } from "../../identifier"; import { IdentifierInterval } from "../../identifierinterval"; import { LogootSOperation } from "../logootsoperation"; var LogootSAddV1 = /** @class */ (function () { function LogootSAddV1() { } LogootSAddV1.fromPlain = function (o) { if (isObject(o) && typeof o.l === "string" && o.l.length > 0) { var id = Identifier.fromPlain(o.id); if (id !== null) { return new LogootSAdd(id, o.l); } } return null; }; return LogootSAddV1; }()); /** * Represents a LogootSplit insert operation. */ var LogootSAdd = /** @class */ (function (_super) { __extends(LogootSAdd, _super); /** * @constructor * @param {Identifier} id - the identifier that localise the insertion in the logoot sequence. * @param {string} content - the content of the block to be inserted. */ function LogootSAdd(id, content) { var _this = this; console.assert(content.length > 0, "content must not be empty"); _this = _super.call(this) || this; _this.id = id; _this.content = content; return _this; } LogootSAdd.fromPlain = function (o) { if (isObject(o) && typeof o.content === "string" && o.content.length > 0) { var id = Identifier.fromPlain(o.id); if (id !== null) { return new LogootSAdd(id, o.content); } } // For backward compatibility // Allow to replay and update previous log of operations return LogootSAddV1.fromPlain(o); }; Object.defineProperty(LogootSAdd.prototype, "author", { get: function () { return this.id.replicaNumber; }, enumerable: true, configurable: true }); Object.defineProperty(LogootSAdd.prototype, "insertedIds", { get: function () { var insertedIdInterval = new IdentifierInterval(this.id, this.id.lastOffset + this.content.length - 1); return insertedIdInterval.toIds(); }, enumerable: true, configurable: true }); LogootSAdd.prototype.equals = function (aOther) { return this.id.equals(aOther.id) && this.content === aOther.content; }; /** * Apply the current insert operation to a LogootSplit document. * @param {LogootSRopes} doc - the LogootSplit document on which the operation wil be applied. * @return {TextInsert[]} the insertion to be applied on the sequence representing the document content. */ LogootSAdd.prototype.execute = function (doc) { return doc.addBlock(this.content, this.id); }; return LogootSAdd; }(LogootSOperation)); export { LogootSAdd }; //# sourceMappingURL=logootsadd.js.map