Skip to content

Commit 9cf111f

Browse files
committed
fix: fix async onnextnode
1 parent 4bbc8c9 commit 9cf111f

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aralroca/diff-dom-streaming",
3-
"version": "0.6.5",
3+
"version": "0.6.6",
44
"exports": {
55
".": "./src/index.ts",
66
"./types": "./index.d.ts"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff-dom-streaming",
3-
"version": "0.6.5",
3+
"version": "0.6.6",
44
"bugs": "https://github.com/aralroca/diff-dom-streaming/issues",
55
"description": "Diff DOM algorithm with streaming. Gets all modifications, insertions and removals between a DOM fragment and a stream HTML reader.",
66
"keywords": [

src/index.test.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,40 @@ describe("Diff test", () => {
16591659
);
16601660
});
16611661

1662+
it("should onNextNode execute in a sequential way when is async", async () => {
1663+
const results = await testDiff({
1664+
oldHTMLString: `
1665+
<div>foo</div>
1666+
`,
1667+
newHTMLStringChunks: ["<div scan>first</div>", "<div scan>second</div>", "<div scan>third</div>"],
1668+
registerWC: true,
1669+
onNextNode: `async (n) => {
1670+
if (!n?.hasAttribute?.('scan')) return
1671+
window.index ??= 1;
1672+
window.logs ??= '';
1673+
await new Promise((r) => setTimeout(() => {
1674+
window.logs += n.innerText + ' ';
1675+
r(true);
1676+
}, ++window.index * 50));
1677+
}`
1678+
});
1679+
1680+
expect(results[0]).toBe(
1681+
normalize(`
1682+
<html>
1683+
<head></head>
1684+
<body>
1685+
<div scan="">first</div>
1686+
<div scan="">second</div>
1687+
<div scan="">third</div>
1688+
</body>
1689+
</html>
1690+
`),
1691+
);
1692+
1693+
expect(results.at(-1)).toBe('first second third ')
1694+
});
1695+
16621696
it("should add WC that modifies the DOM on connect it (old with key)", async () => {
16631697
const [newHTML] = await testDiff({
16641698
oldHTMLString: `
@@ -1689,6 +1723,7 @@ describe("Diff test", () => {
16891723
transition = false,
16901724
ignoreId = false,
16911725
registerWC = false,
1726+
onNextNode,
16921727
}: {
16931728
oldHTMLString: string;
16941729
newHTMLStringChunks: string[];
@@ -1697,9 +1732,10 @@ describe("Diff test", () => {
16971732
transition?: boolean;
16981733
ignoreId?: boolean;
16991734
registerWC?: boolean;
1700-
}): Promise<[string, any[], Node[], boolean]> {
1735+
onNextNode?: string
1736+
}): Promise<[string, any[], Node[], boolean, string]> {
17011737
await page.setContent(normalize(oldHTMLString));
1702-
const [mutations, streamNodes, transitionApplied] = await page.evaluate(
1738+
const [mutations, streamNodes, transitionApplied, logs] = await page.evaluate(
17031739
async ([
17041740
diffCode,
17051741
newHTMLStringChunks,
@@ -1708,6 +1744,7 @@ describe("Diff test", () => {
17081744
transition,
17091745
ignoreId,
17101746
registerWC,
1747+
onNextNode,
17111748
]) => {
17121749
eval(diffCode as string);
17131750
const encoder = new TextEncoder();
@@ -1736,7 +1773,7 @@ describe("Diff test", () => {
17361773
}),
17371774
),
17381775
removedNodes: Array.from(mutation.removedNodes).map(
1739-
(node, index) => ({
1776+
(node) => ({
17401777
nodeName: node.nodeName,
17411778
nodeValue: node.nodeValue,
17421779
}),
@@ -1767,7 +1804,7 @@ describe("Diff test", () => {
17671804
nodeValue: node.nodeValue,
17681805
} as Node);
17691806
}
1770-
: undefined;
1807+
: eval(onNextNode);
17711808

17721809
if (registerWC) {
17731810
class TestWC extends HTMLElement {
@@ -1792,7 +1829,7 @@ describe("Diff test", () => {
17921829

17931830
observer.disconnect();
17941831

1795-
return [allMutations, streamNodes, transitionApplied];
1832+
return [allMutations, streamNodes, transitionApplied, (window as any).logs];
17961833
},
17971834
[
17981835
diffCode,
@@ -1802,6 +1839,7 @@ describe("Diff test", () => {
18021839
transition,
18031840
ignoreId,
18041841
registerWC,
1842+
onNextNode,
18051843
],
18061844
);
18071845

@@ -1810,6 +1848,7 @@ describe("Diff test", () => {
18101848
mutations,
18111849
streamNodes,
18121850
transitionApplied,
1851+
logs
18131852
];
18141853
}
18151854
});

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ async function htmlStreamWalker(
257257
nextNode = nextNode![field];
258258
}
259259

260-
if (nextNode) options.onNextNode?.(nextNode);
260+
if (nextNode) await options.onNextNode?.(nextNode);
261261

262262
const waitChildren = field === "firstChild";
263263

0 commit comments

Comments
 (0)