json-joy
Version:
Collection of libraries for building collaborative editing apps.
59 lines (58 loc) • 1.55 kB
JavaScript
import { printTs, Timestamp } from '../../../json-crdt-patch/clock';
/**
* Represents the `con` type of the JSON CRDT specification.
*
* Constant type represents an immutable JSON value. It can be any JSON/CBOR
* value including deeply nested objects and arrays, Uint8Array binary data, or
* it can store a logical timestamp. The constant value cannot be edited.
*
* @category CRDT Node
*/
export class ConNode {
id;
val;
/**
* @param id ID of the CRDT node.
* @param val Raw value of the constant. It can be any JSON/CBOR value, or
* a logical timestamp {@link Timestamp}.
*/
constructor(id, val) {
this.id = id;
this.val = val;
}
// ----------------------------------------------------------------- JsonNode
/**
* @ignore
*/
children() { }
/**
* @ignore
*/
child() {
return undefined;
}
/**
* @ignore
*/
container() {
return undefined;
}
view() {
return this.val;
}
/**
* @ignore
*/
api = undefined;
name() {
return 'con';
}
// ---------------------------------------------------------------- Printable
toString(tab) {
const val = this.val;
const valFormatted = val instanceof Uint8Array
? `Uint8Array { ${('' + val).replaceAll(',', ', ')} }`
: `{ ${val instanceof Timestamp ? printTs(val) : JSON.stringify(val)} }`;
return `${this.name()} ${printTs(this.id)} ${valFormatted}`;
}
}