@graffy/common
Version:
Common libraries that used by various Graffy modules.
140 lines (118 loc) • 5.31 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
import { isBranch, isRange, findFirst, findLast } from '../node';
import { keyAfter, keyBefore } from './step';
export default function merge(current, changes) {
var index = 0;
for (var _iterator = _createForOfIteratorHelperLoose(changes), _step; !(_step = _iterator()).done;) {
var change = _step.value;
index = isRange(change) ? insertRange(current, change, index) : insertNode(current, change, index);
}
return current;
}
export function insertRange(current, change, start) {
if (start === void 0) {
start = 0;
}
var key = change.key,
end = change.end;
var keyIx = findFirst(current, key, start);
var endIx = findLast(current, end, keyIx); // If current contains nodes that are newer than this range, keep them.
// We do this by merging them back into insertions first.
var insertions = [change];
for (var i = keyIx; i < endIx; i++) {
var node = current[i];
if (isRange(node)) {
insertions.push.apply(insertions, mergeRanges(insertions.pop(), node));
} else {
insertNode(insertions, node, insertions.length - 1);
}
}
current.splice.apply(current, [keyIx, endIx - keyIx].concat(insertions));
return keyIx + insertions.length;
}
function mergeRanges(base, node) {
// assertVersion(node, base.version);
// eslint-disable-next-line no-param-reassign
if (node.version < base.version) {
var _ref = [base, node];
node = _ref[0];
base = _ref[1];
}
return [base.key < node.key && _extends({}, base, {
end: keyBefore(node.key)
}), node, base.end > node.end && _extends({}, base, {
key: keyAfter(node.end)
})].filter(Boolean);
}
export function insertNode(current, change, start) {
if (start === void 0) {
start = 0;
}
if (!current) throw new Error('Current' + current);
var key = change.key;
var index = findFirst(current, key, start);
var node = current[index];
if (node && node.key <= key) {
// This change overlaps with something that exists.
return isRange(node) ? insertNodeIntoRange(current, index, change) : updateNode(current, index, change);
} else {
// This change does not overlap with any existing knowledge. Insert it
current.splice(index, 0, change);
return index + 1;
}
}
function insertNodeIntoRange(current, index, change) {
var key = change.key;
var range = current[index];
var newChange = getNewer(change, range);
if (!newChange) return;
var insertions = [range.key < key && _extends({}, range, {
end: keyBefore(key)
}), newChange, range.end > key && _extends({}, range, {
key: keyAfter(key)
})].filter(Boolean);
current.splice.apply(current, [index, 1].concat(insertions));
return index + insertions.length;
}
function updateNode(current, index, change) {
var node = current[index];
if (isBranch(change) && isBranch(node)) {
// Both are branches: Recursively merge children.
merge(node.children, change.children);
} else if (isBranch(node)) {
// Current node is a branch but the change is a leaf; if the branch
// has newer children, ignore the change and keep only those children;
// Otherwise, discard the branch and keep the change.
var newNode = getNewer(node, change);
current[index] = newNode || change;
} else {
// Current node is a leaf. Replace with the change if it is newer.
var newChange = getNewer(change, node);
if (newChange) current[index] = newChange;
}
return index + 1;
}
function getNewer(node, base) {
var version = base.version;
if (isBranch(node)) {
var children = [{
key: '',
end: "\uFFFF",
version: version
}];
merge(children, node.children);
return children.length === 1 ? null : _extends({}, node, {
children: children
});
} else {
// assertVersion(node, version);
return node.version >= version ? node : null;
}
} // function assertVersion(node, version) {
// // if (node.version === version) {
// // throw Error('merge.version_collision ' + [node.key, version].join(' '));
// // }
// }