UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

377 lines (376 loc) 8.24 kB
import { printTree } from 'tree-dump/lib/printTree'; import { Timestamp, printTs } from './clock'; /** * Operation which creates a constant "con" data type. * * @category Operations */ export class NewConOp { id; val; constructor(id, val) { this.id = id; this.val = val; } span() { return 1; } name() { return 'new_con'; } toString(tab = '') { 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 `${this.name()} ${printTs(this.id)} ${valFormatted}`; } } /** * Operation which creates a new value object. * * @category Operations */ export class NewValOp { id; constructor(id) { this.id = id; } span() { return 1; } name() { return 'new_val'; } toString() { return `${this.name()} ${printTs(this.id)}`; } } /** * Operation which creates a new object. * * @category Operations */ export class NewObjOp { id; constructor(id) { this.id = id; } span() { return 1; } name() { return 'new_obj'; } toString() { return `${this.name()} ${printTs(this.id)}`; } } /** * Operation which creates a new vector object. * * @category Operations */ export class NewVecOp { id; constructor(id) { this.id = id; } span() { return 1; } name() { return 'new_vec'; } toString() { return `${this.name()} ${printTs(this.id)}`; } } /** * Operation which creates a new string object. * * @category Operations */ export class NewStrOp { id; constructor(id) { this.id = id; } span() { return 1; } name() { return 'new_str'; } toString() { return `${this.name()} ${printTs(this.id)}`; } } /** * Operation which creates a new binary object. * * @category Operations */ export class NewBinOp { id; constructor(id) { this.id = id; } span() { return 1; } name() { return 'new_bin'; } toString(tab = '') { return `${this.name()} ${printTs(this.id)}`; } } /** * Operation which creates a new array object. * * @category Operations */ export class NewArrOp { id; constructor(id) { this.id = id; } span() { return 1; } name() { return 'new_arr'; } toString() { return `${this.name()} ${printTs(this.id)}`; } } /** * Operation which writes a new value to a value "val" object. * * @category Operations */ export class InsValOp { id; obj; val; constructor(id, /** @todo Rename to `node`. */ obj, val) { this.id = id; this.obj = obj; this.val = val; } span() { return 1; } name() { return 'ins_val'; } toString(tab = '') { return `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)}, val = ${printTs(this.val)}`; } } /** * Operation which sets object keys. * * @category Operations */ export class InsObjOp { id; obj; data; constructor(id, obj, data) { this.id = id; this.obj = obj; this.data = data; } span() { return 1; } name() { return 'ins_obj'; } toString(tab = '') { const header = `${this.name()} ${printTs(this.id)}!${this.span()}, 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 { id; obj; data; constructor(id, obj, data) { this.id = id; this.obj = obj; this.data = data; } span() { return 1; } name() { return 'ins_vec'; } toString(tab = '') { const header = `${this.name()} ${printTs(this.id)}!${this.span()}, 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 { id; obj; ref; data; constructor(id, obj, ref, data) { this.id = id; this.obj = obj; this.ref = ref; this.data = data; } span() { return this.data.length; } name() { return 'ins_str'; } toString() { return `${this.name()} ${printTs(this.id)}!${this.span()}, 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 { id; obj; ref; data; constructor(id, obj, ref, data) { this.id = id; this.obj = obj; this.ref = ref; this.data = data; } span() { return this.data.length; } name() { return 'ins_bin'; } toString(tab = '') { const ref = printTs(this.ref); return `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)} { ${ref} ← ${this.data} }`; } } /** * Operation which inserts elements into an array. * * @category Operations */ export class InsArrOp { 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) { this.id = id; this.obj = obj; this.ref = ref; this.data = data; } span() { return this.data.length; } name() { return 'ins_arr'; } toString() { return `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)} { ${printTs(this.ref)} ← ${this.data.map(printTs).join(', ')} }`; } } /** * 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 { 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) { this.id = id; this.obj = obj; this.what = what; } span() { return 1; } name() { return 'del'; } toString() { const spans = this.what.map((span) => printTs(span) + '!' + span.span).join(', '); return `${this.name()} ${printTs(this.id)}, 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 { id; len; constructor(id, len) { this.id = id; this.len = len; } span() { return this.len; } name() { return 'nop'; } toString() { return `${this.name()} ${printTs(this.id)}!${this.len}`; } }