All files / json-crdt-patch operations.ts

81.48% Statements 88/108
25% Branches 3/12
73.84% Functions 48/65
82.85% Lines 87/105

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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406174x 174x               174x   133810x 133810x       280531x       24x       24x 24x   24x             24x                 174x 7595x     11073x                                 174x 64087x     120270x       16x       16x                 174x 23701x     47385x                                 174x 52094x     95133x       58x       58x                 174x 6778x     13386x                                 174x 44880x     73778x       14x       14x                 174x   24193x   24193x 24193x       37768x       22x       22x                 174x   73346x 73346x 73346x       213691x       16x       16x 16x       44x                     174x   31647x 31647x 31647x       86973x                                               174x   521015x 521015x 521015x 521015x       1927000x       58x       58x                     174x   17410x 17410x 17410x 17410x       57670x                                   174x                           37617x 37617x 37617x 37617x       104007x       14x       14x                       174x             144427x 144427x 144427x       311685x                                     174x   4273x 4273x       4841x                      
import {printTree} from 'tree-dump/lib/printTree';
import {type ITimestampStruct, type ITimespanStruct, Timestamp, printTs} from './clock';
import type {IJsonCrdtPatchEditOperation, IJsonCrdtPatchOperation} from './types';
 
/**
 * Operation which creates a constant "con" data type.
 *
 * @category Operations
 */
export class NewConOp implements IJsonCrdtPatchOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly val: unknown | undefined | ITimestampStruct,
  ) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_con' as const;
  }
 
  public toString(tab: string = ''): 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 `${this.name()} ${printTs(this.id)} ${valFormatted}`;
  }
}
 
/**
 * Operation which creates a new value object.
 *
 * @category Operations
 */
export class NewValOp implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_val' as const;
  }
 
  public toString(): string {
    return `${this.name()} ${printTs(this.id)}`;
  }
}
 
/**
 * Operation which creates a new object.
 *
 * @category Operations
 */
export class NewObjOp implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_obj' as const;
  }
 
  public toString(): string {
    return `${this.name()} ${printTs(this.id)}`;
  }
}
 
/**
 * Operation which creates a new vector object.
 *
 * @category Operations
 */
export class NewVecOp implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_vec' as const;
  }
 
  public toString(): string {
    return `${this.name()} ${printTs(this.id)}`;
  }
}
 
/**
 * Operation which creates a new string object.
 *
 * @category Operations
 */
export class NewStrOp implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_str' as const;
  }
 
  public toString(): string {
    return `${this.name()} ${printTs(this.id)}`;
  }
}
 
/**
 * Operation which creates a new binary object.
 *
 * @category Operations
 */
export class NewBinOp implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_bin' as const;
  }
 
  public toString(tab: string = ''): string {
    return `${this.name()} ${printTs(this.id)}`;
  }
}
 
/**
 * Operation which creates a new array object.
 *
 * @category Operations
 */
export class NewArrOp implements IJsonCrdtPatchOperation {
  constructor(public readonly id: ITimestampStruct) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'new_arr' as const;
  }
 
  public toString(): string {
    return `${this.name()} ${printTs(this.id)}`;
  }
}
 
/**
 * Operation which writes a new value to a value "val" object.
 *
 * @category Operations
 */
export class InsValOp implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    /** @todo Rename to `node`. */
    public readonly obj: ITimestampStruct,
    public readonly val: ITimestampStruct,
  ) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'ins_val' as const;
  }
 
  public toString(tab: string = ''): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly data: [key: string, value: ITimestampStruct][],
  ) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'ins_obj' as const;
  }
 
  public toString(tab: string = ''): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly data: [key: number, value: ITimestampStruct][],
  ) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'ins_vec' as const;
  }
 
  public toString(tab: string = ''): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly ref: ITimestampStruct,
    public data: string,
  ) {}
 
  public span(): number {
    return this.data.length;
  }
 
  public name() {
    return 'ins_str' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchEditOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly obj: ITimestampStruct,
    public readonly ref: ITimestampStruct,
    public readonly data: Uint8Array,
  ) {}
 
  public span(): number {
    return this.data.length;
  }
 
  public name() {
    return 'ins_bin' as const;
  }
 
  public toString(tab: string = ''): string {
    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 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[],
  ) {}
 
  public span(): number {
    return this.data.length;
  }
 
  public name() {
    return 'ins_arr' as const;
  }
 
  public toString(): string {
    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 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[],
  ) {}
 
  public span(): number {
    return 1;
  }
 
  public name() {
    return 'del' as const;
  }
 
  public toString(): string {
    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 implements IJsonCrdtPatchOperation {
  constructor(
    public readonly id: ITimestampStruct,
    public readonly len: number,
  ) {}
 
  public span(): number {
    return this.len;
  }
 
  public name() {
    return 'nop' as const;
  }
 
  public toString(): string {
    return `${this.name()} ${printTs(this.id)}!${this.len}`;
  }
}