json-joy
Version:
Collection of libraries for building collaborative editing apps.
358 lines • 8.23 kB
JavaScript
import { printTree } from 'tree-dump/lib/printTree';
import { Timestamp, printTs } from './clock';
class Op {
id;
constructor(id) {
this.id = id;
}
span() {
return 1;
}
toString() {
let str = this.name() + ' ' + printTs(this.id);
const span = this.span();
if (span > 1)
str += '!' + span;
return str;
}
}
/**
* Operation which creates a constant "con" data type.
*
* @category Operations
*/
export class NewConOp extends Op {
id;
val;
constructor(id, val) {
super(id);
this.id = id;
this.val = val;
}
name() {
return 'new_con';
}
toString() {
const val = this.val;
const klass = 'Uint8Array';
const valFormatted = val instanceof Timestamp
? `{ ${printTs(val)} }`
: val instanceof Uint8Array
? val.length < 13
? `${klass} { ${('' + val).replaceAll(',', ', ')} }`
: `${klass}(${val.length})`
: `{ ${JSON.stringify(val)} }`;
return super.toString() + ' ' + valFormatted;
}
}
/**
* Operation which creates a new value object.
*
* @category Operations
*/
export class NewValOp extends Op {
name() {
return 'new_val';
}
}
/**
* Operation which creates a new object.
*
* @category Operations
*/
export class NewObjOp extends Op {
name() {
return 'new_obj';
}
}
/**
* Operation which creates a new vector object.
*
* @category Operations
*/
export class NewVecOp extends Op {
name() {
return 'new_vec';
}
}
/**
* Operation which creates a new string object.
*
* @category Operations
*/
export class NewStrOp extends Op {
name() {
return 'new_str';
}
}
/**
* Operation which creates a new binary object.
*
* @category Operations
*/
export class NewBinOp extends Op {
name() {
return 'new_bin';
}
}
/**
* Operation which creates a new array object.
*
* @category Operations
*/
export class NewArrOp extends Op {
name() {
return 'new_arr';
}
}
/**
* Operation which writes a new value to a value "val" object.
*
* @category Operations
*/
export class InsValOp extends Op {
id;
obj;
val;
constructor(id, obj, val) {
super(id);
this.id = id;
this.obj = obj;
this.val = val;
}
name() {
return 'ins_val';
}
toString() {
return super.toString() + `, obj = ${printTs(this.obj)}, val = ${printTs(this.val)}`;
}
}
/**
* Operation which sets object keys.
*
* @category Operations
*/
export class InsObjOp extends Op {
id;
obj;
data;
constructor(id, obj, data) {
super(id);
this.id = id;
this.obj = obj;
this.data = data;
}
name() {
return 'ins_obj';
}
toString(tab = '') {
const header = super.toString() + `, obj = ${printTs(this.obj)}`;
return (header +
printTree(tab, this.data.map((item) => (tab) => `${JSON.stringify(item[0])}: ${printTs(item[1])}`)));
}
}
/**
* Operation which sets vector elements.
*
* @category Operations
*/
export class InsVecOp extends Op {
id;
obj;
data;
constructor(id, obj, data) {
super(id);
this.id = id;
this.obj = obj;
this.data = data;
}
name() {
return 'ins_vec';
}
toString(tab = '') {
const header = super.toString() + `, obj = ${printTs(this.obj)}`;
return (header +
printTree(tab, this.data.map((item) => (tab) => `${item[0]}: ${printTs(item[1])}`)));
}
}
/**
* Operation which inserts text into a "str" string object.
*
* @category Operations
*/
export class InsStrOp extends Op {
id;
obj;
ref;
data;
constructor(id, obj, ref, data) {
super(id);
this.id = id;
this.obj = obj;
this.ref = ref;
this.data = data;
}
span() {
return this.data.length;
}
name() {
return 'ins_str';
}
toString() {
return super.toString() + `, obj = ${printTs(this.obj)} { ${printTs(this.ref)} ← ${JSON.stringify(this.data)} }`;
}
}
/**
* Operations which inserts binary data into a "bin" binary object.
*
* @category Operations
*/
export class InsBinOp extends Op {
id;
obj;
ref;
data;
constructor(id, obj, ref, data) {
super(id);
this.id = id;
this.obj = obj;
this.ref = ref;
this.data = data;
}
span() {
return this.data.length;
}
name() {
return 'ins_bin';
}
toString() {
const ref = printTs(this.ref);
return super.toString() + `, obj = ${printTs(this.obj)} { ${ref} ← ${this.data} }`;
}
}
/**
* Operation which inserts elements into an array.
*
* @category Operations
*/
export class InsArrOp extends Op {
id;
obj;
ref;
data;
/**
* @param id ID if the first operation in this compound operation.
* @param obj ID of the array where to insert elements. In theory `arr` is
* not necessary as it is possible to find the `arr` just using the
* `after` property, however to efficiently be able to find `arr` just
* by `after` at runtime all operations would need to be indexed and
* also they each would need to store a pointer to array type, which
* would require additional dozens of bytes of RAM for each array
* insert operation.
* @param ref ID of the element after which to insert elements.
* @param data The elements to insert.
*/
constructor(id, obj, ref, data) {
super(id);
this.id = id;
this.obj = obj;
this.ref = ref;
this.data = data;
}
span() {
return this.data.length;
}
name() {
return 'ins_arr';
}
toString() {
const obj = printTs(this.obj);
const ref = printTs(this.ref);
const data = this.data.map(printTs).join(', ');
return super.toString() + ', obj = ' + obj + ' { ' + ref + ' ← ' + data + ' }';
}
}
/**
* Operation which updates an existing element in an array.
*
* @category Operations
*/
export class UpdArrOp extends Op {
id;
obj;
ref;
val;
/**
* @param id ID of this operation.
* @param obj and "arr" object ID where to update an element.
* @param ref ID of the element to update.
* @param val ID of the new value to set.
*/
constructor(id, obj, ref, val) {
super(id);
this.id = id;
this.obj = obj;
this.ref = ref;
this.val = val;
}
name() {
return 'upd_arr';
}
toString() {
const obj = printTs(this.obj);
const ref = printTs(this.ref);
const val = printTs(this.val);
return super.toString() + ', obj = ' + obj + ' { ' + ref + ': ' + val + ' }';
}
}
/**
* Operation which deletes one or more ranges of values in some object.
* The object could be a string, an array, or a binary.
*
* @category Operations
*/
export class DelOp extends Op {
id;
obj;
what;
/**
* @param id ID of this operation.
* @param obj Object in which to delete something.
* @param what ID of the first operation to be deleted.
*/
constructor(id, obj, what) {
super(id);
this.id = id;
this.obj = obj;
this.what = what;
}
name() {
return 'del';
}
toString() {
const spans = this.what.map((span) => printTs(span) + '!' + span.span).join(', ');
return super.toString() + `, obj = ${printTs(this.obj)} { ${spans} }`;
}
}
/**
* Operation which does nothing. Useful for skipping clock cycles, so that
* operations with a gap in clock can be included in the same patch.
*
* @category Operations
*/
export class NopOp extends Op {
id;
len;
constructor(id, len) {
super(id);
this.id = id;
this.len = len;
}
span() {
return this.len;
}
name() {
return 'nop';
}
}
//# sourceMappingURL=operations.js.map