UNPKG

loro-wasm

Version:

Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.

1,559 lines (1,495 loc) 191 kB
let imports = {}; imports['__wbindgen_placeholder__'] = module.exports; let wasm; const { TextDecoder, TextEncoder } = require(`util`); const heap = new Array(128).fill(undefined); heap.push(undefined, null, true, false); function getObject(idx) { return heap[idx]; } let heap_next = heap.length; function dropObject(idx) { if (idx < 132) return; heap[idx] = heap_next; heap_next = idx; } function takeObject(idx) { const ret = getObject(idx); dropObject(idx); return ret; } let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); let cachedUint8Memory0 = null; function getUint8Memory0() { if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8Memory0; } function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); } function addHeapObject(obj) { if (heap_next === heap.length) heap.push(heap.length + 1); const idx = heap_next; heap_next = heap[idx]; heap[idx] = obj; return idx; } let WASM_VECTOR_LEN = 0; let cachedTextEncoder = new TextEncoder('utf-8'); const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' ? function (arg, view) { return cachedTextEncoder.encodeInto(arg, view); } : function (arg, view) { const buf = cachedTextEncoder.encode(arg); view.set(buf); return { read: arg.length, written: buf.length }; }); function passStringToWasm0(arg, malloc, realloc) { if (realloc === undefined) { const buf = cachedTextEncoder.encode(arg); const ptr = malloc(buf.length, 1) >>> 0; getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); WASM_VECTOR_LEN = buf.length; return ptr; } let len = arg.length; let ptr = malloc(len, 1) >>> 0; const mem = getUint8Memory0(); let offset = 0; for (; offset < len; offset++) { const code = arg.charCodeAt(offset); if (code > 0x7F) break; mem[ptr + offset] = code; } if (offset !== len) { if (offset !== 0) { arg = arg.slice(offset); } ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; const view = getUint8Memory0().subarray(ptr + offset, ptr + len); const ret = encodeString(arg, view); offset += ret.written; ptr = realloc(ptr, len, offset, 1) >>> 0; } WASM_VECTOR_LEN = offset; return ptr; } function isLikeNone(x) { return x === undefined || x === null; } let cachedInt32Memory0 = null; function getInt32Memory0() { if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); } return cachedInt32Memory0; } let cachedFloat64Memory0 = null; function getFloat64Memory0() { if (cachedFloat64Memory0 === null || cachedFloat64Memory0.byteLength === 0) { cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer); } return cachedFloat64Memory0; } let cachedBigInt64Memory0 = null; function getBigInt64Memory0() { if (cachedBigInt64Memory0 === null || cachedBigInt64Memory0.byteLength === 0) { cachedBigInt64Memory0 = new BigInt64Array(wasm.memory.buffer); } return cachedBigInt64Memory0; } function debugString(val) { // primitive types const type = typeof val; if (type == 'number' || type == 'boolean' || val == null) { return `${val}`; } if (type == 'string') { return `"${val}"`; } if (type == 'symbol') { const description = val.description; if (description == null) { return 'Symbol'; } else { return `Symbol(${description})`; } } if (type == 'function') { const name = val.name; if (typeof name == 'string' && name.length > 0) { return `Function(${name})`; } else { return 'Function'; } } // objects if (Array.isArray(val)) { const length = val.length; let debug = '['; if (length > 0) { debug += debugString(val[0]); } for(let i = 1; i < length; i++) { debug += ', ' + debugString(val[i]); } debug += ']'; return debug; } // Test for built-in const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); let className; if (builtInMatches.length > 1) { className = builtInMatches[1]; } else { // Failed to match the standard '[object ClassName]' return toString.call(val); } if (className == 'Object') { // we're a user defined class or Object // JSON.stringify avoids problems with cycles, and is generally much // easier than looping through ownProperties of `val`. try { return 'Object(' + JSON.stringify(val) + ')'; } catch (_) { return 'Object'; } } // errors if (val instanceof Error) { return `${val.name}: ${val.message}\n${val.stack}`; } // TODO we could test for more things here, like `Set`s and `Map`s. return className; } const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(state => { wasm.__wbindgen_export_2.get(state.dtor)(state.a, state.b) }); function makeMutClosure(arg0, arg1, dtor, f) { const state = { a: arg0, b: arg1, cnt: 1, dtor }; const real = (...args) => { // First up with a closure we increment the internal reference // count. This ensures that the Rust closure environment won't // be deallocated while we're invoking it. state.cnt++; const a = state.a; state.a = 0; try { return f(a, state.b, ...args); } finally { if (--state.cnt === 0) { wasm.__wbindgen_export_2.get(state.dtor)(a, state.b); CLOSURE_DTORS.unregister(state); } else { state.a = a; } } }; real.original = state; CLOSURE_DTORS.register(real, state, state); return real; } function __wbg_adapter_58(arg0, arg1, arg2) { wasm.__wbindgen_export_3(arg0, arg1, addHeapObject(arg2)); } function __wbg_adapter_61(arg0, arg1) { wasm.__wbindgen_export_4(arg0, arg1); } function getArrayU8FromWasm0(ptr, len) { ptr = ptr >>> 0; return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len); } function passArray8ToWasm0(arg, malloc) { const ptr = malloc(arg.length * 1, 1) >>> 0; getUint8Memory0().set(arg, ptr / 1); WASM_VECTOR_LEN = arg.length; return ptr; } let cachedUint32Memory0 = null; function getUint32Memory0() { if (cachedUint32Memory0 === null || cachedUint32Memory0.byteLength === 0) { cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer); } return cachedUint32Memory0; } function getArrayJsValueFromWasm0(ptr, len) { ptr = ptr >>> 0; const mem = getUint32Memory0(); const slice = mem.subarray(ptr / 4, ptr / 4 + len); const result = []; for (let i = 0; i < slice.length; i++) { result.push(takeObject(slice[i])); } return result; } /** */ module.exports.run = function() { wasm.run(); }; function passArrayJsValueToWasm0(array, malloc) { const ptr = malloc(array.length * 4, 4) >>> 0; const mem = getUint32Memory0(); for (let i = 0; i < array.length; i++) { mem[ptr / 4 + i] = addHeapObject(array[i]); } WASM_VECTOR_LEN = array.length; return ptr; } /** * @param {({ peer: PeerID, counter: number })[]} frontiers * @returns {Uint8Array} */ module.exports.encodeFrontiers = function(frontiers) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArrayJsValueToWasm0(frontiers, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.encodeFrontiers(retptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; var r3 = getInt32Memory0()[retptr / 4 + 3]; if (r3) { throw takeObject(r2); } var v2 = getArrayU8FromWasm0(r0, r1).slice(); wasm.__wbindgen_export_5(r0, r1 * 1, 1); return v2; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } }; /** * @param {Uint8Array} bytes * @returns {{ peer: PeerID, counter: number }[]} */ module.exports.decodeFrontiers = function(bytes) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.decodeFrontiers(retptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return takeObject(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } }; /** * Enable debug info of Loro */ module.exports.setDebug = function() { wasm.setDebug(); }; let stack_pointer = 128; function addBorrowedObject(obj) { if (stack_pointer == 1) throw new Error('out of js stack'); heap[--stack_pointer] = obj; return stack_pointer; } function _assertClass(instance, klass) { if (!(instance instanceof klass)) { throw new Error(`expected instance of ${klass.name}`); } return instance.ptr; } /** * Decode the metadata of the import blob. * * This method is useful to get the following metadata of the import blob: * * - startVersionVector * - endVersionVector * - startTimestamp * - endTimestamp * - isSnapshot * - changeNum * @param {Uint8Array} blob * @returns {ImportBlobMetadata} */ module.exports.decodeImportBlobMeta = function(blob) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(blob, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.decodeImportBlobMeta(retptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return takeObject(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } }; function handleError(f, args) { try { return f.apply(this, args); } catch (e) { wasm.__wbindgen_export_6(addHeapObject(e)); } } function notDefined(what) { return () => { throw new Error(`${what} is not defined`); }; } const AwarenessWasmFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_awarenesswasm_free(ptr >>> 0)); /** * `Awareness` is a structure that tracks the ephemeral state of peers. * * It can be used to synchronize cursor positions, selections, and the names of the peers. * * The state of a specific peer is expected to be removed after a specified timeout. Use * `remove_outdated` to eliminate outdated states. */ class AwarenessWasm { __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; AwarenessWasmFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_awarenesswasm_free(ptr); } /** * Creates a new `Awareness` instance. * * The `timeout` parameter specifies the duration in milliseconds. * A state of a peer is considered outdated, if the last update of the state of the peer * is older than the `timeout`. * @param {number | bigint | `${number}`} peer * @param {number} timeout */ constructor(peer, timeout) { const ret = wasm.awarenesswasm_new(addHeapObject(peer), timeout); this.__wbg_ptr = ret >>> 0; return this; } /** * Encodes the state of the given peers. * @param {Array<any>} peers * @returns {Uint8Array} */ encode(peers) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.awarenesswasm_encode(retptr, this.__wbg_ptr, addHeapObject(peers)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var v1 = getArrayU8FromWasm0(r0, r1).slice(); wasm.__wbindgen_export_5(r0, r1 * 1, 1); return v1; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Encodes the state of all peers. * @returns {Uint8Array} */ encodeAll() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.awarenesswasm_encodeAll(retptr, this.__wbg_ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var v1 = getArrayU8FromWasm0(r0, r1).slice(); wasm.__wbindgen_export_5(r0, r1 * 1, 1); return v1; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Applies the encoded state of peers. * * Each peer's deletion countdown will be reset upon update, requiring them to pass through the `timeout` * interval again before being eligible for deletion. * @param {Uint8Array} encoded_peers_info * @returns {{ updated: PeerID[], added: PeerID[] }} */ apply(encoded_peers_info) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(encoded_peers_info, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.awarenesswasm_apply(retptr, this.__wbg_ptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return takeObject(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Sets the state of the local peer. * @param {any} value */ setLocalState(value) { wasm.awarenesswasm_setLocalState(this.__wbg_ptr, addHeapObject(value)); } /** * Get the PeerID of the local peer. * @returns {PeerID} */ peer() { const ret = wasm.awarenesswasm_peer(this.__wbg_ptr); return takeObject(ret); } /** * Get the state of all peers. * @returns {{[peer in PeerID]: unknown}} */ getAllStates() { const ret = wasm.awarenesswasm_getAllStates(this.__wbg_ptr); return takeObject(ret); } /** * Get the state of a given peer. * @param {number | bigint | `${number}`} peer * @returns {any} */ getState(peer) { const ret = wasm.awarenesswasm_getState(this.__wbg_ptr, addHeapObject(peer)); return takeObject(ret); } /** * Get the timestamp of the state of a given peer. * @param {number | bigint | `${number}`} peer * @returns {number | undefined} */ getTimestamp(peer) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.awarenesswasm_getTimestamp(retptr, this.__wbg_ptr, addHeapObject(peer)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r2 = getFloat64Memory0()[retptr / 8 + 1]; return r0 === 0 ? undefined : r2; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Remove the states of outdated peers. * @returns {(PeerID)[]} */ removeOutdated() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.awarenesswasm_removeOutdated(retptr, this.__wbg_ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); wasm.__wbindgen_export_5(r0, r1 * 4, 4); return v1; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Get the number of peers. * @returns {number} */ length() { const ret = wasm.awarenesswasm_length(this.__wbg_ptr); return ret; } /** * If the state is empty. * @returns {boolean} */ isEmpty() { const ret = wasm.awarenesswasm_isEmpty(this.__wbg_ptr); return ret !== 0; } /** * Get all the peers * @returns {(PeerID)[]} */ peers() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.awarenesswasm_peers(retptr, this.__wbg_ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); wasm.__wbindgen_export_5(r0, r1 * 4, 4); return v1; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } } module.exports.AwarenessWasm = AwarenessWasm; const CursorFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_cursor_free(ptr >>> 0)); /** * Cursor is a stable position representation in the doc. * When expressing the position of a cursor, using "index" can be unstable * because the cursor's position may change due to other deletions and insertions, * requiring updates with each edit. To stably represent a position or range within * a list structure, we can utilize the ID of each item/character on List CRDT or * Text CRDT for expression. * * Loro optimizes State metadata by not storing the IDs of deleted elements. This * approach complicates tracking cursors since they rely on these IDs. The solution * recalculates position by replaying relevant history to update cursors * accurately. To minimize the performance impact of history replay, the system * updates cursor info to reference only the IDs of currently present elements, * thereby reducing the need for replay. * * @example * ```ts * * const doc = new LoroDoc(); * const text = doc.getText("text"); * text.insert(0, "123"); * const pos0 = text.getCursor(0, 0); * { * const ans = doc.getCursorPos(pos0!); * expect(ans.offset).toBe(0); * } * text.insert(0, "1"); * { * const ans = doc.getCursorPos(pos0!); * expect(ans.offset).toBe(1); * } * ``` */ class Cursor { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(Cursor.prototype); obj.__wbg_ptr = ptr; CursorFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; CursorFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_cursor_free(ptr); } /** * Get the id of the given container. * @returns {ContainerID} */ containerId() { const ret = wasm.cursor_containerId(this.__wbg_ptr); return takeObject(ret); } /** * Get the ID that represents the position. * * It can be undefined if it's not bind into a specific ID. * @returns {{ peer: PeerID, counter: number } | undefined} */ pos() { const ret = wasm.cursor_pos(this.__wbg_ptr); return takeObject(ret); } /** * Get which side of the character/list item the cursor is on. * @returns {Side} */ side() { const ret = wasm.cursor_side(this.__wbg_ptr); return takeObject(ret); } /** * Encode the cursor into a Uint8Array. * @returns {Uint8Array} */ encode() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.cursor_encode(retptr, this.__wbg_ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var v1 = getArrayU8FromWasm0(r0, r1).slice(); wasm.__wbindgen_export_5(r0, r1 * 1, 1); return v1; } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Decode the cursor from a Uint8Array. * @param {Uint8Array} data * @returns {Cursor} */ static decode(data) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.cursor_decode(retptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return Cursor.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * "Cursor" * @returns {any} */ kind() { const ret = wasm.cursor_kind(this.__wbg_ptr); return takeObject(ret); } } module.exports.Cursor = Cursor; const LoroCounterFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_lorocounter_free(ptr >>> 0)); /** * The handler of a tree(forest) container. */ class LoroCounter { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(LoroCounter.prototype); obj.__wbg_ptr = ptr; LoroCounterFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; LoroCounterFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_lorocounter_free(ptr); } /** * Create a new LoroCounter. */ constructor() { const ret = wasm.lorocounter_new(); this.__wbg_ptr = ret >>> 0; return this; } /** * Increment the counter by the given value. * @param {number} value */ increment(value) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorocounter_increment(retptr, this.__wbg_ptr, value); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Decrement the counter by the given value. * @param {number} value */ decrement(value) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorocounter_decrement(retptr, this.__wbg_ptr, value); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Get the value of the counter. * @returns {number} */ get value() { const ret = wasm.lorocounter_value(this.__wbg_ptr); return ret; } /** * Subscribe to the changes of the counter. * @param {Function} f * @returns {any} */ subscribe(f) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorocounter_subscribe(retptr, this.__wbg_ptr, addHeapObject(f)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return takeObject(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Get the parent container of the counter container. * * - The parent container of the root counter is `undefined`. * - The object returned is a new js object each time because it need to cross * the WASM boundary. * @returns {Container | undefined} */ parent() { const ret = wasm.lorocounter_parent(this.__wbg_ptr); return takeObject(ret); } /** * Whether the container is attached to a docuemnt. * * If it's detached, the operations on the container will not be persisted. * @returns {boolean} */ isAttached() { const ret = wasm.lorocounter_isAttached(this.__wbg_ptr); return ret !== 0; } /** * Get the attached container associated with this. * * Returns an attached `Container` that equals to this or created by this, otherwise `undefined`. * @returns {LoroTree | undefined} */ getAttached() { const ret = wasm.lorocounter_getAttached(this.__wbg_ptr); return takeObject(ret); } } module.exports.LoroCounter = LoroCounter; const LoroDocFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_lorodoc_free(ptr >>> 0)); /** * The CRDTs document. Loro supports different CRDTs include [**List**](LoroList), * [**RichText**](LoroText), [**Map**](LoroMap) and [**Movable Tree**](LoroTree), * you could build all kind of applications by these. * * @example * ```ts * import { LoroDoc } import "loro-crdt" * * const loro = new LoroDoc(); * const text = loro.getText("text"); * const list = loro.getList("list"); * const map = loro.getMap("Map"); * const tree = loro.getTree("tree"); * ``` */ class LoroDoc { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(LoroDoc.prototype); obj.__wbg_ptr = ptr; LoroDocFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; LoroDocFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_lorodoc_free(ptr); } /** * Create a new loro document. * * New document will have random peer id. */ constructor() { const ret = wasm.lorodoc_new(); this.__wbg_ptr = ret >>> 0; return this; } /** * Enables editing in detached mode, which is disabled by default. * * The doc enter detached mode after calling `detach` or checking out a non-latest version. * * # Important Notes: * * - This mode uses a different PeerID for each checkout. * - Ensure no concurrent operations share the same PeerID if set manually. * - Importing does not affect the document's state or version; changes are * recorded in the [OpLog] only. Call `checkout` to apply changes. * @param {boolean} enable */ setDetachedEditing(enable) { wasm.lorodoc_setDetachedEditing(this.__wbg_ptr, enable); } /** * Whether the editing is enabled in detached mode. * * The doc enter detached mode after calling `detach` or checking out a non-latest version. * * # Important Notes: * * - This mode uses a different PeerID for each checkout. * - Ensure no concurrent operations share the same PeerID if set manually. * - Importing does not affect the document's state or version; changes are * recorded in the [OpLog] only. Call `checkout` to apply changes. * @returns {boolean} */ isDetachedEditingEnabled() { const ret = wasm.lorodoc_isDetachedEditingEnabled(this.__wbg_ptr); return ret !== 0; } /** * Set whether to record the timestamp of each change. Default is `false`. * * If enabled, the Unix timestamp will be recorded for each change automatically. * * You can also set each timestamp manually when you commit a change. * The timestamp manually set will override the automatic one. * * NOTE: Timestamps are forced to be in ascending order. * If you commit a new change with a timestamp that is less than the existing one, * the largest existing timestamp will be used instead. * @param {boolean} auto_record */ setRecordTimestamp(auto_record) { wasm.lorodoc_setRecordTimestamp(this.__wbg_ptr, auto_record); } /** * If two continuous local changes are within the interval, they will be merged into one change. * * The default value is 1_000_000, the default unit is milliseconds. * @param {number} interval */ setChangeMergeInterval(interval) { wasm.lorodoc_setChangeMergeInterval(this.__wbg_ptr, interval); } /** * Set the rich text format configuration of the document. * * You need to config it if you use rich text `mark` method. * Specifically, you need to config the `expand` property of each style. * * Expand is used to specify the behavior of expanding when new text is inserted at the * beginning or end of the style. * * You can specify the `expand` option to set the behavior when inserting text at the boundary of the range. * * - `after`(default): when inserting text right after the given range, the mark will be expanded to include the inserted text * - `before`: when inserting text right before the given range, the mark will be expanded to include the inserted text * - `none`: the mark will not be expanded to include the inserted text at the boundaries * - `both`: when inserting text either right before or right after the given range, the mark will be expanded to include the inserted text * * @example * ```ts * const doc = new LoroDoc(); * doc.configTextStyle({ * bold: { expand: "after" }, * link: { expand: "before" } * }); * const text = doc.getText("text"); * text.insert(0, "Hello World!"); * text.mark({ start: 0, end: 5 }, "bold", true); * expect(text.toDelta()).toStrictEqual([ * { * insert: "Hello", * attributes: { * bold: true, * }, * }, * { * insert: " World!", * }, * ] as Delta<string>[]); * ``` * @param {{[key: string]: { expand: 'before'|'after'|'none'|'both' }}} styles */ configTextStyle(styles) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_configTextStyle(retptr, this.__wbg_ptr, addHeapObject(styles)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Get a loro document from the snapshot. * * @see You can check out what is the snapshot [here](#). * * @example * ```ts * import { LoroDoc } import "loro-crdt" * * const bytes = /* The bytes encoded from other loro document *\/; * const loro = LoroDoc.fromSnapshot(bytes); * ``` * @param {Uint8Array} snapshot * @returns {LoroDoc} */ static fromSnapshot(snapshot) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArray8ToWasm0(snapshot, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.lorodoc_fromSnapshot(retptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroDoc.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Attach the document state to the latest known version. * * > The document becomes detached during a `checkout` operation. * > Being `detached` implies that the `DocState` is not synchronized with the latest version of the `OpLog`. * > In a detached state, the document is not editable, and any `import` operations will be * > recorded in the `OpLog` without being applied to the `DocState`. * * This method has the same effect as invoking `checkout_to_latest`. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const text = doc.getText("text"); * const frontiers = doc.frontiers(); * text.insert(0, "Hello World!"); * loro.checkout(frontiers); * // you need call `attach()` or `checkoutToLatest()` before changing the doc. * loro.attach(); * text.insert(0, "Hi"); * ``` */ attach() { wasm.lorodoc_attach(this.__wbg_ptr); } /** * `detached` indicates that the `DocState` is not synchronized with the latest version of `OpLog`. * * > The document becomes detached during a `checkout` operation. * > Being `detached` implies that the `DocState` is not synchronized with the latest version of the `OpLog`. * > In a detached state, the document is not editable, and any `import` operations will be * > recorded in the `OpLog` without being applied to the `DocState`. * * When `detached`, the document is not editable. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const text = doc.getText("text"); * const frontiers = doc.frontiers(); * text.insert(0, "Hello World!"); * console.log(doc.is_detached()); // false * loro.checkout(frontiers); * console.log(doc.is_detached()); // true * loro.attach(); * console.log(doc.is_detached()); // false * ``` * @returns {boolean} */ isDetached() { const ret = wasm.lorodoc_isDetached(this.__wbg_ptr); return ret !== 0; } /** * Detach the document state from the latest known version. * * After detaching, all import operations will be recorded in the `OpLog` without being applied to the `DocState`. * When `detached`, the document is not editable. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * doc.detach(); * console.log(doc.is_detached()); // true * ``` */ detach() { wasm.lorodoc_detach(this.__wbg_ptr); } /** * Duplicate the document with a different PeerID * * The time complexity and space complexity of this operation are both O(n), * @returns {LoroDoc} */ fork() { const ret = wasm.lorodoc_fork(this.__wbg_ptr); return LoroDoc.__wrap(ret); } /** * Creates a new LoroDoc at a specified version (Frontiers) * @param {({ peer: PeerID, counter: number })[]} frontiers * @returns {LoroDoc} */ forkAt(frontiers) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArrayJsValueToWasm0(frontiers, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.lorodoc_forkAt(retptr, this.__wbg_ptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroDoc.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Checkout the `DocState` to the latest version of `OpLog`. * * > The document becomes detached during a `checkout` operation. * > Being `detached` implies that the `DocState` is not synchronized with the latest version of the `OpLog`. * > In a detached state, the document is not editable, and any `import` operations will be * > recorded in the `OpLog` without being applied to the `DocState`. * * This has the same effect as `attach`. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const text = doc.getText("text"); * const frontiers = doc.frontiers(); * text.insert(0, "Hello World!"); * loro.checkout(frontiers); * // you need call `checkoutToLatest()` or `attach()` before changing the doc. * loro.checkoutToLatest(); * text.insert(0, "Hi"); * ``` */ checkoutToLatest() { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_checkoutToLatest(retptr, this.__wbg_ptr); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * @param {({ peer: PeerID, counter: number })[]} ids * @param {Function} f */ travelChangeAncestors(ids, f) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArrayJsValueToWasm0(ids, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.lorodoc_travelChangeAncestors(retptr, this.__wbg_ptr, ptr0, len0, addHeapObject(f)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Checkout the `DocState` to a specific version. * * > The document becomes detached during a `checkout` operation. * > Being `detached` implies that the `DocState` is not synchronized with the latest version of the `OpLog`. * > In a detached state, the document is not editable, and any `import` operations will be * > recorded in the `OpLog` without being applied to the `DocState`. * * You should call `attach` to attach the `DocState` to the latest version of `OpLog`. * * @param frontiers - the specific frontiers * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const text = doc.getText("text"); * const frontiers = doc.frontiers(); * text.insert(0, "Hello World!"); * loro.checkout(frontiers); * console.log(doc.toJSON()); // {"text": ""} * ``` * @param {({ peer: PeerID, counter: number })[]} frontiers */ checkout(frontiers) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); const ptr0 = passArrayJsValueToWasm0(frontiers, wasm.__wbindgen_export_0); const len0 = WASM_VECTOR_LEN; wasm.lorodoc_checkout(retptr, this.__wbg_ptr, ptr0, len0); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Peer ID of the current writer. * @returns {bigint} */ get peerId() { const ret = wasm.lorodoc_peerId(this.__wbg_ptr); return BigInt.asUintN(64, ret); } /** * Get peer id in decimal string. * @returns {PeerID} */ get peerIdStr() { const ret = wasm.lorodoc_peerIdStr(this.__wbg_ptr); return takeObject(ret); } /** * Set the peer ID of the current writer. * * It must be a number, a BigInt, or a decimal string that can be parsed to a unsigned 64-bit integer. * * Note: use it with caution. You need to make sure there is not chance that two peers * have the same peer ID. Otherwise, we cannot ensure the consistency of the document. * @param {number | bigint | `${number}`} peer_id */ setPeerId(peer_id) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_setPeerId(retptr, this.__wbg_ptr, addHeapObject(peer_id)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Commit the cumulative auto committed transaction. * * You can specify the `origin`, `timestamp`, and `message` of the commit. * * The `origin` is used to mark the event, and the `message` works like a git commit message. * * The events will be emitted after a transaction is committed. A transaction is committed when: * * - `doc.commit()` is called. * - `doc.export(mode)` is called. * - `doc.import(data)` is called. * - `doc.checkout(version)` is called. * * NOTE: Timestamps are forced to be in ascending order. * If you commit a new change with a timestamp that is less than the existing one, * the largest existing timestamp will be used instead. * * NOTE: The `origin` will not be persisted, but the `message` will. * @param {{ origin?: string, timestamp?: number, message?: string } | undefined} [options] */ commit(options) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_commit(retptr, this.__wbg_ptr, isLikeNone(options) ? 0 : addHeapObject(options)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; if (r1) { throw takeObject(r0); } } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Get the number of operations in the pending transaction. * * The pending transaction is the one that is not committed yet. It will be committed * automatically after calling `doc.commit()`, `doc.export(mode)` or `doc.checkout(version)`. * @returns {number} */ getPendingTxnLength() { const ret = wasm.lorodoc_getPendingTxnLength(this.__wbg_ptr); return ret >>> 0; } /** * Get a LoroText by container id. * * The object returned is a new js object each time because it need to cross * the WASM boundary. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const text = doc.getText("text"); * ``` * @param {ContainerID | string} cid * @returns {LoroText} */ getText(cid) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getText(retptr, this.__wbg_ptr, addBorrowedObject(cid)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroText.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } } /** * Get a LoroMap by container id * * The object returned is a new js object each time because it need to cross * the WASM boundary. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const map = doc.getMap("map"); * ``` * @param {ContainerID | string} cid * @returns {LoroMap} */ getMap(cid) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getMap(retptr, this.__wbg_ptr, addBorrowedObject(cid)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroMap.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } } /** * Get a LoroList by container id * * The object returned is a new js object each time because it need to cross * the WASM boundary. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const list = doc.getList("list"); * ``` * @param {ContainerID | string} cid * @returns {LoroList} */ getList(cid) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getList(retptr, this.__wbg_ptr, addBorrowedObject(cid)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroList.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } } /** * Get a LoroMovableList by container id * * The object returned is a new js object each time because it need to cross * the WASM boundary. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const list = doc.getMovableList("list"); * ``` * @param {ContainerID | string} cid * @returns {LoroMovableList} */ getMovableList(cid) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getMovableList(retptr, this.__wbg_ptr, addBorrowedObject(cid)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroMovableList.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } } /** * Get a LoroCounter by container id * @param {ContainerID | string} cid * @returns {LoroCounter} */ getCounter(cid) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getCounter(retptr, this.__wbg_ptr, addBorrowedObject(cid)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroCounter.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } } /** * Get a LoroTree by container id * * The object returned is a new js object each time because it need to cross * the WASM boundary. * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * const tree = doc.getTree("tree"); * ``` * @param {ContainerID | string} cid * @returns {LoroTree} */ getTree(cid) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getTree(retptr, this.__wbg_ptr, addBorrowedObject(cid)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return LoroTree.__wrap(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); heap[stack_pointer++] = undefined; } } /** * Get the container corresponding to the container id * * * @example * ```ts * import { LoroDoc } from "loro-crdt"; * * const doc = new LoroDoc(); * let text = doc.getText("text"); * const textId = text.id; * text = doc.getContainerById(textId); * ``` * @param {ContainerID} container_id * @returns {any} */ getContainerById(container_id) { try { const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); wasm.lorodoc_getContainerById(retptr, this.__wbg_ptr, addHeapObject(container_id)); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; var r2 = getInt32Memory0()[retptr / 4 + 2]; if (r2) { throw takeObject(r1); } return takeObject(r0); } finally { wasm.__wbindgen_add_to_stack_pointer(16); } } /** * Set the commit message of the next commit * @param {string} msg */ setNextCommitMessage(msg) { const ptr0 = passStringToWasm0(msg, wasm.__wbindgen_export_0, wasm.__wbindgen_ex