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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | 156x 156x 156x 156x 156x 156x 156x 24390x 24390x 24390x 595x 595x 14337x 14337x 7290x 7290x 7290x 7290x 7047x 7047x 7047x 14182x 14182x 28x 5232x 8x 1484x 24x 75x 8x 3513x 3513x 3513x 3513x 3513x 3513x 3513x 288x 24x 5229x 8x 1484x 21x 75x 8x 3846x 156x 9x 6x 3x 156x 7011x 11x 11x 11x 11x 11x 3280x 3280x 3x 1x 3277x 3277x 3280x 156x 7957x 7957x 7957x 31705x 7957x 3x 6x 4x 3233x 3233x 3226x 3219x 3233x 156x 6x 69x 69x 69x 77x 69x 11x 11x 11x 13x 11x 3312x 3316x 3308x 3304x 3304x 3304x 3304x 3312x 156x 426716x 426716x 426716x 426716x 426716x 426716x 426716x 426716x 426716x 426716x 109855x 109855x 109855x 109855x 109855x 109855x 109855x 109855x 109855x 19x 19x 19x 19x 19x 17x 17x 19x 19x 19x 17x 17x 17x 17x 52x 16x 15x 1x 156x 9x 9x 9x 9x 9x 5x 5x 5x 5x 5x 1x 3x 2x 1x 156x 39x 39x 39x 39x 39x 54x 39x 39x 22x 22x 22x 22x 22x 12x 11x 11x 9x 8x 8x 8x 8x 8x 11x | import {printTree} from 'tree-dump/lib/printTree'; import {find} from './find'; import {type ITimestampStruct, Timestamp} from '../../../json-crdt-patch/clock'; import type {Path} from '@jsonjoy.com/json-pointer'; import {ObjNode, ArrNode, BinNode, ConNode, VecNode, ValNode, StrNode, RootNode} from '../../nodes'; import {NodeEvents} from './NodeEvents'; import {ExtNode} from '../../extensions/ExtNode'; import type {Extension} from '../../extensions/Extension'; import type {ExtApi} from '../../extensions/types'; import type {JsonNode, JsonNodeView} from '../../nodes'; import type * as types from './proxy'; import type {ModelApi} from './ModelApi'; import type {Printable} from 'tree-dump/lib/types'; import type {JsonNodeApi} from './types'; import type {VecNodeExtensionData} from '../../schema/types'; export type ApiPath = string | number | Path | undefined; /** * A generic local changes API for a JSON CRDT node. * * @category Local API */ export class NodeApi<N extends JsonNode = JsonNode> implements Printable { constructor( public node: N, public readonly api: ModelApi<any>, ) {} /** @ignore */ private ev: undefined | NodeEvents<N> = undefined; /** * Event target for listening to node changes. You can subscribe to `"view"` * events, which are triggered every time the node's view changes. * * ```ts * node.events.on('view', () => { * // do something... * }); * ``` */ public get events(): NodeEvents<N> { const et = this.ev; return et || (this.ev = new NodeEvents<N>(this)); } /** * Find a child node at the given path starting from this node. * * @param path Path to the child node to find. * @returns JSON CRDT node at the given path. */ public find(path?: ApiPath): JsonNode { const node = this.node; if (path === undefined) { if (typeof node.child === 'function') { const child = node.child(); Iif (!child) { Iif (node instanceof RootNode) return node; throw new Error('NO_CHILD'); } return child; } throw new Error('CANNOT_IN'); } if (typeof path === 'string' && !!path && path[0] !== '/') path = '/' + path; if (typeof path === 'number') path = [path]; return find(this.node, path); } /** * Find a child node at the given path starting from this node and wrap it in * a local changes API. * * @param path Path to the child node to find. * @returns Local changes API for the child node at the given path. */ public in(path?: ApiPath) { const node = this.find(path); return this.api.wrap(node as any); } public asVal(): ValApi { if (this.node instanceof ValNode) return this.api.wrap(this.node as ValNode); throw new Error('NOT_VAL'); } public asStr(): StrApi { if (this.node instanceof StrNode) return this.api.wrap(this.node); throw new Error('NOT_STR'); } public asBin(): BinApi { if (this.node instanceof BinNode) return this.api.wrap(this.node); throw new Error('NOT_BIN'); } public asArr(): ArrApi { if (this.node instanceof ArrNode) return this.api.wrap(this.node); throw new Error('NOT_ARR'); } public asVec(): VecApi { if (this.node instanceof VecNode) return this.api.wrap(this.node as VecNode); throw new Error('NOT_ARR'); } public asObj(): ObjApi { if (this.node instanceof ObjNode) return this.api.wrap(this.node as ObjNode); throw new Error('NOT_OBJ'); } public asCon(): ConApi { if (this.node instanceof ConNode) return this.api.wrap(this.node); throw new Error('NOT_CONST'); } /** * Returns the API object of the extension if the node is an extension node. * When the `ext` parameter is provided, it checks if the node is an instance * of the given extension and returns the object's TypeScript type. Otherwise, * it returns the API object of the extension, but without any type checking. * * @param ext Extension of the node * @returns API of the extension */ public asExt(): JsonNodeApi<VecNodeExtensionData<N>> | ExtApi<any> | undefined; public asExt<EN extends ExtNode<any, any>, EApi extends ExtApi<EN>>( ext: Extension<any, any, EN, EApi, any, any>, ): EApi; public asExt<EN extends ExtNode<any, any>, EApi extends ExtApi<EN>>( ext?: Extension<any, any, EN, EApi, any, any>, ): EApi { let extNode: ExtNode<any> | undefined = undefined; const node: JsonNode | undefined = this.node; Iif (node instanceof ExtNode) extNode = node; if (node instanceof VecNode) extNode = node.ext(); Iif (!extNode) throw new Error('NOT_EXT'); const api = this.api.wrap(extNode); if (!ext) return api as any; if (api instanceof ext.Api) return api; throw new Error('NOT_EXT'); } public val(path?: ApiPath): ValApi { return this.in(path).asVal(); } public str(path?: ApiPath): StrApi { return this.in(path).asStr(); } public bin(path?: ApiPath): BinApi { return this.in(path).asBin(); } public arr(path?: ApiPath): ArrApi { return this.in(path).asArr(); } public vec(path?: ApiPath): VecApi { return this.in(path).asVec(); } public obj(path?: ApiPath): ObjApi { return this.in(path).asObj(); } public con(path?: ApiPath): ConApi { return this.in(path).asCon(); } public view(): JsonNodeView<N> { return this.node.view() as unknown as JsonNodeView<N>; } public proxy(): types.ProxyNode<N> { return { toApi: () => <any>this, toView: () => this.node.view() as any, }; } public toString(tab: string = ''): string { return 'api' + printTree(tab, [(tab) => this.node.toString(tab)]); } } /** * Represents the local changes API for the `con` JSON CRDT node {@link ConNode}. * * @category Local API */ export class ConApi<N extends ConNode<any> = ConNode<any>> extends NodeApi<N> { /** * Returns a proxy object for this node. */ public proxy(): types.ProxyNodeCon<N> { return { toApi: () => <any>this, toView: () => this.node.view(), }; } } /** * Local changes API for the `val` JSON CRDT node {@link ValNode}. * * @category Local API */ export class ValApi<N extends ValNode<any> = ValNode<any>> extends NodeApi<N> { /** * Get API instance of the inner node. * @returns Inner node API. */ public get(): JsonNodeApi<N extends ValNode<infer T> ? T : JsonNode> { return this.in() as any; } /** * Sets the value of the node. * * @param json JSON/CBOR value or ID (logical timestamp) of the value to set. * @returns Reference to itself. */ public set(json: JsonNodeView<N>): void { const {api, node} = this; const builder = api.builder; const val = builder.constOrJson(json); api.builder.setVal(node.id, val); api.apply(); } /** * Returns a proxy object for this node. Allows to access the value of the * node by accessing the `.val` property. */ public proxy(): types.ProxyNodeVal<N> { const self = this; const proxy = { toApi: () => <any>this, toView: () => this.node.view(), get val() { const childNode = self.node.node(); return (<any>self).api.wrap(childNode).proxy(); }, }; return <any>proxy; } } type UnVecNode<N> = N extends VecNode<infer T> ? T : never; /** * Local changes API for the `vec` JSON CRDT node {@link VecNode}. * * @category Local API */ export class VecApi<N extends VecNode<any> = VecNode<any>> extends NodeApi<N> { /** * Get API instance of a child node. * * @param key Object key to get. * @returns A specified child node API. */ public get<K extends keyof UnVecNode<N>>(key: K): JsonNodeApi<UnVecNode<N>[K]> { return this.in(key as string) as any; } /** * Sets a list of elements to the given values. * * @param entries List of index-value pairs to set. * @returns Reference to itself. */ public set(entries: [index: number, value: unknown][]): void { const {api, node} = this; const {builder} = api; builder.insVec( node.id, entries.map(([index, json]) => [index, builder.constOrJson(json)]), ); api.apply(); } public push(...values: unknown[]): void { const length = this.length(); this.set(values.map((value, index) => [length + index, value])); } /** * Get the length of the vector without materializing it to a view. * * @returns Length of the vector. */ public length(): number { return this.node.elements.length; } /** * Returns a proxy object for this node. Allows to access vector elements by * index. */ public proxy(): types.ProxyNodeVec<N> { const proxy = new Proxy( {}, { get: (target, prop, receiver) => { if (prop === 'toApi') return () => this; if (prop === 'toView') return () => this.view(); if (prop === 'toExt') return () => this.asExt(); const index = Number(prop); Iif (Number.isNaN(index)) throw new Error('INVALID_INDEX'); const child = this.node.get(index); Iif (!child) throw new Error('OUT_OF_BOUNDS'); return (<any>this).api.wrap(child).proxy(); }, }, ); return proxy as types.ProxyNodeVec<N>; } } type UnObjNode<N> = N extends ObjNode<infer T> ? T : never; /** * Local changes API for the `obj` JSON CRDT node {@link ObjNode}. * * @category Local API */ export class ObjApi<N extends ObjNode<any> = ObjNode<any>> extends NodeApi<N> { /** * Get API instance of a child node. * * @param key Object key to get. * @returns A specified child node API. */ public get<K extends keyof UnObjNode<N>>(key: K): JsonNodeApi<UnObjNode<N>[K]> { return this.in(key as string) as any; } /** * Sets a list of keys to the given values. * * @param entries List of key-value pairs to set. * @returns Reference to itself. */ public set(entries: Partial<JsonNodeView<N>>): void { const {api, node} = this; const {builder} = api; builder.insObj( node.id, Object.entries(entries).map(([key, json]) => [key, builder.constOrJson(json)]), ); api.apply(); } /** * Deletes a list of keys from the object. * * @param keys List of keys to delete. * @returns Reference to itself. */ public del(keys: string[]): void { const {api, node} = this; const {builder} = api; api.builder.insObj( node.id, keys.map((key) => [key, builder.const(undefined)]), ); api.apply(); } /** * Returns a proxy object for this node. Allows to access object properties * by key. */ public proxy(): types.ProxyNodeObj<N> { const proxy = new Proxy( {}, { get: (target, prop, receiver) => { if (prop === 'toApi') return () => this; if (prop === 'toView') return () => this.view(); const key = String(prop); const child = this.node.get(key); Iif (!child) throw new Error('NO_SUCH_KEY'); return (<any>this).api.wrap(child).proxy(); }, }, ); return proxy as types.ProxyNodeObj<N>; } } /** * Local changes API for the `str` JSON CRDT node {@link StrNode}. This API * allows to insert and delete bytes in the UTF-16 string by referencing its * local character positions. * * @category Local API */ export class StrApi extends NodeApi<StrNode> { /** * Inserts text at a given position. * * @param index Position at which to insert text. * @param text Text to insert. * @returns Reference to itself. */ public ins(index: number, text: string): void { const {api, node} = this; api.onBeforeLocalChange.emit(api.next); const builder = api.builder; builder.pad(); const nextTime = api.builder.nextTime(); const id = new Timestamp(builder.clock.sid, nextTime); const after = node.insAt(index, id, text); Iif (!after) throw new Error('OUT_OF_BOUNDS'); builder.insStr(node.id, after, text); api.advance(); } /** * Deletes a range of text at a given position. * * @param index Position at which to delete text. * @param length Number of UTF-16 code units to delete. * @returns Reference to itself. */ public del(index: number, length: number): void { const {api, node} = this; api.onBeforeLocalChange.emit(api.next); const builder = api.builder; builder.pad(); const spans = node.findInterval(index, length); Iif (!spans) throw new Error('OUT_OF_BOUNDS'); node.delete(spans); builder.del(node.id, spans); api.advance(); } /** * Given a character index in local coordinates, find the ID of the character * in the global coordinates. * * @param index Index of the character or `-1` for before the first character. * @returns ID of the character after which the given position is located. */ public findId(index: number | -1): ITimestampStruct { const node = this.node; const length = node.length(); const max = length - 1; Iif (index > max) index = max; if (index < 0) return node.id; const id = node.find(index); return id || node.id; } /** * Given a position in global coordinates, find the position in local * coordinates. * * @param id ID of the character. * @returns Index of the character in local coordinates. Returns -1 if the * the position refers to the beginning of the string. */ public findPos(id: ITimestampStruct): number | -1 { const node = this.node; const nodeId = node.id; if (nodeId.sid === id.sid && nodeId.time === id.time) return -1; const chunk = node.findById(id); Iif (!chunk) return -1; const pos = node.pos(chunk); return pos + (chunk.del ? 0 : id.time - chunk.id.time); } /** * Get the length of the string without materializing it to a view. * * @returns Length of the string. */ public length(): number { return this.node.length(); } /** * Returns a proxy object for this node. */ public proxy(): types.ProxyNodeStr { return { toApi: () => this, toView: () => this.node.view(), }; } } /** * Local changes API for the `bin` JSON CRDT node {@link BinNode}. This API * allows to insert and delete bytes in the binary string by referencing their * local index. * * @category Local API */ export class BinApi extends NodeApi<BinNode> { /** * Inserts octets at a given position. * * @param index Position at which to insert octets. * @param data Octets to insert. * @returns Reference to itself. */ public ins(index: number, data: Uint8Array): void { const {api, node} = this; const after = !index ? node.id : node.find(index - 1); Iif (!after) throw new Error('OUT_OF_BOUNDS'); api.builder.insBin(node.id, after, data); api.apply(); } /** * Deletes a range of octets at a given position. * * @param index Position at which to delete octets. * @param length Number of octets to delete. * @returns Reference to itself. */ public del(index: number, length: number): void { const {api, node} = this; const spans = node.findInterval(index, length); Iif (!spans) throw new Error('OUT_OF_BOUNDS'); api.builder.del(node.id, spans); api.apply(); } /** * Get the length of the binary blob without materializing it to a view. * * @returns Length of the binary blob. */ public length(): number { return this.node.length(); } /** * Returns a proxy object for this node. */ public proxy(): types.ProxyNodeBin { return { toApi: () => this, toView: () => this.node.view(), }; } } type UnArrNode<N> = N extends ArrNode<infer T> ? T : never; /** * Local changes API for the `arr` JSON CRDT node {@link ArrNode}. This API * allows to insert and delete elements in the array by referencing their local * index. * * @category Local API */ export class ArrApi<N extends ArrNode<any> = ArrNode<any>> extends NodeApi<N> { /** * Get API instance of a child node. * * @param index Index of the element to get. * @returns Child node API for the element at the given index. */ public get(index: number): JsonNodeApi<UnArrNode<N>> { return this.in(index) as any; } /** * Inserts elements at a given position. * * @param index Position at which to insert elements. * @param values JSON/CBOR values or IDs of the values to insert. * @returns Reference to itself. */ public ins(index: number, values: Array<JsonNodeView<N>[number]>): void { const {api, node} = this; const {builder} = api; const after = !index ? node.id : node.find(index - 1); Iif (!after) throw new Error('OUT_OF_BOUNDS'); const valueIds: ITimestampStruct[] = []; for (let i = 0; i < values.length; i++) valueIds.push(builder.json(values[i])); builder.insArr(node.id, after, valueIds); api.apply(); } /** * Deletes a range of elements at a given position. * * @param index Position at which to delete elements. * @param length Number of elements to delete. * @returns Reference to itself. */ public del(index: number, length: number): void { const {api, node} = this; const spans = node.findInterval(index, length); Iif (!spans) throw new Error('OUT_OF_BOUNDS'); api.builder.del(node.id, spans); api.apply(); } /** * Get the length of the array without materializing it to a view. * * @returns Length of the array. */ public length(): number { return this.node.length(); } /** * Returns a proxy object that allows to access array elements by index. * * @returns Proxy object that allows to access array elements by index. */ public proxy(): types.ProxyNodeArr<N> { const proxy = new Proxy( {}, { get: (target, prop, receiver) => { if (prop === 'toApi') return () => this; if (prop === 'toView') return () => this.view(); const index = Number(prop); Iif (Number.isNaN(index)) throw new Error('INVALID_INDEX'); const child = this.node.getNode(index); Iif (!child) throw new Error('OUT_OF_BOUNDS'); return (this.api.wrap(child) as any).proxy(); }, }, ); return proxy as types.ProxyNodeArr<N>; } } |