@graffy/common 0.16.0-alpha.8 → 0.16.0-alpha.9

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) {
@@ -391,8 +391,6 @@ function encode$3(arg) {
391
391
  return node;
392
392
  }
393
393
  function decode$3(node) {
394
- if (typeof node === "string")
395
- throw Error("why?");
396
394
  const { key, end, limit } = node;
397
395
  errIf("no_key", !isDef(key));
398
396
  errIf("limit_without_end", isDef(limit) && !isDef(end));
@@ -1039,14 +1037,16 @@ function getKnown(graph, version = 0) {
1039
1037
  }
1040
1038
  return query;
1041
1039
  }
1042
- function finalize(graph, query, version = Date.now()) {
1040
+ function finalize(graph, query, version = Date.now(), suppressSetVersion = false) {
1043
1041
  let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
1044
1042
  merge(result, graph);
1045
1043
  if (query)
1046
1044
  result = slice(result, query).known || [];
1047
- result = setVersion(result, version);
1045
+ if (!suppressSetVersion)
1046
+ result = setVersion(result, version);
1048
1047
  return result;
1049
1048
  }
1049
+ const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
1050
1050
  function decode$1(nodes = [], { isGraph } = {}) {
1051
1051
  function decodeChildren(nodes2) {
1052
1052
  let result = [];
@@ -1062,6 +1062,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1062
1062
  result.push(...objects);
1063
1063
  }
1064
1064
  const putRanges = [];
1065
+ const prefixChildPuts = [];
1065
1066
  let lastNode = null;
1066
1067
  function addPutRange({ key, end }) {
1067
1068
  if (lastNode) {
@@ -1087,9 +1088,13 @@ function decode$1(nodes = [], { isGraph } = {}) {
1087
1088
  for (const node of nodes2) {
1088
1089
  if (isGraph && addPutRange(node))
1089
1090
  continue;
1090
- if (isPrefix(node))
1091
- pushResult(...decodePrefixNode(node));
1092
- else if (isGraph && isRange(node))
1091
+ if (isPrefix(node)) {
1092
+ const decodedChildren = decodePrefixNode(node);
1093
+ if (PRE_CHI_PUT in decodedChildren) {
1094
+ prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
1095
+ }
1096
+ pushResult(...decodedChildren);
1097
+ } else if (isGraph && isRange(node))
1093
1098
  pushResult(decodeRangeNode(node));
1094
1099
  else if (isBranch(node))
1095
1100
  pushResult(decodeBranchNode(node));
@@ -1123,15 +1128,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
1123
1128
  Object.defineProperty(result, "$put", { value: true });
1124
1129
  } else {
1125
1130
  Object.defineProperty(result, "$put", {
1126
- value: putRanges.map((rNode) => decode$3(rNode))
1131
+ value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
1127
1132
  });
1128
1133
  }
1134
+ } else if (prefixChildPuts.length) {
1135
+ Object.defineProperty(result, "$put", { value: prefixChildPuts });
1129
1136
  }
1130
1137
  return result;
1131
1138
  }
1132
1139
  function decodePrefixNode(node) {
1133
1140
  let args = decode$3(node);
1134
- if (args === "")
1141
+ if (!args)
1135
1142
  args = {};
1136
1143
  if (typeof args === "string") {
1137
1144
  throw Error("decode.unencoded_prefix: " + args);
@@ -1161,6 +1168,16 @@ function decode$1(nodes = [], { isGraph } = {}) {
1161
1168
  }
1162
1169
  child.$key = { ...args, ...child.$key };
1163
1170
  }
1171
+ if (children.$put === true) {
1172
+ children[PRE_CHI_PUT] = [{ ...args, $all: true }];
1173
+ } else if (Array.isArray(children.$put)) {
1174
+ children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
1175
+ ...args,
1176
+ ...rarg
1177
+ }));
1178
+ } else if (isDef(children.$put)) {
1179
+ children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
1180
+ }
1164
1181
  return children;
1165
1182
  }
1166
1183
  function decodeBranchNode(node) {
@@ -1481,7 +1498,7 @@ function encode(value, { version, isGraph } = {}) {
1481
1498
  }
1482
1499
  }
1483
1500
  const combine = isGraph ? merge : add;
1484
- function makeNode2(object, key, ver) {
1501
+ function makeNode2(object, key, ver, parentPuts = []) {
1485
1502
  var _a2;
1486
1503
  if (!isDef(object))
1487
1504
  return;
@@ -1492,8 +1509,13 @@ function encode(value, { version, isGraph } = {}) {
1492
1509
  ver = $ver;
1493
1510
  if (isPlainObject($key)) {
1494
1511
  const [page, filter] = splitArgs($key);
1512
+ const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual__default.default(filter, putFilter)).map(([range]) => range);
1495
1513
  if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1496
- const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1514
+ const node2 = makeNode2(
1515
+ { ...object, $key: filter || {}, $put: foundPuts },
1516
+ key,
1517
+ ver
1518
+ );
1497
1519
  if (!filter)
1498
1520
  node2.key = MIN_KEY;
1499
1521
  node2.prefix = true;
@@ -1505,7 +1527,8 @@ function encode(value, { version, isGraph } = {}) {
1505
1527
  $key: filter || {},
1506
1528
  $chi: [
1507
1529
  { ...object, $key: isDef(page.$cursor) ? page.$cursor : page }
1508
- ]
1530
+ ],
1531
+ ...isGraph ? { $put: foundPuts } : {}
1509
1532
  },
1510
1533
  key,
1511
1534
  ver
@@ -1516,6 +1539,25 @@ function encode(value, { version, isGraph } = {}) {
1516
1539
  return node2;
1517
1540
  }
1518
1541
  }
1542
+ let putQuery = [], prefixPuts = [];
1543
+ if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1544
+ putQuery = [encode$3({ $since: 0, $until: Infinity })];
1545
+ }
1546
+ function classifyPut(put) {
1547
+ const [range, filter] = splitArgs(put);
1548
+ if (filter) {
1549
+ prefixPuts.push([range, filter]);
1550
+ } else {
1551
+ putQuery.push(encode$3(put));
1552
+ }
1553
+ }
1554
+ if ($put === true) {
1555
+ putQuery = null;
1556
+ } else if (Array.isArray($put)) {
1557
+ $put.forEach(classifyPut);
1558
+ } else if (isDef($put)) {
1559
+ classifyPut($put);
1560
+ }
1519
1561
  if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1520
1562
  key = $key;
1521
1563
  const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
@@ -1523,7 +1565,9 @@ function encode(value, { version, isGraph } = {}) {
1523
1565
  if (object === null) {
1524
1566
  node.end = node.key;
1525
1567
  } else if (isDef($key) && isDef(key) && key !== $key) {
1526
- node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1568
+ node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
1569
+ Boolean
1570
+ );
1527
1571
  return node;
1528
1572
  } else if ($ref) {
1529
1573
  pushLink($ref, node.version, props2, $val, $chi);
@@ -1537,12 +1581,12 @@ function encode(value, { version, isGraph } = {}) {
1537
1581
  } else if (typeof object !== "object") {
1538
1582
  node.value = isGraph || typeof object === "number" ? object : 1;
1539
1583
  } else if (isDef($chi)) {
1540
- const children = $chi.map((obj) => makeNode2(obj, void 0, ver)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1584
+ const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1541
1585
  if (children.length) {
1542
1586
  node.children = children;
1543
1587
  }
1544
1588
  } else if (Array.isArray(object)) {
1545
- const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1589
+ const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
1546
1590
  combine(acc, [it]);
1547
1591
  return acc;
1548
1592
  }, []);
@@ -1564,19 +1608,13 @@ function encode(value, { version, isGraph } = {}) {
1564
1608
  node.value = 1;
1565
1609
  }
1566
1610
  }
1567
- let putQuery;
1568
- if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1569
- putQuery = [encode$3({ $since: 0, $until: Infinity })];
1570
- }
1571
- if ($put === true) {
1572
- putQuery = null;
1573
- } else if (Array.isArray($put)) {
1574
- putQuery = $put.map((arg) => encode$3(arg));
1575
- } else if (isDef($put)) {
1576
- putQuery = [encode$3($put)];
1577
- }
1578
- if (isGraph && isDef(putQuery)) {
1579
- node.children = finalize(node.children || [], putQuery, node.version);
1611
+ if (isGraph && (putQuery === null || putQuery.length)) {
1612
+ node.children = finalize(
1613
+ node.children || [],
1614
+ putQuery,
1615
+ node.version,
1616
+ true
1617
+ );
1580
1618
  }
1581
1619
  if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
1582
1620
  return node;
@@ -1591,7 +1629,7 @@ function encode(value, { version, isGraph } = {}) {
1591
1629
  return result;
1592
1630
  }
1593
1631
  function encodeGraph(obj, version = Date.now()) {
1594
- return encode(obj, { version, isGraph: true });
1632
+ return setVersion(encode(obj, { version, isGraph: true }), version);
1595
1633
  }
1596
1634
  function encodeQuery(obj, version = 0) {
1597
1635
  return encode(obj, { version, isGraph: false });
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) {
@@ -386,8 +386,6 @@ function encode$3(arg) {
386
386
  return node;
387
387
  }
388
388
  function decode$3(node) {
389
- if (typeof node === "string")
390
- throw Error("why?");
391
389
  const { key, end, limit } = node;
392
390
  errIf("no_key", !isDef(key));
393
391
  errIf("limit_without_end", isDef(limit) && !isDef(end));
@@ -1034,14 +1032,16 @@ function getKnown(graph, version = 0) {
1034
1032
  }
1035
1033
  return query;
1036
1034
  }
1037
- function finalize(graph, query, version = Date.now()) {
1035
+ function finalize(graph, query, version = Date.now(), suppressSetVersion = false) {
1038
1036
  let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
1039
1037
  merge(result, graph);
1040
1038
  if (query)
1041
1039
  result = slice(result, query).known || [];
1042
- result = setVersion(result, version);
1040
+ if (!suppressSetVersion)
1041
+ result = setVersion(result, version);
1043
1042
  return result;
1044
1043
  }
1044
+ const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
1045
1045
  function decode$1(nodes = [], { isGraph } = {}) {
1046
1046
  function decodeChildren(nodes2) {
1047
1047
  let result = [];
@@ -1057,6 +1057,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
1057
1057
  result.push(...objects);
1058
1058
  }
1059
1059
  const putRanges = [];
1060
+ const prefixChildPuts = [];
1060
1061
  let lastNode = null;
1061
1062
  function addPutRange({ key, end }) {
1062
1063
  if (lastNode) {
@@ -1082,9 +1083,13 @@ function decode$1(nodes = [], { isGraph } = {}) {
1082
1083
  for (const node of nodes2) {
1083
1084
  if (isGraph && addPutRange(node))
1084
1085
  continue;
1085
- if (isPrefix(node))
1086
- pushResult(...decodePrefixNode(node));
1087
- else if (isGraph && isRange(node))
1086
+ if (isPrefix(node)) {
1087
+ const decodedChildren = decodePrefixNode(node);
1088
+ if (PRE_CHI_PUT in decodedChildren) {
1089
+ prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
1090
+ }
1091
+ pushResult(...decodedChildren);
1092
+ } else if (isGraph && isRange(node))
1088
1093
  pushResult(decodeRangeNode(node));
1089
1094
  else if (isBranch(node))
1090
1095
  pushResult(decodeBranchNode(node));
@@ -1118,15 +1123,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
1118
1123
  Object.defineProperty(result, "$put", { value: true });
1119
1124
  } else {
1120
1125
  Object.defineProperty(result, "$put", {
1121
- value: putRanges.map((rNode) => decode$3(rNode))
1126
+ value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
1122
1127
  });
1123
1128
  }
1129
+ } else if (prefixChildPuts.length) {
1130
+ Object.defineProperty(result, "$put", { value: prefixChildPuts });
1124
1131
  }
1125
1132
  return result;
1126
1133
  }
1127
1134
  function decodePrefixNode(node) {
1128
1135
  let args = decode$3(node);
1129
- if (args === "")
1136
+ if (!args)
1130
1137
  args = {};
1131
1138
  if (typeof args === "string") {
1132
1139
  throw Error("decode.unencoded_prefix: " + args);
@@ -1156,6 +1163,16 @@ function decode$1(nodes = [], { isGraph } = {}) {
1156
1163
  }
1157
1164
  child.$key = { ...args, ...child.$key };
1158
1165
  }
1166
+ if (children.$put === true) {
1167
+ children[PRE_CHI_PUT] = [{ ...args, $all: true }];
1168
+ } else if (Array.isArray(children.$put)) {
1169
+ children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
1170
+ ...args,
1171
+ ...rarg
1172
+ }));
1173
+ } else if (isDef(children.$put)) {
1174
+ children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
1175
+ }
1159
1176
  return children;
1160
1177
  }
1161
1178
  function decodeBranchNode(node) {
@@ -1476,7 +1493,7 @@ function encode(value, { version, isGraph } = {}) {
1476
1493
  }
1477
1494
  }
1478
1495
  const combine = isGraph ? merge : add;
1479
- function makeNode2(object, key, ver) {
1496
+ function makeNode2(object, key, ver, parentPuts = []) {
1480
1497
  var _a2;
1481
1498
  if (!isDef(object))
1482
1499
  return;
@@ -1487,8 +1504,13 @@ function encode(value, { version, isGraph } = {}) {
1487
1504
  ver = $ver;
1488
1505
  if (isPlainObject($key)) {
1489
1506
  const [page, filter] = splitArgs($key);
1507
+ const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual(filter, putFilter)).map(([range]) => range);
1490
1508
  if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1491
- const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1509
+ const node2 = makeNode2(
1510
+ { ...object, $key: filter || {}, $put: foundPuts },
1511
+ key,
1512
+ ver
1513
+ );
1492
1514
  if (!filter)
1493
1515
  node2.key = MIN_KEY;
1494
1516
  node2.prefix = true;
@@ -1500,7 +1522,8 @@ function encode(value, { version, isGraph } = {}) {
1500
1522
  $key: filter || {},
1501
1523
  $chi: [
1502
1524
  { ...object, $key: isDef(page.$cursor) ? page.$cursor : page }
1503
- ]
1525
+ ],
1526
+ ...isGraph ? { $put: foundPuts } : {}
1504
1527
  },
1505
1528
  key,
1506
1529
  ver
@@ -1511,6 +1534,25 @@ function encode(value, { version, isGraph } = {}) {
1511
1534
  return node2;
1512
1535
  }
1513
1536
  }
1537
+ let putQuery = [], prefixPuts = [];
1538
+ if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1539
+ putQuery = [encode$3({ $since: 0, $until: Infinity })];
1540
+ }
1541
+ function classifyPut(put) {
1542
+ const [range, filter] = splitArgs(put);
1543
+ if (filter) {
1544
+ prefixPuts.push([range, filter]);
1545
+ } else {
1546
+ putQuery.push(encode$3(put));
1547
+ }
1548
+ }
1549
+ if ($put === true) {
1550
+ putQuery = null;
1551
+ } else if (Array.isArray($put)) {
1552
+ $put.forEach(classifyPut);
1553
+ } else if (isDef($put)) {
1554
+ classifyPut($put);
1555
+ }
1514
1556
  if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1515
1557
  key = $key;
1516
1558
  const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
@@ -1518,7 +1560,9 @@ function encode(value, { version, isGraph } = {}) {
1518
1560
  if (object === null) {
1519
1561
  node.end = node.key;
1520
1562
  } else if (isDef($key) && isDef(key) && key !== $key) {
1521
- node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1563
+ node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
1564
+ Boolean
1565
+ );
1522
1566
  return node;
1523
1567
  } else if ($ref) {
1524
1568
  pushLink($ref, node.version, props2, $val, $chi);
@@ -1532,12 +1576,12 @@ function encode(value, { version, isGraph } = {}) {
1532
1576
  } else if (typeof object !== "object") {
1533
1577
  node.value = isGraph || typeof object === "number" ? object : 1;
1534
1578
  } else if (isDef($chi)) {
1535
- const children = $chi.map((obj) => makeNode2(obj, void 0, ver)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1579
+ const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1536
1580
  if (children.length) {
1537
1581
  node.children = children;
1538
1582
  }
1539
1583
  } else if (Array.isArray(object)) {
1540
- const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1584
+ const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
1541
1585
  combine(acc, [it]);
1542
1586
  return acc;
1543
1587
  }, []);
@@ -1559,19 +1603,13 @@ function encode(value, { version, isGraph } = {}) {
1559
1603
  node.value = 1;
1560
1604
  }
1561
1605
  }
1562
- let putQuery;
1563
- if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1564
- putQuery = [encode$3({ $since: 0, $until: Infinity })];
1565
- }
1566
- if ($put === true) {
1567
- putQuery = null;
1568
- } else if (Array.isArray($put)) {
1569
- putQuery = $put.map((arg) => encode$3(arg));
1570
- } else if (isDef($put)) {
1571
- putQuery = [encode$3($put)];
1572
- }
1573
- if (isGraph && isDef(putQuery)) {
1574
- node.children = finalize(node.children || [], putQuery, node.version);
1606
+ if (isGraph && (putQuery === null || putQuery.length)) {
1607
+ node.children = finalize(
1608
+ node.children || [],
1609
+ putQuery,
1610
+ node.version,
1611
+ true
1612
+ );
1575
1613
  }
1576
1614
  if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
1577
1615
  return node;
@@ -1586,7 +1624,7 @@ function encode(value, { version, isGraph } = {}) {
1586
1624
  return result;
1587
1625
  }
1588
1626
  function encodeGraph(obj, version = Date.now()) {
1589
- return encode(obj, { version, isGraph: true });
1627
+ return setVersion(encode(obj, { version, isGraph: true }), version);
1590
1628
  }
1591
1629
  function encodeQuery(obj, version = 0) {
1592
1630
  return encode(obj, { version, isGraph: false });
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.8",
5
+ "version": "0.16.0-alpha.9",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -17,7 +17,7 @@
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
19
  "lodash": "^4.17.19",
20
- "merge-async-iterators": "^0.2.1",
21
- "@graffy/stream": "0.16.0-alpha.8"
20
+ "@graffy/stream": "0.16.0-alpha.9",
21
+ "merge-async-iterators": "^0.2.1"
22
22
  }
23
23
  }
@@ -1,4 +1,4 @@
1
- export default function finalize(graph: any, query: any, version?: number): {
1
+ export default function finalize(graph: any, query: any, version?: number, suppressSetVersion?: boolean): {
2
2
  key: Uint8Array;
3
3
  end: Uint8Array;
4
4
  version: number;