All files / json-crdt-patch Patch.ts

94.18% Statements 81/86
85.71% Branches 36/42
100% Functions 16/16
98.52% Lines 67/68

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227174x 174x 174x 174x                                                                                                   174x         44702x           111377x           111377x                 24921x 24921x 24918x                   13851x 22404x 13851x             2158301x 2158301x 2158301x 2049295x 2049295x                     8x 8x 8x 8x 8x 39x 39x 39x 9x 30x 29x 27x 25x 22x 21x 20x 4x 16x 5x 11x 1x 10x 6x 4x 3x       10x     1x 1x       2x         8x                               6x 6x 6x 6x 6x 6x 6x 6x 74x 66x 66x 62x                   20x                 62x                         26x 26x 26x       222x          
import * as operations from './operations';
import {type ITimestampStruct, ts, printTs, Timestamp} from './clock';
import {printTree} from 'tree-dump/lib/printTree';
import {encode, decode} from './codec/binary';
import type {Printable} from 'tree-dump/lib/types';
 
/**
 * A union type of all possible JSON CRDT patch operations.
 */
export type JsonCrdtPatchOperation =
  | operations.NewConOp
  | operations.NewValOp
  | operations.NewVecOp
  | operations.NewObjOp
  | operations.NewStrOp
  | operations.NewBinOp
  | operations.NewArrOp
  | operations.InsValOp
  | operations.InsObjOp
  | operations.InsVecOp
  | operations.InsStrOp
  | operations.InsBinOp
  | operations.InsArrOp
  | operations.DelOp
  | operations.NopOp;
 
/**
 * Represents a JSON CRDT patch.
 *
 * Normally, you would create a new patch using the {@link PatchBuilder} class.
 *
 * ```ts
 * import {Patch, PatchBuilder, LogicalClock} from 'json-joy/lib/json-crdt-patch';
 *
 * const clock = new LogicalClock(3, 100);
 * const builder = new PatchBuilder(clock);
 * const patch = builder.flush();
 * ```
 *
 * Save patch to a binary representation:
 *
 * ```ts
 * const binary = patch.toBinary();
 * ```
 *
 * Load patch from a binary representation:
 *
 * ```ts
 * const patch = Patch.fromBinary(binary);
 * ```
 *
 * @category Patch
 */
export class Patch implements Printable {
  /**
   * Un-marshals a JSON CRDT patch from a binary representation.
   */
  public static fromBinary(data: Uint8Array): Patch {
    return decode(data);
  }
 
  /**
   * A list of operations in the patch.
   */
  public ops: JsonCrdtPatchOperation[] = [];
 
  /**
   * Arbitrary metadata associated with the patch, which is not used by the
   * library.
   */
  public meta: unknown = undefined;
 
  /**
   * Returns the patch ID, which is equal to the ID of the first operation
   * in the patch.
   *
   * @returns The ID of the first operation in the patch.
   */
  public getId(): ITimestampStruct | undefined {
    const op = this.ops[0];
    if (!op) return undefined;
    return op.id;
  }
 
  /**
   * Returns the total time span of the patch, which is the sum of all
   * operation spans.
   *
   * @returns The length of the patch.
   */
  public span(): number {
    let span = 0;
    for (const op of this.ops) span += op.span();
    return span;
  }
 
  /**
   * Returns the expected time of the next inserted operation.
   */
  public nextTime(): number {
    const ops = this.ops;
    const length = ops.length;
    if (!length) return 0;
    const lastOp = ops[length - 1];
    return lastOp.id.time + lastOp.span();
  }
 
  /**
   * Creates a new patch where all timestamps are transformed using the
   * provided function.
   *
   * @param ts Timestamp transformation function.
   * @returns A new patch with transformed timestamps.
   */
  public rewriteTime(ts: (id: ITimestampStruct) => ITimestampStruct): Patch {
    const patch = new Patch();
    const ops = this.ops;
    const length = ops.length;
    const patchOps = patch.ops;
    for (let i = 0; i < length; i++) {
      const op = ops[i];
      Iif (op instanceof operations.DelOp) patchOps.push(new operations.DelOp(ts(op.id), ts(op.obj), op.what));
      else if (op instanceof operations.NewConOp)
        patchOps.push(new operations.NewConOp(ts(op.id), op.val instanceof Timestamp ? ts(op.val) : op.val));
      else if (op instanceof operations.NewVecOp) patchOps.push(new operations.NewVecOp(ts(op.id)));
      else if (op instanceof operations.NewValOp) patchOps.push(new operations.NewValOp(ts(op.id)));
      else if (op instanceof operations.NewObjOp) patchOps.push(new operations.NewObjOp(ts(op.id)));
      else if (op instanceof operations.NewStrOp) patchOps.push(new operations.NewStrOp(ts(op.id)));
      else if (op instanceof operations.NewBinOp) patchOps.push(new operations.NewBinOp(ts(op.id)));
      else if (op instanceof operations.NewArrOp) patchOps.push(new operations.NewArrOp(ts(op.id)));
      else if (op instanceof operations.InsArrOp)
        patchOps.push(new operations.InsArrOp(ts(op.id), ts(op.obj), ts(op.ref), op.data.map(ts)));
      else if (op instanceof operations.InsStrOp)
        patchOps.push(new operations.InsStrOp(ts(op.id), ts(op.obj), ts(op.ref), op.data));
      else if (op instanceof operations.InsBinOp)
        patchOps.push(new operations.InsBinOp(ts(op.id), ts(op.obj), ts(op.ref), op.data));
      else if (op instanceof operations.InsValOp)
        patchOps.push(new operations.InsValOp(ts(op.id), ts(op.obj), ts(op.val)));
      else if (op instanceof operations.InsObjOp)
        patchOps.push(
          new operations.InsObjOp(
            ts(op.id),
            ts(op.obj),
            op.data.map(([key, value]) => [key, ts(value)]),
          ),
        );
      else if (op instanceof operations.InsVecOp)
        patchOps.push(
          new operations.InsVecOp(
            ts(op.id),
            ts(op.obj),
            op.data.map(([key, value]) => [key, ts(value)]),
          ),
        );
      else IEif (op instanceof operations.NopOp) patchOps.push(new operations.NopOp(ts(op.id), op.len));
    }
    return patch;
  }
 
  /**
   * The `.rebase()` operation is meant to be applied to patches which have not
   * yet been advertised to the server (other peers), or when
   * the server clock is used and concurrent change on the server happened.
   *
   * The .rebase() operation returns a new `Patch` with the IDs recalculated
   * such that the first operation has the `time` equal to `newTime`.
   *
   * @param newTime Time where the patch ID should begin (ID of the first operation).
   * @param transformAfter Time after (and including) which the IDs should be
   *     transformed. If not specified, equals to the time of the first operation.
   */
  public rebase(newTime: number, transformAfter?: number): Patch {
    const id = this.getId();
    Iif (!id) throw new Error('EMPTY_PATCH');
    const sid = id.sid;
    const patchStartTime = id.time;
    transformAfter ??= patchStartTime;
    Iif (patchStartTime === newTime) return this;
    const delta = newTime - patchStartTime;
    return this.rewriteTime((id: ITimestampStruct): ITimestampStruct => {
      if (id.sid !== sid) return id;
      const time = id.time;
      if (time < transformAfter) return id;
      return ts(sid, time + delta);
    });
  }
 
  /**
   * Creates a deep clone of the patch.
   *
   * @returns A deep clone of the patch.
   */
  public clone(): Patch {
    return this.rewriteTime((id) => id);
  }
 
  /**
   * Marshals the patch into a binary representation.
   *
   * @returns A binary representation of the patch.
   */
  public toBinary() {
    return encode(this);
  }
 
  // ---------------------------------------------------------------- Printable
 
  /**
   * Returns a textual human-readable representation of the patch. This can be
   * used for debugging purposes.
   *
   * @param tab Start string for each line.
   * @returns Text representation of the patch.
   */
  public toString(tab: string = ''): string {
    const id = this.getId();
    const header = `Patch ${id ? printTs(id) : '(nil)'}!${this.span()}`;
    return (
      header +
      printTree(
        tab,
        this.ops.map((op) => (tab) => op.toString(tab)),
      )
    );
  }
}