mute-structs
Version:
NodeJS module providing an implementation of the LogootSplit CRDT algorithm
82 lines (78 loc) • 3.02 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/>.
*/
import { isObject } from "../../data-validation";
import { Identifier } from "../../identifier";
import { IdentifierInterval } from "../../identifierinterval";
import { LogootSOperation } from "../logootsoperation";
class LogootSAddV1 {
static fromPlain(o) {
if (isObject(o) &&
typeof o.l === "string" && o.l.length > 0) {
const id = Identifier.fromPlain(o.id);
if (id !== null) {
return new LogootSAdd(id, o.l);
}
}
return null;
}
}
/**
* Represents a LogootSplit insert operation.
*/
export class LogootSAdd extends LogootSOperation {
/**
* @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.
*/
constructor(id, content) {
console.assert(content.length > 0, "content must not be empty");
super();
this.id = id;
this.content = content;
}
static fromPlain(o) {
if (isObject(o) &&
typeof o.content === "string" && o.content.length > 0) {
const 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);
}
get author() {
return this.id.replicaNumber;
}
get insertedIds() {
const insertedIdInterval = new IdentifierInterval(this.id, this.id.lastOffset + this.content.length - 1);
return insertedIdInterval.toIds();
}
equals(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.
*/
execute(doc) {
return doc.addBlock(this.content, this.id);
}
}
//# sourceMappingURL=logootsadd.js.map