UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

57 lines (56 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChunkSlice = void 0; const json_hash_1 = require("../../../json-hash"); const hash_1 = require("../../../json-crdt/hash"); const clock_1 = require("../../../json-crdt-patch/clock"); class ChunkSlice { constructor( /** Chunk from which slice is computed. */ chunk, /** Start offset of the slice within the chunk. */ off, /** Length of the slice. */ len) { this.off = off; this.len = len; // ----------------------------------------------------------------- Stateful this.hash = 0; this.chunk = chunk; } id() { const id = this.chunk.id; const off = this.off; return !off ? id : new clock_1.Timestamp(id.sid, id.time + off); } key() { const id = this.chunk.id; const sid = id.sid; const time = id.time + this.off; return sid.toString(36) + time.toString(36); } view() { const offset = this.off; return this.chunk.view().slice(offset, offset + this.len); } refresh() { const { chunk, off, len } = this; const delOffLenState = (((off << 16) + len) << 1) + +chunk.del; let state = json_hash_1.CONST.START_STATE; state = (0, hash_1.updateId)(state, chunk.id); state = (0, json_hash_1.updateNum)(state, delOffLenState); return (this.hash = state); } // ---------------------------------------------------------------- Printable toString(tab = '') { const name = 'ChunkSlice'; const off = this.off; const len = this.len; const str = this.view() + ''; const truncate = str.length > 32; const view = JSON.stringify(truncate ? str.slice(0, 32) : str) + (truncate ? ' …' : ''); const id = (0, clock_1.printTs)(this.chunk.id); return `${name} ${id} [${off}..${off + len}) ${view}`; } } exports.ChunkSlice = ChunkSlice;