UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

104 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChangeEvent = void 0; const json_crdt_patch_1 = require("../../../json-crdt-patch"); /** Operation targets specific node: the operation has `.obj` property. */ const operationTargetsNode = (op) => { return (op instanceof json_crdt_patch_1.InsValOp || op instanceof json_crdt_patch_1.InsObjOp || op instanceof json_crdt_patch_1.InsVecOp || op instanceof json_crdt_patch_1.InsStrOp || op instanceof json_crdt_patch_1.InsBinOp || op instanceof json_crdt_patch_1.InsArrOp || op instanceof json_crdt_patch_1.UpdArrOp || op instanceof json_crdt_patch_1.DelOp); }; class ChangeEvent { constructor(raw, api) { this.raw = raw; this.api = api; this._direct = null; this._parents = null; } origin() { const { raw, api } = this; return raw instanceof Set ? 2 /* ChangeEventOrigin.Reset */ : typeof raw === 'number' ? 0 /* ChangeEventOrigin.Local */ : raw instanceof json_crdt_patch_1.Patch ? raw.getId()?.sid === api.model.clock.sid ? 0 /* ChangeEventOrigin.Local */ : 1 /* ChangeEventOrigin.Remote */ : 0 /* ChangeEventOrigin.Local */; } isLocal() { return this.origin() === 0 /* ChangeEventOrigin.Local */; } isReset() { return this.raw instanceof Set; } /** * JSON CRDT nodes directly affected by this change event, i.e. nodes * which are direct targets of operations in the change. */ direct() { let direct = this._direct; DIRECT: if (!direct) { const raw = this.raw; if (raw instanceof Set) { this._direct = direct = raw; break DIRECT; } this._direct = direct = new Set(); const index = this.api.model.index; if (typeof raw === 'number') { const startIndex = raw; const api = this.api; const ops = api.builder.patch.ops; for (let i = startIndex; i < ops.length; i++) { const op = ops[i]; if (operationTargetsNode(op)) { const node = index.get(op.obj); if (node) direct.add(node); } } } else if (raw instanceof json_crdt_patch_1.Patch) { const ops = raw.ops; const length = ops.length; for (let i = 0; i < length; i++) { const op = ops[i]; if (operationTargetsNode(op)) { const node = index.get(op.obj); if (node) direct.add(node); } } } } return direct; } /** * JSON CRDT nodes which are parents of directly affected nodes in this * change event. */ parents() { let parents = this._parents; if (!parents) { this._parents = parents = new Set(); const direct = this.direct(); for (const start of direct) { let parent = start.parent; while (parent && !parents.has(parent)) { parents.add(parent); parent = parent.parent; } } } return parents; } } exports.ChangeEvent = ChangeEvent; //# sourceMappingURL=events.js.map