@graffy/common 0.16.0-alpha.8 → 0.16.0
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 +113 -58
- package/index.mjs +112 -57
- package/package.json +2 -2
- package/types/ops/finalize.d.ts +8 -1
- package/types/ops/setVersion.d.ts +1 -1
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) {
|
|
@@ -14,13 +14,19 @@ function encode$6(string) {
|
|
|
14
14
|
function decode$6(u8Arr) {
|
|
15
15
|
return textDecoder.decode(u8Arr);
|
|
16
16
|
}
|
|
17
|
+
function TwosComplement(view) {
|
|
18
|
+
let lo = -view.getUint32(4) >>> 0;
|
|
19
|
+
const carry = lo ? 0 : -1;
|
|
20
|
+
let hi = ~view.getUint32(0) + carry >>> 0;
|
|
21
|
+
view.setUint32(0, hi);
|
|
22
|
+
view.setUint32(4, lo);
|
|
23
|
+
}
|
|
17
24
|
function encode$5(number) {
|
|
18
25
|
const buffer = new ArrayBuffer(8);
|
|
19
26
|
const view = new DataView(buffer);
|
|
20
27
|
view.setFloat64(0, number);
|
|
21
28
|
if (number < 0) {
|
|
22
|
-
|
|
23
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
29
|
+
TwosComplement(view);
|
|
24
30
|
} else {
|
|
25
31
|
view.setUint8(0, view.getUint8(0) | 128);
|
|
26
32
|
}
|
|
@@ -35,8 +41,7 @@ function decode$5(u8Arr) {
|
|
|
35
41
|
if (high & 128) {
|
|
36
42
|
view.setUint8(0, high & 127);
|
|
37
43
|
} else {
|
|
38
|
-
|
|
39
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
44
|
+
TwosComplement(view);
|
|
40
45
|
}
|
|
41
46
|
return view.getFloat64(0);
|
|
42
47
|
}
|
|
@@ -118,6 +123,9 @@ const stringifyDescriptor = {
|
|
|
118
123
|
}
|
|
119
124
|
};
|
|
120
125
|
function addStringify(buffer) {
|
|
126
|
+
if ("toJSON" in buffer || "toString" in buffer || Symbol.for("nodejs.util.inspect.custom") in buffer) {
|
|
127
|
+
return buffer;
|
|
128
|
+
}
|
|
121
129
|
Object.defineProperties(buffer, {
|
|
122
130
|
toJSON: stringifyDescriptor,
|
|
123
131
|
toString: stringifyDescriptor,
|
|
@@ -282,10 +290,12 @@ function keyStep(key) {
|
|
|
282
290
|
switch (key[l]) {
|
|
283
291
|
case 0:
|
|
284
292
|
newKey = key.slice(0, l);
|
|
293
|
+
addStringify(newKey);
|
|
285
294
|
step = 1;
|
|
286
295
|
break;
|
|
287
296
|
case 255:
|
|
288
297
|
newKey = key.slice(0, l);
|
|
298
|
+
addStringify(newKey);
|
|
289
299
|
newKey[l - 1]++;
|
|
290
300
|
step = -1;
|
|
291
301
|
break;
|
|
@@ -293,7 +303,7 @@ function keyStep(key) {
|
|
|
293
303
|
newKey = key;
|
|
294
304
|
step = 0;
|
|
295
305
|
}
|
|
296
|
-
return { key:
|
|
306
|
+
return { key: newKey, step };
|
|
297
307
|
}
|
|
298
308
|
function keyBefore(key) {
|
|
299
309
|
if (isMinKey(key) || isMaxKey(key))
|
|
@@ -391,8 +401,6 @@ function encode$3(arg) {
|
|
|
391
401
|
return node;
|
|
392
402
|
}
|
|
393
403
|
function decode$3(node) {
|
|
394
|
-
if (typeof node === "string")
|
|
395
|
-
throw Error("why?");
|
|
396
404
|
const { key, end, limit } = node;
|
|
397
405
|
errIf("no_key", !isDef(key));
|
|
398
406
|
errIf("limit_without_end", isDef(limit) && !isDef(end));
|
|
@@ -1013,11 +1021,12 @@ function getNewerChange(node, base) {
|
|
|
1013
1021
|
return node.version >= base.version ? node : null;
|
|
1014
1022
|
}
|
|
1015
1023
|
}
|
|
1016
|
-
function setVersion(graph, version) {
|
|
1024
|
+
function setVersion(graph, version, onlyIfZero = false) {
|
|
1017
1025
|
for (const node of graph) {
|
|
1018
|
-
node.version
|
|
1026
|
+
if (!onlyIfZero || !node.version)
|
|
1027
|
+
node.version = version;
|
|
1019
1028
|
if (node.children)
|
|
1020
|
-
setVersion(node.children, version);
|
|
1029
|
+
setVersion(node.children, version, onlyIfZero);
|
|
1021
1030
|
}
|
|
1022
1031
|
return graph;
|
|
1023
1032
|
}
|
|
@@ -1044,9 +1053,11 @@ function finalize(graph, query, version = Date.now()) {
|
|
|
1044
1053
|
merge(result, graph);
|
|
1045
1054
|
if (query)
|
|
1046
1055
|
result = slice(result, query).known || [];
|
|
1047
|
-
|
|
1056
|
+
if (!version !== false)
|
|
1057
|
+
result = setVersion(result, version, true);
|
|
1048
1058
|
return result;
|
|
1049
1059
|
}
|
|
1060
|
+
const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
|
|
1050
1061
|
function decode$1(nodes = [], { isGraph } = {}) {
|
|
1051
1062
|
function decodeChildren(nodes2) {
|
|
1052
1063
|
let result = [];
|
|
@@ -1062,6 +1073,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1062
1073
|
result.push(...objects);
|
|
1063
1074
|
}
|
|
1064
1075
|
const putRanges = [];
|
|
1076
|
+
const prefixChildPuts = [];
|
|
1065
1077
|
let lastNode = null;
|
|
1066
1078
|
function addPutRange({ key, end }) {
|
|
1067
1079
|
if (lastNode) {
|
|
@@ -1087,9 +1099,13 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1087
1099
|
for (const node of nodes2) {
|
|
1088
1100
|
if (isGraph && addPutRange(node))
|
|
1089
1101
|
continue;
|
|
1090
|
-
if (isPrefix(node))
|
|
1091
|
-
|
|
1092
|
-
|
|
1102
|
+
if (isPrefix(node)) {
|
|
1103
|
+
const decodedChildren = decodePrefixNode(node);
|
|
1104
|
+
if (PRE_CHI_PUT in decodedChildren) {
|
|
1105
|
+
prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
|
|
1106
|
+
}
|
|
1107
|
+
pushResult(...decodedChildren);
|
|
1108
|
+
} else if (isGraph && isRange(node))
|
|
1093
1109
|
pushResult(decodeRangeNode(node));
|
|
1094
1110
|
else if (isBranch(node))
|
|
1095
1111
|
pushResult(decodeBranchNode(node));
|
|
@@ -1123,15 +1139,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1123
1139
|
Object.defineProperty(result, "$put", { value: true });
|
|
1124
1140
|
} else {
|
|
1125
1141
|
Object.defineProperty(result, "$put", {
|
|
1126
|
-
value: putRanges.map((rNode) => decode$3(rNode))
|
|
1142
|
+
value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
|
|
1127
1143
|
});
|
|
1128
1144
|
}
|
|
1145
|
+
} else if (prefixChildPuts.length) {
|
|
1146
|
+
Object.defineProperty(result, "$put", { value: prefixChildPuts });
|
|
1129
1147
|
}
|
|
1130
1148
|
return result;
|
|
1131
1149
|
}
|
|
1132
1150
|
function decodePrefixNode(node) {
|
|
1133
1151
|
let args = decode$3(node);
|
|
1134
|
-
if (args
|
|
1152
|
+
if (!args)
|
|
1135
1153
|
args = {};
|
|
1136
1154
|
if (typeof args === "string") {
|
|
1137
1155
|
throw Error("decode.unencoded_prefix: " + args);
|
|
@@ -1161,6 +1179,16 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1161
1179
|
}
|
|
1162
1180
|
child.$key = { ...args, ...child.$key };
|
|
1163
1181
|
}
|
|
1182
|
+
if (children.$put === true) {
|
|
1183
|
+
children[PRE_CHI_PUT] = [{ ...args, $all: true }];
|
|
1184
|
+
} else if (Array.isArray(children.$put)) {
|
|
1185
|
+
children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
|
|
1186
|
+
...args,
|
|
1187
|
+
...rarg
|
|
1188
|
+
}));
|
|
1189
|
+
} else if (isDef(children.$put)) {
|
|
1190
|
+
children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
|
|
1191
|
+
}
|
|
1164
1192
|
return children;
|
|
1165
1193
|
}
|
|
1166
1194
|
function decodeBranchNode(node) {
|
|
@@ -1481,41 +1509,75 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1481
1509
|
}
|
|
1482
1510
|
}
|
|
1483
1511
|
const combine = isGraph ? merge : add;
|
|
1484
|
-
function makeNode2(object, key, ver) {
|
|
1512
|
+
function makeNode2(object, key, ver, parentPuts = []) {
|
|
1485
1513
|
var _a2;
|
|
1486
1514
|
if (!isDef(object))
|
|
1487
1515
|
return;
|
|
1516
|
+
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1517
|
+
if (typeof object === "object" && object && !Array.isArray(object)) {
|
|
1518
|
+
object = {
|
|
1519
|
+
...Object.fromEntries(
|
|
1520
|
+
Object.entries({
|
|
1521
|
+
$key,
|
|
1522
|
+
$ver,
|
|
1523
|
+
$ref,
|
|
1524
|
+
$val,
|
|
1525
|
+
$chi,
|
|
1526
|
+
$put
|
|
1527
|
+
}).filter(([_, val]) => isDef(val))
|
|
1528
|
+
),
|
|
1529
|
+
...props2
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1488
1532
|
if (typeof object === "object" && object && isEmpty(object))
|
|
1489
1533
|
return;
|
|
1490
|
-
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1491
1534
|
if (isDef($ver))
|
|
1492
1535
|
ver = $ver;
|
|
1493
1536
|
if (isPlainObject($key)) {
|
|
1494
1537
|
const [page, filter] = splitArgs($key);
|
|
1495
|
-
if (
|
|
1496
|
-
const
|
|
1497
|
-
if (!
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1538
|
+
if (page) {
|
|
1539
|
+
const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual__default.default(filter, putFilter)).map(([range]) => range);
|
|
1540
|
+
if (isGraph && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
|
|
1541
|
+
object.$key = filter || {};
|
|
1542
|
+
object.$put = foundPuts;
|
|
1543
|
+
const node2 = makeNode2(object, key, ver);
|
|
1544
|
+
if (!filter)
|
|
1545
|
+
node2.key = MIN_KEY;
|
|
1546
|
+
node2.prefix = true;
|
|
1547
|
+
return node2;
|
|
1548
|
+
}
|
|
1549
|
+
if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
|
|
1550
|
+
object.$key = isDef(page.$cursor) ? page.$cursor : page;
|
|
1551
|
+
const wrapper = { $key: filter || {}, $chi: [object] };
|
|
1552
|
+
if (isGraph)
|
|
1553
|
+
wrapper.$put = foundPuts;
|
|
1554
|
+
const node2 = makeNode2(wrapper, key, ver);
|
|
1555
|
+
if (!filter)
|
|
1556
|
+
node2.key = MIN_KEY;
|
|
1557
|
+
node2.prefix = true;
|
|
1558
|
+
return node2;
|
|
1559
|
+
}
|
|
1501
1560
|
}
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
);
|
|
1513
|
-
if (!filter)
|
|
1514
|
-
node2.key = MIN_KEY;
|
|
1515
|
-
node2.prefix = true;
|
|
1516
|
-
return node2;
|
|
1561
|
+
}
|
|
1562
|
+
let putQuery = [], prefixPuts = [];
|
|
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
|
+
function classifyPut(put) {
|
|
1567
|
+
const [range, filter] = splitArgs(put);
|
|
1568
|
+
if (filter) {
|
|
1569
|
+
prefixPuts.push([range, filter]);
|
|
1570
|
+
} else {
|
|
1571
|
+
putQuery.push(encode$3(put));
|
|
1517
1572
|
}
|
|
1518
1573
|
}
|
|
1574
|
+
if ($put === true) {
|
|
1575
|
+
putQuery = null;
|
|
1576
|
+
} else if (Array.isArray($put)) {
|
|
1577
|
+
$put.forEach(classifyPut);
|
|
1578
|
+
} else if (isDef($put)) {
|
|
1579
|
+
classifyPut($put);
|
|
1580
|
+
}
|
|
1519
1581
|
if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
|
|
1520
1582
|
key = $key;
|
|
1521
1583
|
const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
|
|
@@ -1523,7 +1585,9 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1523
1585
|
if (object === null) {
|
|
1524
1586
|
node.end = node.key;
|
|
1525
1587
|
} else if (isDef($key) && isDef(key) && key !== $key) {
|
|
1526
|
-
node.children = [makeNode2(object, void 0, ver)].filter(
|
|
1588
|
+
node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
|
|
1589
|
+
Boolean
|
|
1590
|
+
);
|
|
1527
1591
|
return node;
|
|
1528
1592
|
} else if ($ref) {
|
|
1529
1593
|
pushLink($ref, node.version, props2, $val, $chi);
|
|
@@ -1537,12 +1601,12 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1537
1601
|
} else if (typeof object !== "object") {
|
|
1538
1602
|
node.value = isGraph || typeof object === "number" ? object : 1;
|
|
1539
1603
|
} 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));
|
|
1604
|
+
const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
|
|
1541
1605
|
if (children.length) {
|
|
1542
1606
|
node.children = children;
|
|
1543
1607
|
}
|
|
1544
1608
|
} else if (Array.isArray(object)) {
|
|
1545
|
-
const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
|
|
1609
|
+
const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
|
|
1546
1610
|
combine(acc, [it]);
|
|
1547
1611
|
return acc;
|
|
1548
1612
|
}, []);
|
|
@@ -1564,19 +1628,8 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1564
1628
|
node.value = 1;
|
|
1565
1629
|
}
|
|
1566
1630
|
}
|
|
1567
|
-
|
|
1568
|
-
|
|
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);
|
|
1631
|
+
if (isGraph && (putQuery === null || putQuery.length)) {
|
|
1632
|
+
node.children = finalize(node.children || [], putQuery, false);
|
|
1580
1633
|
}
|
|
1581
1634
|
if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
|
|
1582
1635
|
return node;
|
|
@@ -1591,7 +1644,9 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1591
1644
|
return result;
|
|
1592
1645
|
}
|
|
1593
1646
|
function encodeGraph(obj, version = Date.now()) {
|
|
1594
|
-
|
|
1647
|
+
const encoded = encode(obj, { version, isGraph: true });
|
|
1648
|
+
const versioned = setVersion(encoded, version, true);
|
|
1649
|
+
return versioned;
|
|
1595
1650
|
}
|
|
1596
1651
|
function encodeQuery(obj, version = 0) {
|
|
1597
1652
|
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) {
|
|
@@ -9,13 +9,19 @@ function encode$6(string) {
|
|
|
9
9
|
function decode$6(u8Arr) {
|
|
10
10
|
return textDecoder.decode(u8Arr);
|
|
11
11
|
}
|
|
12
|
+
function TwosComplement(view) {
|
|
13
|
+
let lo = -view.getUint32(4) >>> 0;
|
|
14
|
+
const carry = lo ? 0 : -1;
|
|
15
|
+
let hi = ~view.getUint32(0) + carry >>> 0;
|
|
16
|
+
view.setUint32(0, hi);
|
|
17
|
+
view.setUint32(4, lo);
|
|
18
|
+
}
|
|
12
19
|
function encode$5(number) {
|
|
13
20
|
const buffer = new ArrayBuffer(8);
|
|
14
21
|
const view = new DataView(buffer);
|
|
15
22
|
view.setFloat64(0, number);
|
|
16
23
|
if (number < 0) {
|
|
17
|
-
|
|
18
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
24
|
+
TwosComplement(view);
|
|
19
25
|
} else {
|
|
20
26
|
view.setUint8(0, view.getUint8(0) | 128);
|
|
21
27
|
}
|
|
@@ -30,8 +36,7 @@ function decode$5(u8Arr) {
|
|
|
30
36
|
if (high & 128) {
|
|
31
37
|
view.setUint8(0, high & 127);
|
|
32
38
|
} else {
|
|
33
|
-
|
|
34
|
-
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
39
|
+
TwosComplement(view);
|
|
35
40
|
}
|
|
36
41
|
return view.getFloat64(0);
|
|
37
42
|
}
|
|
@@ -113,6 +118,9 @@ const stringifyDescriptor = {
|
|
|
113
118
|
}
|
|
114
119
|
};
|
|
115
120
|
function addStringify(buffer) {
|
|
121
|
+
if ("toJSON" in buffer || "toString" in buffer || Symbol.for("nodejs.util.inspect.custom") in buffer) {
|
|
122
|
+
return buffer;
|
|
123
|
+
}
|
|
116
124
|
Object.defineProperties(buffer, {
|
|
117
125
|
toJSON: stringifyDescriptor,
|
|
118
126
|
toString: stringifyDescriptor,
|
|
@@ -277,10 +285,12 @@ function keyStep(key) {
|
|
|
277
285
|
switch (key[l]) {
|
|
278
286
|
case 0:
|
|
279
287
|
newKey = key.slice(0, l);
|
|
288
|
+
addStringify(newKey);
|
|
280
289
|
step = 1;
|
|
281
290
|
break;
|
|
282
291
|
case 255:
|
|
283
292
|
newKey = key.slice(0, l);
|
|
293
|
+
addStringify(newKey);
|
|
284
294
|
newKey[l - 1]++;
|
|
285
295
|
step = -1;
|
|
286
296
|
break;
|
|
@@ -288,7 +298,7 @@ function keyStep(key) {
|
|
|
288
298
|
newKey = key;
|
|
289
299
|
step = 0;
|
|
290
300
|
}
|
|
291
|
-
return { key:
|
|
301
|
+
return { key: newKey, step };
|
|
292
302
|
}
|
|
293
303
|
function keyBefore(key) {
|
|
294
304
|
if (isMinKey(key) || isMaxKey(key))
|
|
@@ -386,8 +396,6 @@ function encode$3(arg) {
|
|
|
386
396
|
return node;
|
|
387
397
|
}
|
|
388
398
|
function decode$3(node) {
|
|
389
|
-
if (typeof node === "string")
|
|
390
|
-
throw Error("why?");
|
|
391
399
|
const { key, end, limit } = node;
|
|
392
400
|
errIf("no_key", !isDef(key));
|
|
393
401
|
errIf("limit_without_end", isDef(limit) && !isDef(end));
|
|
@@ -1008,11 +1016,12 @@ function getNewerChange(node, base) {
|
|
|
1008
1016
|
return node.version >= base.version ? node : null;
|
|
1009
1017
|
}
|
|
1010
1018
|
}
|
|
1011
|
-
function setVersion(graph, version) {
|
|
1019
|
+
function setVersion(graph, version, onlyIfZero = false) {
|
|
1012
1020
|
for (const node of graph) {
|
|
1013
|
-
node.version
|
|
1021
|
+
if (!onlyIfZero || !node.version)
|
|
1022
|
+
node.version = version;
|
|
1014
1023
|
if (node.children)
|
|
1015
|
-
setVersion(node.children, version);
|
|
1024
|
+
setVersion(node.children, version, onlyIfZero);
|
|
1016
1025
|
}
|
|
1017
1026
|
return graph;
|
|
1018
1027
|
}
|
|
@@ -1039,9 +1048,11 @@ function finalize(graph, query, version = Date.now()) {
|
|
|
1039
1048
|
merge(result, graph);
|
|
1040
1049
|
if (query)
|
|
1041
1050
|
result = slice(result, query).known || [];
|
|
1042
|
-
|
|
1051
|
+
if (!version !== false)
|
|
1052
|
+
result = setVersion(result, version, true);
|
|
1043
1053
|
return result;
|
|
1044
1054
|
}
|
|
1055
|
+
const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
|
|
1045
1056
|
function decode$1(nodes = [], { isGraph } = {}) {
|
|
1046
1057
|
function decodeChildren(nodes2) {
|
|
1047
1058
|
let result = [];
|
|
@@ -1057,6 +1068,7 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1057
1068
|
result.push(...objects);
|
|
1058
1069
|
}
|
|
1059
1070
|
const putRanges = [];
|
|
1071
|
+
const prefixChildPuts = [];
|
|
1060
1072
|
let lastNode = null;
|
|
1061
1073
|
function addPutRange({ key, end }) {
|
|
1062
1074
|
if (lastNode) {
|
|
@@ -1082,9 +1094,13 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1082
1094
|
for (const node of nodes2) {
|
|
1083
1095
|
if (isGraph && addPutRange(node))
|
|
1084
1096
|
continue;
|
|
1085
|
-
if (isPrefix(node))
|
|
1086
|
-
|
|
1087
|
-
|
|
1097
|
+
if (isPrefix(node)) {
|
|
1098
|
+
const decodedChildren = decodePrefixNode(node);
|
|
1099
|
+
if (PRE_CHI_PUT in decodedChildren) {
|
|
1100
|
+
prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
|
|
1101
|
+
}
|
|
1102
|
+
pushResult(...decodedChildren);
|
|
1103
|
+
} else if (isGraph && isRange(node))
|
|
1088
1104
|
pushResult(decodeRangeNode(node));
|
|
1089
1105
|
else if (isBranch(node))
|
|
1090
1106
|
pushResult(decodeBranchNode(node));
|
|
@@ -1118,15 +1134,17 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1118
1134
|
Object.defineProperty(result, "$put", { value: true });
|
|
1119
1135
|
} else {
|
|
1120
1136
|
Object.defineProperty(result, "$put", {
|
|
1121
|
-
value: putRanges.map((rNode) => decode$3(rNode))
|
|
1137
|
+
value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
|
|
1122
1138
|
});
|
|
1123
1139
|
}
|
|
1140
|
+
} else if (prefixChildPuts.length) {
|
|
1141
|
+
Object.defineProperty(result, "$put", { value: prefixChildPuts });
|
|
1124
1142
|
}
|
|
1125
1143
|
return result;
|
|
1126
1144
|
}
|
|
1127
1145
|
function decodePrefixNode(node) {
|
|
1128
1146
|
let args = decode$3(node);
|
|
1129
|
-
if (args
|
|
1147
|
+
if (!args)
|
|
1130
1148
|
args = {};
|
|
1131
1149
|
if (typeof args === "string") {
|
|
1132
1150
|
throw Error("decode.unencoded_prefix: " + args);
|
|
@@ -1156,6 +1174,16 @@ function decode$1(nodes = [], { isGraph } = {}) {
|
|
|
1156
1174
|
}
|
|
1157
1175
|
child.$key = { ...args, ...child.$key };
|
|
1158
1176
|
}
|
|
1177
|
+
if (children.$put === true) {
|
|
1178
|
+
children[PRE_CHI_PUT] = [{ ...args, $all: true }];
|
|
1179
|
+
} else if (Array.isArray(children.$put)) {
|
|
1180
|
+
children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
|
|
1181
|
+
...args,
|
|
1182
|
+
...rarg
|
|
1183
|
+
}));
|
|
1184
|
+
} else if (isDef(children.$put)) {
|
|
1185
|
+
children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
|
|
1186
|
+
}
|
|
1159
1187
|
return children;
|
|
1160
1188
|
}
|
|
1161
1189
|
function decodeBranchNode(node) {
|
|
@@ -1476,41 +1504,75 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1476
1504
|
}
|
|
1477
1505
|
}
|
|
1478
1506
|
const combine = isGraph ? merge : add;
|
|
1479
|
-
function makeNode2(object, key, ver) {
|
|
1507
|
+
function makeNode2(object, key, ver, parentPuts = []) {
|
|
1480
1508
|
var _a2;
|
|
1481
1509
|
if (!isDef(object))
|
|
1482
1510
|
return;
|
|
1511
|
+
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1512
|
+
if (typeof object === "object" && object && !Array.isArray(object)) {
|
|
1513
|
+
object = {
|
|
1514
|
+
...Object.fromEntries(
|
|
1515
|
+
Object.entries({
|
|
1516
|
+
$key,
|
|
1517
|
+
$ver,
|
|
1518
|
+
$ref,
|
|
1519
|
+
$val,
|
|
1520
|
+
$chi,
|
|
1521
|
+
$put
|
|
1522
|
+
}).filter(([_, val]) => isDef(val))
|
|
1523
|
+
),
|
|
1524
|
+
...props2
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1483
1527
|
if (typeof object === "object" && object && isEmpty(object))
|
|
1484
1528
|
return;
|
|
1485
|
-
const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
|
|
1486
1529
|
if (isDef($ver))
|
|
1487
1530
|
ver = $ver;
|
|
1488
1531
|
if (isPlainObject($key)) {
|
|
1489
1532
|
const [page, filter] = splitArgs($key);
|
|
1490
|
-
if (
|
|
1491
|
-
const
|
|
1492
|
-
if (!
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1533
|
+
if (page) {
|
|
1534
|
+
const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual(filter, putFilter)).map(([range]) => range);
|
|
1535
|
+
if (isGraph && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
|
|
1536
|
+
object.$key = filter || {};
|
|
1537
|
+
object.$put = foundPuts;
|
|
1538
|
+
const node2 = makeNode2(object, key, ver);
|
|
1539
|
+
if (!filter)
|
|
1540
|
+
node2.key = MIN_KEY;
|
|
1541
|
+
node2.prefix = true;
|
|
1542
|
+
return node2;
|
|
1543
|
+
}
|
|
1544
|
+
if ((!isDef(key) || Number.isInteger(key)) && (filter || isDef(page.$cursor))) {
|
|
1545
|
+
object.$key = isDef(page.$cursor) ? page.$cursor : page;
|
|
1546
|
+
const wrapper = { $key: filter || {}, $chi: [object] };
|
|
1547
|
+
if (isGraph)
|
|
1548
|
+
wrapper.$put = foundPuts;
|
|
1549
|
+
const node2 = makeNode2(wrapper, key, ver);
|
|
1550
|
+
if (!filter)
|
|
1551
|
+
node2.key = MIN_KEY;
|
|
1552
|
+
node2.prefix = true;
|
|
1553
|
+
return node2;
|
|
1554
|
+
}
|
|
1496
1555
|
}
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
);
|
|
1508
|
-
if (!filter)
|
|
1509
|
-
node2.key = MIN_KEY;
|
|
1510
|
-
node2.prefix = true;
|
|
1511
|
-
return node2;
|
|
1556
|
+
}
|
|
1557
|
+
let putQuery = [], prefixPuts = [];
|
|
1558
|
+
if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
|
|
1559
|
+
putQuery = [encode$3({ $since: 0, $until: Infinity })];
|
|
1560
|
+
}
|
|
1561
|
+
function classifyPut(put) {
|
|
1562
|
+
const [range, filter] = splitArgs(put);
|
|
1563
|
+
if (filter) {
|
|
1564
|
+
prefixPuts.push([range, filter]);
|
|
1565
|
+
} else {
|
|
1566
|
+
putQuery.push(encode$3(put));
|
|
1512
1567
|
}
|
|
1513
1568
|
}
|
|
1569
|
+
if ($put === true) {
|
|
1570
|
+
putQuery = null;
|
|
1571
|
+
} else if (Array.isArray($put)) {
|
|
1572
|
+
$put.forEach(classifyPut);
|
|
1573
|
+
} else if (isDef($put)) {
|
|
1574
|
+
classifyPut($put);
|
|
1575
|
+
}
|
|
1514
1576
|
if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
|
|
1515
1577
|
key = $key;
|
|
1516
1578
|
const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
|
|
@@ -1518,7 +1580,9 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1518
1580
|
if (object === null) {
|
|
1519
1581
|
node.end = node.key;
|
|
1520
1582
|
} else if (isDef($key) && isDef(key) && key !== $key) {
|
|
1521
|
-
node.children = [makeNode2(object, void 0, ver)].filter(
|
|
1583
|
+
node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
|
|
1584
|
+
Boolean
|
|
1585
|
+
);
|
|
1522
1586
|
return node;
|
|
1523
1587
|
} else if ($ref) {
|
|
1524
1588
|
pushLink($ref, node.version, props2, $val, $chi);
|
|
@@ -1532,12 +1596,12 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1532
1596
|
} else if (typeof object !== "object") {
|
|
1533
1597
|
node.value = isGraph || typeof object === "number" ? object : 1;
|
|
1534
1598
|
} 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));
|
|
1599
|
+
const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
|
|
1536
1600
|
if (children.length) {
|
|
1537
1601
|
node.children = children;
|
|
1538
1602
|
}
|
|
1539
1603
|
} else if (Array.isArray(object)) {
|
|
1540
|
-
const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
|
|
1604
|
+
const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
|
|
1541
1605
|
combine(acc, [it]);
|
|
1542
1606
|
return acc;
|
|
1543
1607
|
}, []);
|
|
@@ -1559,19 +1623,8 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1559
1623
|
node.value = 1;
|
|
1560
1624
|
}
|
|
1561
1625
|
}
|
|
1562
|
-
|
|
1563
|
-
|
|
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);
|
|
1626
|
+
if (isGraph && (putQuery === null || putQuery.length)) {
|
|
1627
|
+
node.children = finalize(node.children || [], putQuery, false);
|
|
1575
1628
|
}
|
|
1576
1629
|
if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
|
|
1577
1630
|
return node;
|
|
@@ -1586,7 +1639,9 @@ function encode(value, { version, isGraph } = {}) {
|
|
|
1586
1639
|
return result;
|
|
1587
1640
|
}
|
|
1588
1641
|
function encodeGraph(obj, version = Date.now()) {
|
|
1589
|
-
|
|
1642
|
+
const encoded = encode(obj, { version, isGraph: true });
|
|
1643
|
+
const versioned = setVersion(encoded, version, true);
|
|
1644
|
+
return versioned;
|
|
1590
1645
|
}
|
|
1591
1646
|
function encodeQuery(obj, version = 0) {
|
|
1592
1647
|
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
|
|
5
|
+
"version": "0.16.0",
|
|
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
|
|
21
|
+
"@graffy/stream": "0.16.0"
|
|
22
22
|
}
|
|
23
23
|
}
|
package/types/ops/finalize.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
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;
|