Skip to content

Commit a6ddc4e

Browse files
committed
feat(mutate): accept updates: AwaitIterable<Update | Update[]>
1 parent c6fafce commit a6ddc4e

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/mutate.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { firstElement } from "@tabcat/ith-element";
1+
import { firstElement, lastElement } from "@tabcat/ith-element";
22
import { union } from "@tabcat/ordered-sets/union";
33
import { pairwiseTraversal } from "@tabcat/ordered-sets/util";
44
import { Blockstore } from "interface-blockstore";
@@ -74,21 +74,33 @@ const handleUpdate = (
7474
}
7575
};
7676

77-
async function takeOne<T>(it: AwaitIterable<T>): Promise<T | void> {
78-
for await (const v of it) return v;
77+
async function takeOneUpdate(
78+
updates: AwaitIterable<Update | Update[]>,
79+
): Promise<Update | void> {
80+
for await (const u of updates) return Array.isArray(u) ? u[0] : u;
7981
}
8082

8183
async function populateUpdts(
82-
updates: AwaitIterable<Update>,
84+
updates: AwaitIterable<Update | Update[]>,
8385
updts: Update[],
8486
updatee: Bucket,
8587
isHead: boolean,
8688
): Promise<void> {
87-
for await (const update of updates) {
88-
updts.push(update);
89-
const boundary = updatee.getBoundary();
89+
for await (let _updates of updates) {
90+
if (!Array.isArray(_updates)) {
91+
_updates = [_updates];
92+
}
93+
94+
for (const u of _updates) {
95+
updts.push(u);
96+
}
9097

91-
if (boundary != null && !isHead && compareTuples(update, boundary) >= 0) {
98+
const boundary = updatee.getBoundary();
99+
if (
100+
boundary != null &&
101+
!isHead &&
102+
compareTuples(lastElement(updts), boundary) >= 0
103+
) {
92104
break;
93105
}
94106
}
@@ -106,13 +118,14 @@ async function populateUpdts(
106118
export async function* mutate(
107119
blockstore: Blockstore,
108120
tree: ProllyTree,
109-
updates: AwaitIterable<Update>,
121+
updates: AwaitIterable<Update | Update[]>,
110122
): AsyncGenerator<ProllyTreeDiff> {
111123
// whole function should be rewritten around updates async iterator, too complicated right now
112124
if (Array.isArray(updates)) {
113125
updates = updates[Symbol.iterator]();
114126
}
115-
const firstUpdate = await takeOne(updates);
127+
128+
const firstUpdate = await takeOneUpdate(updates);
116129

117130
if (firstUpdate == null) {
118131
return tree;
@@ -270,7 +283,7 @@ export async function* mutate(
270283
break;
271284
}
272285

273-
let nextUpdt = updts[0] ?? (await takeOne(updates));
286+
let nextUpdt = updts[0] ?? (await takeOneUpdate(updates));
274287
let nextLevel = level;
275288

276289
if (nextUpdt == null) {

test/mutate.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { pairwiseTraversal } from "@tabcat/ordered-sets/util";
33
import { describe, expect, it } from "vitest";
44
import { compareBuckets, compareBytes, compareTuples } from "../src/compare.js";
55
import { BucketDiff, NodeDiff } from "../src/diff.js";
6-
import { cloneTree } from "../src/index.js";
6+
import { cloneTree, createEmptyTree } from "../src/index.js";
77
import { Node, ProllyTree } from "../src/interface.js";
88
import { Update, mutate } from "../src/mutate.js";
99
import { nodeToTuple } from "../src/utils.js";
@@ -162,4 +162,21 @@ describe("mutate", () => {
162162
});
163163
}
164164
}
165+
166+
it("accepts updates with type AwaitIterable<Update | Update[]>", async () => {
167+
const tree1 = createEmptyTree();
168+
const tree2 = createEmptyTree();
169+
170+
const updates: Update[] = [
171+
{ timestamp: 1, hash: new Uint8Array(32), message: new Uint8Array() },
172+
];
173+
174+
for await (const _ of mutate(blockstore, tree1, updates)) {
175+
}
176+
177+
for await (const _ of mutate(blockstore, tree2, [updates])) {
178+
}
179+
180+
expect(tree1).to.deep.equal(tree2);
181+
});
165182
});

0 commit comments

Comments
 (0)