All files / json-crdt-patch operations.ts

84.21% Statements 96/114
44.44% Branches 4/9
74.46% Functions 35/47
85.45% Lines 94/110

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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389193x 193x         1405602x     1650816x           222x 222x 222x 222x                 193x   208263x 208263x   208263x       30x       24x 24x   24x             24x                 193x                     193x   16x                 193x                     193x   58x                 193x                     193x   14x                 193x   39806x 39806x 39806x   39806x       23x       22x                 193x   85325x 85325x 85325x   85325x       18x       16x 16x       44x                     193x   37378x 37378x 37378x   37378x       2x                                       193x   554504x 554504x 554504x 554504x   554504x       2030051x       61x       58x                 193x   17028x 17028x 17028x 17028x   17028x       55512x       2x                           193x                           46599x 46599x 46599x 46599x   46599x       130757x       15x       14x 14x 14x 14x                 193x               131x 131x 131x 131x   131x                                         193x             145344x 145344x 145344x   145344x       6x                             193x   4681x 4681x   4681x       5535x              
import {printTree} from 'tree-dump/lib/printTree';
import {type ITimestampStruct, type ITimespanStruct, Timestamp, printTs} from './clock';
import type {IJsonCrdtPatchEditOperation, IJsonCrdtPatchOperation} from './types';
import type {JsonCrdtPatchMnemonic} from './codec/verbose';
 
abstract class Op implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public abstract name(): JsonCrdtPatchMnemonic;
 
  public toString(): string {
    let str: string = 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 implements IJsonCrdtPatchOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly val: unknown | undefined | ITimestampStruct,
  ) {
    super(id);
  }
 
  public name() {
    return 'new_con' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchOperation {
  public name() {
    return 'new_val' as const;
  }
}
 
/**
 * Operation which creates a new object.
 *
 * @category Operations
 */
export class NewObjOp extends Op implements IJsonCrdtPatchOperation {
  public name() {
    return 'new_obj' as const;
  }
}
 
/**
 * Operation which creates a new vector object.
 *
 * @category Operations
 */
export class NewVecOp extends Op implements IJsonCrdtPatchOperation {
  public name() {
    return 'new_vec' as const;
  }
}
 
/**
 * Operation which creates a new string object.
 *
 * @category Operations
 */
export class NewStrOp extends Op implements IJsonCrdtPatchOperation {
  public name() {
    return 'new_str' as const;
  }
}
 
/**
 * Operation which creates a new binary object.
 *
 * @category Operations
 */
export class NewBinOp extends Op implements IJsonCrdtPatchOperation {
  public name() {
    return 'new_bin' as const;
  }
}
 
/**
 * Operation which creates a new array object.
 *
 * @category Operations
 */
export class NewArrOp extends Op implements IJsonCrdtPatchOperation {
  public name() {
    return 'new_arr' as const;
  }
}
 
/**
 * Operation which writes a new value to a value "val" object.
 *
 * @category Operations
 */
export class InsValOp extends Op implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly val: ITimestampStruct,
  ) {
    super(id);
  }
 
  public name() {
    return 'ins_val' as const;
  }
 
  public toString(): string {
    return super.toString() + `, obj = ${printTs(this.obj)}, val = ${printTs(this.val)}`;
  }
}
 
/**
 * Operation which sets object keys.
 *
 * @category Operations
 */
export class InsObjOp extends Op implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly data: [key: string, value: ITimestampStruct][],
  ) {
    super(id);
  }
 
  public name() {
    return 'ins_obj' as const;
  }
 
  public toString(tab: string = ''): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly data: [key: number, value: ITimestampStruct][],
  ) {
    super(id);
  }
 
  public name() {
    return 'ins_vec' as const;
  }
 
  public toString(tab: string = ''): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly ref: ITimestampStruct,
    public data: string,
  ) {
    super(id);
  }
 
  public span(): number {
    return this.data.length;
  }
 
  public name() {
    return 'ins_str' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly ref: ITimestampStruct,
    public readonly data: Uint8Array,
  ) {
    super(id);
  }
 
  public span(): number {
    return this.data.length;
  }
 
  public name() {
    return 'ins_bin' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchEditOperation {
  /**
   * @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(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly ref: ITimestampStruct,
    public readonly data: ITimestampStruct[],
  ) {
    super(id);
  }
 
  public span(): number {
    return this.data.length;
  }
 
  public name() {
    return 'ins_arr' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchEditOperation {
  /**
   * @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(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly ref: ITimestampStruct,
    public readonly val: ITimestampStruct,
  ) {
    super(id);
  }
 
  public name() {
    return 'upd_arr' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchEditOperation {
  /**
   * @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(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly what: ITimespanStruct[],
  ) {
    super(id);
  }
 
  public name() {
    return 'del' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly len: number,
  ) {
    super(id);
  }
 
  public span(): number {
    return this.len;
  }
 
  public name() {
    return 'nop' as const;
  }
}