All files / json-crdt/nodes/const ConNode.ts

91.66% Statements 11/12
50% Branches 2/4
85.71% Functions 6/7
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70156x                         156x             324524x 324524x                                         1x       170105x           324524x     14616x           7308x   7308x     7308x      
import {type ITimestampStruct, printTs, Timestamp} from '../../../json-crdt-patch/clock';
import type {JsonNode} from '../types';
import type {Printable} from 'tree-dump/lib/types';
 
/**
 * 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<View = unknown | ITimestampStruct> implements JsonNode<View>, Printable {
  /**
   * @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(
    public readonly id: ITimestampStruct,
    public readonly val: View,
  ) {}
 
  // ----------------------------------------------------------------- JsonNode
 
  /**
   * @ignore
   */
  public children() {}
 
  /**
   * @ignore
   */
  public child() {
    return undefined;
  }
 
  /**
   * @ignore
   */
  public container(): JsonNode | undefined {
    return undefined;
  }
 
  public view(): View {
    return this.val;
  }
 
  /**
   * @ignore
   */
  public api: undefined | unknown = undefined;
 
  public name(): string {
    return 'con';
  }
 
  // ---------------------------------------------------------------- Printable
 
  public toString(tab?: string): string {
    const val = this.val;
    const valFormatted =
      val instanceof Uint8Array
        ? `Uint8Array { ${('' + val).replaceAll(',', ', ')} }`
        : `{ ${val instanceof Timestamp ? printTs(val as Timestamp) : JSON.stringify(val)} }`;
    return `${this.name()} ${printTs(this.id)} ${valFormatted}`;
  }
}