Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ jobs:
- name: Setup bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.2.2
bun-version: 1.2.9
- name: Install dependencies, build, and test
run: bun install
- run: bunx playwright@1.43.1 install
- run: bunx playwright@1.43.1 install-deps
- run: bunx playwright@1.51.1 install
- run: bunx playwright@1.51.1 install-deps
- run: bun run build
- run: bun run bundlewatch
- run: bun run test
22 changes: 11 additions & 11 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aralroca/diff-dom-streaming",
"version": "0.6.5",
"version": "0.6.6",
"exports": {
".": "./src/index.ts",
"./types": "./index.d.ts"
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diff-dom-streaming",
"version": "0.6.5",
"version": "0.6.6",
"bugs": "https://github.com/aralroca/diff-dom-streaming/issues",
"description": "Diff DOM algorithm with streaming. Gets all modifications, insertions and removals between a DOM fragment and a stream HTML reader.",
"keywords": [
Expand Down Expand Up @@ -53,9 +53,9 @@
"example:spa-like": "bun run ./examples/spa-like/index.ts"
},
"devDependencies": {
"typescript": "5.7.3",
"@types/bun": "1.2.2",
"bundlewatch": "0.4.0",
"playwright": "1.43.1"
"typescript": "5.8.3",
"@types/bun": "1.2.9",
"bundlewatch": "0.4.1",
"playwright": "1.51.1"
}
}
49 changes: 44 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,40 @@ describe("Diff test", () => {
);
});

it("should onNextNode execute in a sequential way when is async", async () => {
const results = await testDiff({
oldHTMLString: `
<div>foo</div>
`,
newHTMLStringChunks: ["<div scan>first</div>", "<div scan>second</div>", "<div scan>third</div>"],
registerWC: true,
onNextNode: `async (n) => {
if (!n?.hasAttribute?.('scan')) return
window.index ??= 1;
window.logs ??= '';
await new Promise((r) => setTimeout(() => {
window.logs += n.innerText + ' ';
r(true);
}, ++window.index * 50));
}`
});

expect(results[0]).toBe(
normalize(`
<html>
<head></head>
<body>
<div scan="">first</div>
<div scan="">second</div>
<div scan="">third</div>
</body>
</html>
`),
);

expect(results.at(-1)).toBe('first second third ')
});

it("should add WC that modifies the DOM on connect it (old with key)", async () => {
const [newHTML] = await testDiff({
oldHTMLString: `
Expand Down Expand Up @@ -1689,6 +1723,7 @@ describe("Diff test", () => {
transition = false,
ignoreId = false,
registerWC = false,
onNextNode,
}: {
oldHTMLString: string;
newHTMLStringChunks: string[];
Expand All @@ -1697,9 +1732,10 @@ describe("Diff test", () => {
transition?: boolean;
ignoreId?: boolean;
registerWC?: boolean;
}): Promise<[string, any[], Node[], boolean]> {
onNextNode?: string
}): Promise<[string, any[], Node[], boolean, string]> {
await page.setContent(normalize(oldHTMLString));
const [mutations, streamNodes, transitionApplied] = await page.evaluate(
const [mutations, streamNodes, transitionApplied, logs] = await page.evaluate(
async ([
diffCode,
newHTMLStringChunks,
Expand All @@ -1708,6 +1744,7 @@ describe("Diff test", () => {
transition,
ignoreId,
registerWC,
onNextNode,
]) => {
eval(diffCode as string);
const encoder = new TextEncoder();
Expand Down Expand Up @@ -1736,7 +1773,7 @@ describe("Diff test", () => {
}),
),
removedNodes: Array.from(mutation.removedNodes).map(
(node, index) => ({
(node) => ({
nodeName: node.nodeName,
nodeValue: node.nodeValue,
}),
Expand Down Expand Up @@ -1767,7 +1804,7 @@ describe("Diff test", () => {
nodeValue: node.nodeValue,
} as Node);
}
: undefined;
: eval(onNextNode);

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

observer.disconnect();

return [allMutations, streamNodes, transitionApplied];
return [allMutations, streamNodes, transitionApplied, (window as any).logs];
},
[
diffCode,
Expand All @@ -1802,6 +1839,7 @@ describe("Diff test", () => {
transition,
ignoreId,
registerWC,
onNextNode,
],
);

Expand All @@ -1810,6 +1848,7 @@ describe("Diff test", () => {
mutations,
streamNodes,
transitionApplied,
logs
];
}
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async function htmlStreamWalker(
nextNode = nextNode![field];
}

if (nextNode) options.onNextNode?.(nextNode);
if (nextNode) await options.onNextNode?.(nextNode);

const waitChildren = field === "firstChild";

Expand Down