@graffy/common 0.16.0-alpha.1 → 0.16.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.cjs CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const isEqual = require("lodash/isEqual.js");
3
4
  const mergeIterators = require("merge-async-iterators");
4
5
  const stream = require("@graffy/stream");
5
- const isEqual = require("lodash/isEqual.js");
6
6
  const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
7
- const mergeIterators__default = /* @__PURE__ */ _interopDefaultLegacy(mergeIterators);
8
7
  const isEqual__default = /* @__PURE__ */ _interopDefaultLegacy(isEqual);
8
+ const mergeIterators__default = /* @__PURE__ */ _interopDefaultLegacy(mergeIterators);
9
9
  const textEncoder = new TextEncoder();
10
10
  const textDecoder = new TextDecoder("utf-8");
11
11
  function encode$6(string) {
@@ -119,6 +119,7 @@ const stringifyDescriptor = {
119
119
  };
120
120
  function addStringify(buffer) {
121
121
  Object.defineProperties(buffer, {
122
+ toJSON: stringifyDescriptor,
122
123
  toString: stringifyDescriptor,
123
124
  [Symbol.for("nodejs.util.inspect.custom")]: stringifyDescriptor
124
125
  });
@@ -390,8 +391,6 @@ function encode$3(arg) {
390
391
  return node;
391
392
  }
392
393
  function decode$3(node) {
393
- if (typeof node === "string")
394
- throw Error("why?");
395
394
  const { key, end, limit } = node;
396
395
  errIf("no_key", !isDef(key));
397
396
  errIf("limit_without_end", isDef(limit) && !isDef(end));
@@ -639,10 +638,10 @@ function getNewer(node, base) {
639
638
  }
640
639
  }
641
640
  const IS_VAL = Symbol("IS_VAL");
642
- function makeNode(seg, props) {
641
+ function makeNode(seg, props2) {
643
642
  if (ArrayBuffer.isView(seg))
644
- return { key: seg, ...props };
645
- return { ...seg, ...props };
643
+ return { key: seg, ...props2 };
644
+ return { ...seg, ...props2 };
646
645
  }
647
646
  function wrapValue(value, path, version = 0) {
648
647
  const node = makeNode(path[path.length - 1], { value, version });
@@ -869,7 +868,7 @@ function insertRange(current, change, result, start = 0) {
869
868
  const { key, end } = change;
870
869
  const keyIx = findFirst(current, key, start);
871
870
  const endIx = findLast(current, end, keyIx);
872
- if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, key) <= 0 && cmp(current[keyIx].end, end) >= 0)) {
871
+ if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, end) <= 0 && cmp(current[keyIx].end || current[keyIx].key, key) >= 0)) {
873
872
  return keyIx;
874
873
  }
875
874
  const appliedChange = [];
@@ -1012,11 +1011,12 @@ function getNewerChange(node, base) {
1012
1011
  return node.version >= base.version ? node : null;
1013
1012
  }
1014
1013
  }
1015
- function setVersion(graph, version) {
1014
+ function setVersion(graph, version, onlyIfZero = false) {
1016
1015
  for (const node of graph) {
1017
- node.version = version;
1016
+ if (!onlyIfZero || !node.version)
1017
+ node.version = version;
1018
1018
  if (node.children)
1019
- setVersion(node.children, version);
1019
+ setVersion(node.children, version, onlyIfZero);
1020
1020
  }
1021
1021
  return graph;
1022
1022
  }
@@ -1043,9 +1043,11 @@ function finalize(graph, query, version = Date.now()) {
1043
1043
  merge(result, graph);
1044
1044
  if (query)
1045
1045
  result = slice(result, query).known || [];
1046
- result = setVersion(result, version);
1046
+ if (!version !== false)
1047
+ result = setVersion(result, version, true);
1047
1048
  return result;
1048
1049
  }
1050
+ const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
1049
1051
  function decode$1(nodes = [], { isGraph } = {}) {
1050
1052
  function decodeChildren(nodes2) {
1051
1053
  let result = [];
@@ -1061,6 +1063,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1061
1063
  result.push(...objects);
1062
1064
  }
1063
1065
  const putRanges = [];
1066
+ const prefixChildPuts = [];
1064
1067
  let lastNode = null;
1065
1068
  function addPutRange({ key, end }) {
1066
1069
  if (lastNode) {
@@ -1086,9 +1089,13 @@ function decode$1(nodes = [], { isGraph } = {}) {
1086
1089
  for (const node of nodes2) {
1087
1090
  if (isGraph && addPutRange(node))
1088
1091
  continue;
1089
- if (isPrefix(node))
1090
- pushResult(...decodePrefixNode(node));
1091
- else if (isGraph && isRange(node))
1092
+ if (isPrefix(node)) {
1093
+ const decodedChildren = decodePrefixNode(node);
1094
+ if (PRE_CHI_PUT in decodedChildren) {
1095
+ prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
1096
+ }
1097
+ pushResult(...decodedChildren);
1098
+ } else if (isGraph && isRange(node))
1092
1099
  pushResult(decodeRangeNode(node));
1093
1100
  else if (isBranch(node))
1094
1101
  pushResult(decodeBranchNode(node));
@@ -1122,15 +1129,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
1122
1129
  Object.defineProperty(result, "$put", { value: true });
1123
1130
  } else {
1124
1131
  Object.defineProperty(result, "$put", {
1125
- value: putRanges.map((rNode) => decode$3(rNode))
1132
+ value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
1126
1133
  });
1127
1134
  }
1135
+ } else if (prefixChildPuts.length) {
1136
+ Object.defineProperty(result, "$put", { value: prefixChildPuts });
1128
1137
  }
1129
1138
  return result;
1130
1139
  }
1131
1140
  function decodePrefixNode(node) {
1132
1141
  let args = decode$3(node);
1133
- if (args === "")
1142
+ if (!args)
1134
1143
  args = {};
1135
1144
  if (typeof args === "string") {
1136
1145
  throw Error("decode.unencoded_prefix: " + args);
@@ -1160,6 +1169,16 @@ function decode$1(nodes = [], { isGraph } = {}) {
1160
1169
  }
1161
1170
  child.$key = { ...args, ...child.$key };
1162
1171
  }
1172
+ if (children.$put === true) {
1173
+ children[PRE_CHI_PUT] = [{ ...args, $all: true }];
1174
+ } else if (Array.isArray(children.$put)) {
1175
+ children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
1176
+ ...args,
1177
+ ...rarg
1178
+ }));
1179
+ } else if (isDef(children.$put)) {
1180
+ children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
1181
+ }
1163
1182
  return children;
1164
1183
  }
1165
1184
  function decodeBranchNode(node) {
@@ -1202,7 +1221,7 @@ function decorate(rootGraph, rootQuery) {
1202
1221
  query = [query];
1203
1222
  let graph;
1204
1223
  if (query.$ref) {
1205
- const { $ref, ...props } = query;
1224
+ const { $ref, ...props2 } = query;
1206
1225
  const [range, filter] = splitRef($ref);
1207
1226
  const path = encode$2($ref);
1208
1227
  const targetPlumGraph = unwrap(rootGraph, path);
@@ -1211,7 +1230,7 @@ function decorate(rootGraph, rootQuery) {
1211
1230
  targetPlumGraph[PRE] = filter;
1212
1231
  graph = construct(
1213
1232
  targetPlumGraph,
1214
- range ? { $key: range, ...props } : props
1233
+ range ? { $key: range, ...props2 } : props2
1215
1234
  );
1216
1235
  Object.defineProperty(graph, "$ref", { value: $ref });
1217
1236
  }
@@ -1221,8 +1240,8 @@ function decorate(rootGraph, rootQuery) {
1221
1240
  if (!(item == null ? void 0 : item.$key)) {
1222
1241
  return construct(descend(plumGraph, i), item);
1223
1242
  }
1224
- const { $key, $chi, ...props } = item;
1225
- const subQuery = $chi || (isEmpty(props) ? 1 : props);
1243
+ const { $key, $chi, ...props2 } = item;
1244
+ const subQuery = $chi || (isEmpty(props2) ? 1 : props2);
1226
1245
  if (!isPlainObject($key) || !splitArgs($key)[0]) {
1227
1246
  return construct(descend(plumGraph, $key), subQuery);
1228
1247
  }
@@ -1388,25 +1407,86 @@ function decode(string, start = 0) {
1388
1407
  }
1389
1408
  return addStringify(new Uint8Array(buffer));
1390
1409
  }
1391
- function serialize(obj) {
1392
- return JSON.stringify(obj, (_key, value) => {
1393
- return ArrayBuffer.isView(value) ? "\0" + encode$1(value) : value;
1394
- });
1410
+ const props = [
1411
+ "end",
1412
+ "version",
1413
+ "limit",
1414
+ "value",
1415
+ "path",
1416
+ "prefix",
1417
+ "children"
1418
+ ];
1419
+ function serializeKey(key) {
1420
+ if (key[0] === STR) {
1421
+ const last = key[key.length - 1];
1422
+ if (last !== 0 && last !== 255) {
1423
+ return decode$4(key);
1424
+ }
1425
+ }
1426
+ return "\0" + encode$1(key);
1395
1427
  }
1396
- function deserialize(str) {
1397
- return JSON.parse(
1398
- str,
1399
- (_key, value) => typeof value === "string" && value[0] === "\0" ? decode(value.slice(1)) : value
1428
+ function deserializeKey(key) {
1429
+ if (key[0] === "\0")
1430
+ return decode(key.slice(1));
1431
+ return encode$4(key);
1432
+ }
1433
+ function pack(children, parentVersion) {
1434
+ if (!Array.isArray(children))
1435
+ return children;
1436
+ const array = children.map(
1437
+ (node) => props.reduce(
1438
+ (array2, prop, i) => {
1439
+ if (!(prop in node))
1440
+ return array2;
1441
+ let value = node[prop];
1442
+ if (prop === "version" && value === parentVersion)
1443
+ return array2;
1444
+ if (prop === "children")
1445
+ value = pack(value, node.version);
1446
+ if (prop === "end")
1447
+ value = serializeKey(value);
1448
+ if (prop === "path")
1449
+ value = value.map(serializeKey);
1450
+ array2[1] |= 1 << i;
1451
+ array2.push(value);
1452
+ return array2;
1453
+ },
1454
+ [serializeKey(node.key), 0]
1455
+ )
1400
1456
  );
1457
+ return array;
1458
+ }
1459
+ function unpack(children, parentVersion) {
1460
+ if (!Array.isArray(children))
1461
+ return children;
1462
+ const node = children.map(
1463
+ ([key, type, ...values]) => props.reduce(
1464
+ (node2, prop, i) => {
1465
+ if (!(type & 1 << i))
1466
+ return node2;
1467
+ let value = values.shift();
1468
+ if (prop === "children")
1469
+ value = unpack(value, node2.version);
1470
+ if (prop === "end")
1471
+ value = deserializeKey(value);
1472
+ if (prop === "path")
1473
+ value = value.map(deserializeKey);
1474
+ node2[prop] = value;
1475
+ return node2;
1476
+ },
1477
+ { key: deserializeKey(key), version: parentVersion }
1478
+ )
1479
+ );
1480
+ return node;
1401
1481
  }
1402
1482
  const ROOT_KEY = Symbol();
1403
1483
  function encode(value, { version, isGraph } = {}) {
1404
1484
  var _a;
1405
1485
  const links = [];
1406
- function pushLink($ref, $ver, props, $val, $chi) {
1486
+ function pushLink($ref, $ver, props2, $val, $chi) {
1407
1487
  const [range, _] = splitRef($ref);
1408
- let children = !isEmpty(props) ? makeNode2(
1409
- range ? [{ $key: range, ...props }] : props,
1488
+ let children = !isEmpty(props2) ? makeNode2(
1489
+ range ? [{ $key: range, ...props2 }] : props2,
1410
1490
  void 0,
1411
1491
  $ver
1412
1492
  ).children : isDef($chi) ? makeNode2(
@@ -1419,41 +1499,75 @@ function encode(value, { version, isGraph } = {}) {
1419
1499
  }
1420
1500
  }
1421
1501
  const combine = isGraph ? merge : add;
1422
- function makeNode2(object, key, ver) {
1502
+ function makeNode2(object, key, ver, parentPuts = []) {
1423
1503
  var _a2;
1424
1504
  if (!isDef(object))
1425
1505
  return;
1426
1506
  if (typeof object === "object" && object && isEmpty(object))
1427
1507
  return;
1428
- const { $key, $ver, $ref, $val, $chi, $put, ...props } = object || {};
1508
+ const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1429
1509
  if (isDef($ver))
1430
1510
  ver = $ver;
1431
1511
  if (isPlainObject($key)) {
1432
1512
  const [page, filter] = splitArgs($key);
1433
- if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props))) {
1434
- const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1435
- if (!filter)
1436
- node2.key = MIN_KEY;
1437
- node2.prefix = true;
1438
- return node2;
1513
+ if (page) {
1514
+ if (typeof object === "object" && object && !Array.isArray(object)) {
1515
+ object = {
1516
+ ...Object.fromEntries(
1517
+ Object.entries({
1518
+ $key,
1519
+ $ver,
1520
+ $ref,
1521
+ $val,
1522
+ $chi,
1523
+ $put
1524
+ }).filter(([_, val]) => isDef(val))
1525
+ ),
1526
+ ...props2
1527
+ };
1528
+ }
1529
+ const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual__default.default(filter, putFilter)).map(([range]) => range);
1530
+ if (isGraph && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1531
+ object.$key = filter || {};
1532
+ object.$put = foundPuts;
1533
+ const node2 = makeNode2(object, key, ver);
1534
+ if (!filter)
1535
+ node2.key = MIN_KEY;
1536
+ node2.prefix = true;
1537
+ return node2;
1538
+ }
1539
+ if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
1540
+ object.$key = isDef(page.$cursor) ? page.$cursor : page;
1541
+ const wrapper = { $key: filter || {}, $chi: [object] };
1542
+ if (isGraph)
1543
+ wrapper.$put = foundPuts;
1544
+ const node2 = makeNode2(wrapper, key, ver);
1545
+ if (!filter)
1546
+ node2.key = MIN_KEY;
1547
+ node2.prefix = true;
1548
+ return node2;
1549
+ }
1439
1550
  }
1440
- if ((!isDef(key) || Number.isInteger(key)) && page && (filter || isDef(page.$cursor))) {
1441
- const node2 = makeNode2(
1442
- {
1443
- $key: filter || {},
1444
- $chi: [
1445
- { ...object, $key: isDef(page.$cursor) ? page.$cursor : page }
1446
- ]
1447
- },
1448
- key,
1449
- ver
1450
- );
1451
- if (!filter)
1452
- node2.key = MIN_KEY;
1453
- node2.prefix = true;
1454
- return node2;
1551
+ }
1552
+ let putQuery = [], prefixPuts = [];
1553
+ if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1554
+ putQuery = [encode$3({ $since: 0, $until: Infinity })];
1555
+ }
1556
+ function classifyPut(put) {
1557
+ const [range, filter] = splitArgs(put);
1558
+ if (filter) {
1559
+ prefixPuts.push([range, filter]);
1560
+ } else {
1561
+ putQuery.push(encode$3(put));
1455
1562
  }
1456
1563
  }
1564
+ if ($put === true) {
1565
+ putQuery = null;
1566
+ } else if (Array.isArray($put)) {
1567
+ $put.forEach(classifyPut);
1568
+ } else if (isDef($put)) {
1569
+ classifyPut($put);
1570
+ }
1457
1571
  if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1458
1572
  key = $key;
1459
1573
  const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
@@ -1461,26 +1575,28 @@ function encode(value, { version, isGraph } = {}) {
1461
1575
  if (object === null) {
1462
1576
  node.end = node.key;
1463
1577
  } else if (isDef($key) && isDef(key) && key !== $key) {
1464
- node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1578
+ node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
1579
+ Boolean
1580
+ );
1465
1581
  return node;
1466
1582
  } else if ($ref) {
1467
- pushLink($ref, node.version, props, $val, $chi);
1583
+ pushLink($ref, node.version, props2, $val, $chi);
1468
1584
  if (!isGraph)
1469
1585
  return;
1470
1586
  node.path = encode$2($ref);
1471
1587
  } else if ($val === true) {
1472
- node.value = props;
1588
+ node.value = props2;
1473
1589
  } else if (isDef($val)) {
1474
1590
  node.value = $val;
1475
1591
  } else if (typeof object !== "object") {
1476
1592
  node.value = isGraph || typeof object === "number" ? object : 1;
1477
1593
  } else if (isDef($chi)) {
1478
- const children = $chi.map((obj) => makeNode2(obj, void 0, ver)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1594
+ const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1479
1595
  if (children.length) {
1480
1596
  node.children = children;
1481
1597
  }
1482
1598
  } else if (Array.isArray(object)) {
1483
- const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1599
+ const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
1484
1600
  combine(acc, [it]);
1485
1601
  return acc;
1486
1602
  }, []);
@@ -1488,7 +1604,7 @@ function encode(value, { version, isGraph } = {}) {
1488
1604
  node.children = children;
1489
1605
  }
1490
1606
  } else {
1491
- const children = Object.keys(props).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1607
+ const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1492
1608
  if (children.length) {
1493
1609
  node.children = children;
1494
1610
  } else if (isGraph) {
@@ -1502,19 +1618,8 @@ function encode(value, { version, isGraph } = {}) {
1502
1618
  node.value = 1;
1503
1619
  }
1504
1620
  }
1505
- let putQuery;
1506
- if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1507
- putQuery = [encode$3({ $since: 0, $until: Infinity })];
1508
- }
1509
- if ($put === true) {
1510
- putQuery = null;
1511
- } else if (Array.isArray($put)) {
1512
- putQuery = $put.map((arg) => encode$3(arg));
1513
- } else if (isDef($put)) {
1514
- putQuery = [encode$3($put)];
1515
- }
1516
- if (isGraph && isDef(putQuery)) {
1517
- node.children = finalize(node.children || [], putQuery, node.version);
1621
+ if (isGraph && (putQuery === null || putQuery.length)) {
1622
+ node.children = finalize(node.children || [], putQuery, false);
1518
1623
  }
1519
1624
  if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
1520
1625
  return node;
@@ -1529,7 +1634,9 @@ function encode(value, { version, isGraph } = {}) {
1529
1634
  return result;
1530
1635
  }
1531
1636
  function encodeGraph(obj, version = Date.now()) {
1532
- return encode(obj, { version, isGraph: true });
1637
+ const encoded = encode(obj, { version, isGraph: true });
1638
+ const versioned = setVersion(encoded, version, true);
1639
+ return versioned;
1533
1640
  }
1534
1641
  function encodeQuery(obj, version = 0) {
1535
1642
  return encode(obj, { version, isGraph: false });
@@ -1652,7 +1759,6 @@ exports.decodePath = decode$2;
1652
1759
  exports.decodeQuery = decodeQuery;
1653
1760
  exports.decodeValue = decode$4;
1654
1761
  exports.decorate = decorate;
1655
- exports.deserialize = deserialize;
1656
1762
  exports.encodeArgs = encode$3;
1657
1763
  exports.encodeGraph = encodeGraph;
1658
1764
  exports.encodePath = encode$2;
@@ -1685,13 +1791,14 @@ exports.makeWatcher = makeWatcher;
1685
1791
  exports.merge = merge;
1686
1792
  exports.mergeObject = mergeObject;
1687
1793
  exports.mergeStreams = mergeStreams;
1794
+ exports.pack = pack;
1688
1795
  exports.remove = remove;
1689
- exports.serialize = serialize;
1690
1796
  exports.setVersion = setVersion;
1691
1797
  exports.sieve = sieve;
1692
1798
  exports.slice = slice;
1693
1799
  exports.splitArgs = splitArgs;
1694
1800
  exports.splitRef = splitRef;
1801
+ exports.unpack = unpack;
1695
1802
  exports.unwrap = unwrap;
1696
1803
  exports.unwrapObject = unwrapObject;
1697
1804
  exports.wrap = wrap;
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
+ import isEqual from "lodash/isEqual.js";
1
2
  import mergeIterators from "merge-async-iterators";
2
3
  import { makeStream } from "@graffy/stream";
3
- import isEqual from "lodash/isEqual.js";
4
4
  const textEncoder = new TextEncoder();
5
5
  const textDecoder = new TextDecoder("utf-8");
6
6
  function encode$6(string) {
@@ -114,6 +114,7 @@ const stringifyDescriptor = {
114
114
  };
115
115
  function addStringify(buffer) {
116
116
  Object.defineProperties(buffer, {
117
+ toJSON: stringifyDescriptor,
117
118
  toString: stringifyDescriptor,
118
119
  [Symbol.for("nodejs.util.inspect.custom")]: stringifyDescriptor
119
120
  });
@@ -385,8 +386,6 @@ function encode$3(arg) {
385
386
  return node;
386
387
  }
387
388
  function decode$3(node) {
388
- if (typeof node === "string")
389
- throw Error("why?");
390
389
  const { key, end, limit } = node;
391
390
  errIf("no_key", !isDef(key));
392
391
  errIf("limit_without_end", isDef(limit) && !isDef(end));
@@ -634,10 +633,10 @@ function getNewer(node, base) {
634
633
  }
635
634
  }
636
635
  const IS_VAL = Symbol("IS_VAL");
637
- function makeNode(seg, props) {
636
+ function makeNode(seg, props2) {
638
637
  if (ArrayBuffer.isView(seg))
639
- return { key: seg, ...props };
640
- return { ...seg, ...props };
638
+ return { key: seg, ...props2 };
639
+ return { ...seg, ...props2 };
641
640
  }
642
641
  function wrapValue(value, path, version = 0) {
643
642
  const node = makeNode(path[path.length - 1], { value, version });
@@ -864,7 +863,7 @@ function insertRange(current, change, result, start = 0) {
864
863
  const { key, end } = change;
865
864
  const keyIx = findFirst(current, key, start);
866
865
  const endIx = findLast(current, end, keyIx);
867
- if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, key) <= 0 && cmp(current[keyIx].end, end) >= 0)) {
866
+ if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, end) <= 0 && cmp(current[keyIx].end || current[keyIx].key, key) >= 0)) {
868
867
  return keyIx;
869
868
  }
870
869
  const appliedChange = [];
@@ -1007,11 +1006,12 @@ function getNewerChange(node, base) {
1007
1006
  return node.version >= base.version ? node : null;
1008
1007
  }
1009
1008
  }
1010
- function setVersion(graph, version) {
1009
+ function setVersion(graph, version, onlyIfZero = false) {
1011
1010
  for (const node of graph) {
1012
- node.version = version;
1011
+ if (!onlyIfZero || !node.version)
1012
+ node.version = version;
1013
1013
  if (node.children)
1014
- setVersion(node.children, version);
1014
+ setVersion(node.children, version, onlyIfZero);
1015
1015
  }
1016
1016
  return graph;
1017
1017
  }
@@ -1038,9 +1038,11 @@ function finalize(graph, query, version = Date.now()) {
1038
1038
  merge(result, graph);
1039
1039
  if (query)
1040
1040
  result = slice(result, query).known || [];
1041
- result = setVersion(result, version);
1041
+ if (!version !== false)
1042
+ result = setVersion(result, version, true);
1042
1043
  return result;
1043
1044
  }
1045
+ const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
1044
1046
  function decode$1(nodes = [], { isGraph } = {}) {
1045
1047
  function decodeChildren(nodes2) {
1046
1048
  let result = [];
@@ -1056,6 +1058,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1056
1058
  result.push(...objects);
1057
1059
  }
1058
1060
  const putRanges = [];
1061
+ const prefixChildPuts = [];
1059
1062
  let lastNode = null;
1060
1063
  function addPutRange({ key, end }) {
1061
1064
  if (lastNode) {
@@ -1081,9 +1084,13 @@ function decode$1(nodes = [], { isGraph } = {}) {
1081
1084
  for (const node of nodes2) {
1082
1085
  if (isGraph && addPutRange(node))
1083
1086
  continue;
1084
- if (isPrefix(node))
1085
- pushResult(...decodePrefixNode(node));
1086
- else if (isGraph && isRange(node))
1087
+ if (isPrefix(node)) {
1088
+ const decodedChildren = decodePrefixNode(node);
1089
+ if (PRE_CHI_PUT in decodedChildren) {
1090
+ prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
1091
+ }
1092
+ pushResult(...decodedChildren);
1093
+ } else if (isGraph && isRange(node))
1087
1094
  pushResult(decodeRangeNode(node));
1088
1095
  else if (isBranch(node))
1089
1096
  pushResult(decodeBranchNode(node));
@@ -1117,15 +1124,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
1117
1124
  Object.defineProperty(result, "$put", { value: true });
1118
1125
  } else {
1119
1126
  Object.defineProperty(result, "$put", {
1120
- value: putRanges.map((rNode) => decode$3(rNode))
1127
+ value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
1121
1128
  });
1122
1129
  }
1130
+ } else if (prefixChildPuts.length) {
1131
+ Object.defineProperty(result, "$put", { value: prefixChildPuts });
1123
1132
  }
1124
1133
  return result;
1125
1134
  }
1126
1135
  function decodePrefixNode(node) {
1127
1136
  let args = decode$3(node);
1128
- if (args === "")
1137
+ if (!args)
1129
1138
  args = {};
1130
1139
  if (typeof args === "string") {
1131
1140
  throw Error("decode.unencoded_prefix: " + args);
@@ -1155,6 +1164,16 @@ function decode$1(nodes = [], { isGraph } = {}) {
1155
1164
  }
1156
1165
  child.$key = { ...args, ...child.$key };
1157
1166
  }
1167
+ if (children.$put === true) {
1168
+ children[PRE_CHI_PUT] = [{ ...args, $all: true }];
1169
+ } else if (Array.isArray(children.$put)) {
1170
+ children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
1171
+ ...args,
1172
+ ...rarg
1173
+ }));
1174
+ } else if (isDef(children.$put)) {
1175
+ children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
1176
+ }
1158
1177
  return children;
1159
1178
  }
1160
1179
  function decodeBranchNode(node) {
@@ -1197,7 +1216,7 @@ function decorate(rootGraph, rootQuery) {
1197
1216
  query = [query];
1198
1217
  let graph;
1199
1218
  if (query.$ref) {
1200
- const { $ref, ...props } = query;
1219
+ const { $ref, ...props2 } = query;
1201
1220
  const [range, filter] = splitRef($ref);
1202
1221
  const path = encode$2($ref);
1203
1222
  const targetPlumGraph = unwrap(rootGraph, path);
@@ -1206,7 +1225,7 @@ function decorate(rootGraph, rootQuery) {
1206
1225
  targetPlumGraph[PRE] = filter;
1207
1226
  graph = construct(
1208
1227
  targetPlumGraph,
1209
- range ? { $key: range, ...props } : props
1228
+ range ? { $key: range, ...props2 } : props2
1210
1229
  );
1211
1230
  Object.defineProperty(graph, "$ref", { value: $ref });
1212
1231
  }
@@ -1216,8 +1235,8 @@ function decorate(rootGraph, rootQuery) {
1216
1235
  if (!(item == null ? void 0 : item.$key)) {
1217
1236
  return construct(descend(plumGraph, i), item);
1218
1237
  }
1219
- const { $key, $chi, ...props } = item;
1220
- const subQuery = $chi || (isEmpty(props) ? 1 : props);
1238
+ const { $key, $chi, ...props2 } = item;
1239
+ const subQuery = $chi || (isEmpty(props2) ? 1 : props2);
1221
1240
  if (!isPlainObject($key) || !splitArgs($key)[0]) {
1222
1241
  return construct(descend(plumGraph, $key), subQuery);
1223
1242
  }
@@ -1383,25 +1402,86 @@ function decode(string, start = 0) {
1383
1402
  }
1384
1403
  return addStringify(new Uint8Array(buffer));
1385
1404
  }
1386
- function serialize(obj) {
1387
- return JSON.stringify(obj, (_key, value) => {
1388
- return ArrayBuffer.isView(value) ? "\0" + encode$1(value) : value;
1389
- });
1405
+ const props = [
1406
+ "end",
1407
+ "version",
1408
+ "limit",
1409
+ "value",
1410
+ "path",
1411
+ "prefix",
1412
+ "children"
1413
+ ];
1414
+ function serializeKey(key) {
1415
+ if (key[0] === STR) {
1416
+ const last = key[key.length - 1];
1417
+ if (last !== 0 && last !== 255) {
1418
+ return decode$4(key);
1419
+ }
1420
+ }
1421
+ return "\0" + encode$1(key);
1422
+ }
1423
+ function deserializeKey(key) {
1424
+ if (key[0] === "\0")
1425
+ return decode(key.slice(1));
1426
+ return encode$4(key);
1427
+ }
1428
+ function pack(children, parentVersion) {
1429
+ if (!Array.isArray(children))
1430
+ return children;
1431
+ const array = children.map(
1432
+ (node) => props.reduce(
1433
+ (array2, prop, i) => {
1434
+ if (!(prop in node))
1435
+ return array2;
1436
+ let value = node[prop];
1437
+ if (prop === "version" && value === parentVersion)
1438
+ return array2;
1439
+ if (prop === "children")
1440
+ value = pack(value, node.version);
1441
+ if (prop === "end")
1442
+ value = serializeKey(value);
1443
+ if (prop === "path")
1444
+ value = value.map(serializeKey);
1445
+ array2[1] |= 1 << i;
1446
+ array2.push(value);
1447
+ return array2;
1448
+ },
1449
+ [serializeKey(node.key), 0]
1450
+ )
1451
+ );
1452
+ return array;
1390
1453
  }
1391
- function deserialize(str) {
1392
- return JSON.parse(
1393
- str,
1394
- (_key, value) => typeof value === "string" && value[0] === "\0" ? decode(value.slice(1)) : value
1454
+ function unpack(children, parentVersion) {
1455
+ if (!Array.isArray(children))
1456
+ return children;
1457
+ const node = children.map(
1458
+ ([key, type, ...values]) => props.reduce(
1459
+ (node2, prop, i) => {
1460
+ if (!(type & 1 << i))
1461
+ return node2;
1462
+ let value = values.shift();
1463
+ if (prop === "children")
1464
+ value = unpack(value, node2.version);
1465
+ if (prop === "end")
1466
+ value = deserializeKey(value);
1467
+ if (prop === "path")
1468
+ value = value.map(deserializeKey);
1469
+ node2[prop] = value;
1470
+ return node2;
1471
+ },
1472
+ { key: deserializeKey(key), version: parentVersion }
1473
+ )
1395
1474
  );
1475
+ return node;
1396
1476
  }
1397
1477
  const ROOT_KEY = Symbol();
1398
1478
  function encode(value, { version, isGraph } = {}) {
1399
1479
  var _a;
1400
1480
  const links = [];
1401
- function pushLink($ref, $ver, props, $val, $chi) {
1481
+ function pushLink($ref, $ver, props2, $val, $chi) {
1402
1482
  const [range, _] = splitRef($ref);
1403
- let children = !isEmpty(props) ? makeNode2(
1404
- range ? [{ $key: range, ...props }] : props,
1483
+ let children = !isEmpty(props2) ? makeNode2(
1484
+ range ? [{ $key: range, ...props2 }] : props2,
1405
1485
  void 0,
1406
1486
  $ver
1407
1487
  ).children : isDef($chi) ? makeNode2(
@@ -1414,41 +1494,75 @@ function encode(value, { version, isGraph } = {}) {
1414
1494
  }
1415
1495
  }
1416
1496
  const combine = isGraph ? merge : add;
1417
- function makeNode2(object, key, ver) {
1497
+ function makeNode2(object, key, ver, parentPuts = []) {
1418
1498
  var _a2;
1419
1499
  if (!isDef(object))
1420
1500
  return;
1421
1501
  if (typeof object === "object" && object && isEmpty(object))
1422
1502
  return;
1423
- const { $key, $ver, $ref, $val, $chi, $put, ...props } = object || {};
1503
+ const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1424
1504
  if (isDef($ver))
1425
1505
  ver = $ver;
1426
1506
  if (isPlainObject($key)) {
1427
1507
  const [page, filter] = splitArgs($key);
1428
- if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props))) {
1429
- const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1430
- if (!filter)
1431
- node2.key = MIN_KEY;
1432
- node2.prefix = true;
1433
- return node2;
1508
+ if (page) {
1509
+ if (typeof object === "object" && object && !Array.isArray(object)) {
1510
+ object = {
1511
+ ...Object.fromEntries(
1512
+ Object.entries({
1513
+ $key,
1514
+ $ver,
1515
+ $ref,
1516
+ $val,
1517
+ $chi,
1518
+ $put
1519
+ }).filter(([_, val]) => isDef(val))
1520
+ ),
1521
+ ...props2
1522
+ };
1523
+ }
1524
+ const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual(filter, putFilter)).map(([range]) => range);
1525
+ if (isGraph && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1526
+ object.$key = filter || {};
1527
+ object.$put = foundPuts;
1528
+ const node2 = makeNode2(object, key, ver);
1529
+ if (!filter)
1530
+ node2.key = MIN_KEY;
1531
+ node2.prefix = true;
1532
+ return node2;
1533
+ }
1534
+ if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
1535
+ object.$key = isDef(page.$cursor) ? page.$cursor : page;
1536
+ const wrapper = { $key: filter || {}, $chi: [object] };
1537
+ if (isGraph)
1538
+ wrapper.$put = foundPuts;
1539
+ const node2 = makeNode2(wrapper, key, ver);
1540
+ if (!filter)
1541
+ node2.key = MIN_KEY;
1542
+ node2.prefix = true;
1543
+ return node2;
1544
+ }
1434
1545
  }
1435
- if ((!isDef(key) || Number.isInteger(key)) && page && (filter || isDef(page.$cursor))) {
1436
- const node2 = makeNode2(
1437
- {
1438
- $key: filter || {},
1439
- $chi: [
1440
- { ...object, $key: isDef(page.$cursor) ? page.$cursor : page }
1441
- ]
1442
- },
1443
- key,
1444
- ver
1445
- );
1446
- if (!filter)
1447
- node2.key = MIN_KEY;
1448
- node2.prefix = true;
1449
- return node2;
1546
+ }
1547
+ let putQuery = [], prefixPuts = [];
1548
+ if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1549
+ putQuery = [encode$3({ $since: 0, $until: Infinity })];
1550
+ }
1551
+ function classifyPut(put) {
1552
+ const [range, filter] = splitArgs(put);
1553
+ if (filter) {
1554
+ prefixPuts.push([range, filter]);
1555
+ } else {
1556
+ putQuery.push(encode$3(put));
1450
1557
  }
1451
1558
  }
1559
+ if ($put === true) {
1560
+ putQuery = null;
1561
+ } else if (Array.isArray($put)) {
1562
+ $put.forEach(classifyPut);
1563
+ } else if (isDef($put)) {
1564
+ classifyPut($put);
1565
+ }
1452
1566
  if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1453
1567
  key = $key;
1454
1568
  const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
@@ -1456,26 +1570,28 @@ function encode(value, { version, isGraph } = {}) {
1456
1570
  if (object === null) {
1457
1571
  node.end = node.key;
1458
1572
  } else if (isDef($key) && isDef(key) && key !== $key) {
1459
- node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1573
+ node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
1574
+ Boolean
1575
+ );
1460
1576
  return node;
1461
1577
  } else if ($ref) {
1462
- pushLink($ref, node.version, props, $val, $chi);
1578
+ pushLink($ref, node.version, props2, $val, $chi);
1463
1579
  if (!isGraph)
1464
1580
  return;
1465
1581
  node.path = encode$2($ref);
1466
1582
  } else if ($val === true) {
1467
- node.value = props;
1583
+ node.value = props2;
1468
1584
  } else if (isDef($val)) {
1469
1585
  node.value = $val;
1470
1586
  } else if (typeof object !== "object") {
1471
1587
  node.value = isGraph || typeof object === "number" ? object : 1;
1472
1588
  } else if (isDef($chi)) {
1473
- const children = $chi.map((obj) => makeNode2(obj, void 0, ver)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1589
+ const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1474
1590
  if (children.length) {
1475
1591
  node.children = children;
1476
1592
  }
1477
1593
  } else if (Array.isArray(object)) {
1478
- const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1594
+ const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
1479
1595
  combine(acc, [it]);
1480
1596
  return acc;
1481
1597
  }, []);
@@ -1483,7 +1599,7 @@ function encode(value, { version, isGraph } = {}) {
1483
1599
  node.children = children;
1484
1600
  }
1485
1601
  } else {
1486
- const children = Object.keys(props).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1602
+ const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1487
1603
  if (children.length) {
1488
1604
  node.children = children;
1489
1605
  } else if (isGraph) {
@@ -1497,19 +1613,8 @@ function encode(value, { version, isGraph } = {}) {
1497
1613
  node.value = 1;
1498
1614
  }
1499
1615
  }
1500
- let putQuery;
1501
- if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1502
- putQuery = [encode$3({ $since: 0, $until: Infinity })];
1503
- }
1504
- if ($put === true) {
1505
- putQuery = null;
1506
- } else if (Array.isArray($put)) {
1507
- putQuery = $put.map((arg) => encode$3(arg));
1508
- } else if (isDef($put)) {
1509
- putQuery = [encode$3($put)];
1510
- }
1511
- if (isGraph && isDef(putQuery)) {
1512
- node.children = finalize(node.children || [], putQuery, node.version);
1616
+ if (isGraph && (putQuery === null || putQuery.length)) {
1617
+ node.children = finalize(node.children || [], putQuery, false);
1513
1618
  }
1514
1619
  if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
1515
1620
  return node;
@@ -1524,7 +1629,9 @@ function encode(value, { version, isGraph } = {}) {
1524
1629
  return result;
1525
1630
  }
1526
1631
  function encodeGraph(obj, version = Date.now()) {
1527
- return encode(obj, { version, isGraph: true });
1632
+ const encoded = encode(obj, { version, isGraph: true });
1633
+ const versioned = setVersion(encoded, version, true);
1634
+ return versioned;
1528
1635
  }
1529
1636
  function encodeQuery(obj, version = 0) {
1530
1637
  return encode(obj, { version, isGraph: false });
@@ -1648,7 +1755,6 @@ export {
1648
1755
  decodeQuery,
1649
1756
  decode$4 as decodeValue,
1650
1757
  decorate,
1651
- deserialize,
1652
1758
  encode$3 as encodeArgs,
1653
1759
  encodeGraph,
1654
1760
  encode$2 as encodePath,
@@ -1681,13 +1787,14 @@ export {
1681
1787
  merge,
1682
1788
  mergeObject,
1683
1789
  mergeStreams,
1790
+ pack,
1684
1791
  remove,
1685
- serialize,
1686
1792
  setVersion,
1687
1793
  sieve,
1688
1794
  slice,
1689
1795
  splitArgs,
1690
1796
  splitRef,
1797
+ unpack,
1691
1798
  unwrap,
1692
1799
  unwrapObject,
1693
1800
  wrap,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/common",
3
3
  "description": "Common libraries that used by various Graffy modules.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.16.0-alpha.1",
5
+ "version": "0.16.0-alpha.11",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -18,6 +18,6 @@
18
18
  "dependencies": {
19
19
  "lodash": "^4.17.19",
20
20
  "merge-async-iterators": "^0.2.1",
21
- "@graffy/stream": "0.16.0-alpha.1"
21
+ "@graffy/stream": "0.16.0-alpha.11"
22
22
  }
23
23
  }
@@ -1,6 +1,6 @@
1
1
  export { default as makeId } from "./id.js";
2
2
  export { default as decorate } from "./decorate.js";
3
- export * from "./serialize.js";
3
+ export * from "./pack.js";
4
4
  export * from "./encodeTree.js";
5
5
  export * from "./decodeTree.js";
6
6
  export { encode as encodeValue, decode as decodeValue } from "./struct.js";
@@ -0,0 +1,2 @@
1
+ export function pack(children: any, parentVersion: any): any;
2
+ export function unpack(children: any, parentVersion: any): any;
@@ -1,4 +1,11 @@
1
- export default function finalize(graph: any, query: any, version?: number): {
1
+ /**
2
+ *
3
+ * @param {any} graph
4
+ * @param {any} query
5
+ * @param {number | false} version
6
+ * @returns any
7
+ */
8
+ export default function finalize(graph: any, query: any, version?: number | false): {
2
9
  key: Uint8Array;
3
10
  end: Uint8Array;
4
11
  version: number;
@@ -1 +1 @@
1
- export default function setVersion(graph: any, version: any): any;
1
+ export default function setVersion(graph: any, version: any, onlyIfZero?: boolean): any;
@@ -1,2 +0,0 @@
1
- export function serialize(obj: any): string;
2
- export function deserialize(str: any): any;