You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nice library! I have been trying to use set-dom and it works very well. However I was thinking of using it with HTML streaming and then it doesn't work anymore, I think it's because it's implemented with BFS and the streaming is received with DFS. I have tried to extend the changes to make it work but I have had some troubles.
Do you think it would be feasible to do it or is there any restriction that it has to be in BFS?
To transform chunks to nodes I use this:
constSTART_CHUNK_SELECTOR="S-C";constSTART_CHUNK_COMMENT=`<!--${START_CHUNK_SELECTOR}-->`;constdecoder=newTextDecoder();constparser=newDOMParser();/** * Create a generator that extracts nodes from a stream of HTML. * * This is useful to work with the RPC response stream and * transform the HTML into a stream of nodes to use in the * diffing algorithm. */exportdefaultasyncfunction*parseHTMLStream(streamReader: ReadableStreamDefaultReader<Uint8Array>,ignoreNodeTypes: Set<number>=newSet(),text="",): AsyncGenerator<Node>{const{ done, value }=awaitstreamReader.read();if(done)return;// Append the new chunk to the text with a marker.// This marker is necessary because without it, we// can't know where the new chunk starts and ends.text=`${text.replace(START_CHUNK_COMMENT,"")}${START_CHUNK_COMMENT}${decoder.decode(value)}`;// Find the start chunk nodefunctionstartChunk(){returndocument.createTreeWalker(parser.parseFromString(text,"text/html"),128,/* NodeFilter.SHOW_COMMENT */{acceptNode: (node)=>node.textContent===START_CHUNK_SELECTOR
? 1/* NodeFilter.FILTER_ACCEPT */
: 2/* NodeFilter.FILTER_REJECT */}).nextNode();}// Iterate over the chunk nodesfor(letnode=getNextNode(startChunk());node;node=getNextNode(node)){if(!ignoreNodeTypes.has(node.nodeType))yieldnode;}// Continue reading the streamyield*awaitparseHTMLStream(streamReader,ignoreNodeTypes,text);}/** * Get the next node in the tree. * It uses depth-first search in order to work with the streamed HTML. */exportfunctiongetNextNode(node: Node|null,deeperDone?: Boolean,): Node|null{if(!node)returnnull;if(node.childNodes.length&&!deeperDone)returnnode.firstChild;returnnode.nextSibling??getNextNode(node.parentNode,true);}
Nice library! I have been trying to use set-dom and it works very well. However I was thinking of using it with HTML streaming and then it doesn't work anymore, I think it's because it's implemented with BFS and the streaming is received with DFS. I have tried to extend the changes to make it work but I have had some troubles.
Do you think it would be feasible to do it or is there any restriction that it has to be in BFS?
To transform chunks to nodes I use this:
Then I can use the stream nodes directly with:
The text was updated successfully, but these errors were encountered: