UNPKG

@graffy/common

Version:

Common libraries that used by various Graffy modules.

235 lines (196 loc) 8.09 kB
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 sieve(current, changes, result) { if (result === void 0) { result = []; } var index = 0; for (var _iterator = _createForOfIteratorHelperLoose(changes), _step; !(_step = _iterator()).done;) { var change = _step.value; index = isRange(change) ? insertRange(current, change, result, index) : insertNode(current, change, result, index); } return result; } export function insertRange(current, change, result, 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 (keyIx === endIx && !(current[keyIx] && current[keyIx].key <= key && current[keyIx].end >= end)) { // This range does not overlap with any existing data. Ignore it. return keyIx; } var appliedChange = []; var currentKey = change.key; for (var i = keyIx; i < endIx; i++) { var node = current[i]; // We treat a negative version as a non-existent node // as this is a hack used by subscribe. if (isRange(node) && node.version >= 0) { if (node.key > currentKey) { appliedChange.push({ key: currentKey, end: keyBefore(node.key), version: change.version }); } currentKey = keyAfter(node.end); } else { if (getNewerChange(node, change)) { appliedChange.push({ key: currentKey, end: keyBefore(node.key), version: change.version }); currentKey = keyAfter(node.key); } } if (currentKey >= change.end) { break; } } if (currentKey <= change.end) { appliedChange.push({ key: currentKey, end: change.end, version: change.version }); } if (appliedChange.length) result.push.apply(result, appliedChange); // 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)) { // console.log('Sieve Range-Range', debug(change), debug(node)); insertions.push.apply(insertions, mergeRanges(insertions.pop(), _node)); } else { // console.log('Sieve Change-Node', debug(change), debug(node)); insertNode(insertions, _node, [], insertions.length - 1); } } // console.log('Sieve:insertions', debug(insertions)); 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]; } // Ensure node is newer than base 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, result, start) { if (start === void 0) { start = 0; } 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, result) : updateNode(current, index, change, result); } else { // This change does not overlap with any existing knowledge. Skip it // current.splice(index, 0, change); return index; } } function insertNodeIntoRange(current, index, change, result) { var key = change.key; var range = current[index]; var newChange = getNewerChange(change, range); var newNode = getNewerNode(change, range); if (!newChange) return; result.push(newChange); var insertions = [range.key < key && _extends({}, range, { end: keyBefore(key) }), newNode, 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, result) { var node = current[index]; if (isBranch(change) && isBranch(node)) { // Both are branches: Recursively merge children. var nextResult = []; node.version = change.version; sieve(node.children, change.children, nextResult); if (nextResult.length) result.push(_extends({}, change, { children: nextResult })); } 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 = getNewerNode(node, change); current[index] = newNode || change; if (!newNode) result.push(change); // TODO: In the case of partial removal, what should result be? } else { // Current node is a leaf. Replace with the change if it is newer. var newChange = getNewerChange(change, node); var _newNode = getNewerNode(change, node); if (_newNode) current[index] = _newNode; // console.log(current); if (newChange && (change.value !== node.value || !isPathEqual(change.path, node.path))) { result.push(newChange); } } return index + 1; } function isPathEqual(first, second) { if (!first && !second) return true; if (!first || !second) return false; if (first.length !== second.length) return false; for (var i = 0; i < first.length; i++) { if (first[i] !== second[i]) return false; } return true; } function getNewerNode(node, base) { if (isBranch(node)) { var emptyNode = { key: '', end: "\uFFFF", version: base.version }; var children = [emptyNode]; sieve(children, node.children); return children.length === 1 && children[0] === emptyNode ? null : _extends({}, node, { children: children }); } else { // assertVersion(node, version); return node.version >= base.version ? node : null; } } function getNewerChange(node, base) { if (isBranch(node)) { var children = node.children.filter(function (child) { return getNewerChange(child, base); }); return children.length && _extends({}, node, { children: children }); } else { // assertVersion(node, version); return node.version >= base.version ? node : null; } } // function assertVersion(node, version) { // // if (node.version === version) { // // throw Error('merge.version_collision ' + [node.key, version].join(' ')); // // } // }