@@ -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} ) ;
0 commit comments