( ast: html.Block, connectedBlocks: html.Block[], visitor: html.Visitor, bindingParser: BindingParser, )
| 59 | |
| 60 | /** Creates a deferred block from an HTML AST node. */ |
| 61 | export function createDeferredBlock( |
| 62 | ast: html.Block, |
| 63 | connectedBlocks: html.Block[], |
| 64 | visitor: html.Visitor, |
| 65 | bindingParser: BindingParser, |
| 66 | ): {node: t.DeferredBlock; errors: ParseError[]} { |
| 67 | const errors: ParseError[] = []; |
| 68 | const {placeholder, loading, error} = parseConnectedBlocks(connectedBlocks, errors, visitor); |
| 69 | const {triggers, prefetchTriggers, hydrateTriggers, definedName} = parsePrimaryTriggers( |
| 70 | ast, |
| 71 | bindingParser, |
| 72 | errors, |
| 73 | placeholder, |
| 74 | ); |
| 75 | |
| 76 | // The `defer` block has a main span encompassing all of the connected branches as well. |
| 77 | let lastEndSourceSpan = ast.endSourceSpan; |
| 78 | let endOfLastSourceSpan = ast.sourceSpan.end; |
| 79 | if (connectedBlocks.length > 0) { |
| 80 | const lastConnectedBlock = connectedBlocks[connectedBlocks.length - 1]; |
| 81 | lastEndSourceSpan = lastConnectedBlock.endSourceSpan; |
| 82 | endOfLastSourceSpan = lastConnectedBlock.sourceSpan.end; |
| 83 | } |
| 84 | |
| 85 | const sourceSpanWithConnectedBlocks = new ParseSourceSpan( |
| 86 | ast.sourceSpan.start, |
| 87 | endOfLastSourceSpan, |
| 88 | ); |
| 89 | |
| 90 | const node = new t.DeferredBlock( |
| 91 | html.visitAll(visitor, ast.children, ast.children), |
| 92 | triggers, |
| 93 | prefetchTriggers, |
| 94 | hydrateTriggers, |
| 95 | placeholder, |
| 96 | loading, |
| 97 | error, |
| 98 | ast.nameSpan, |
| 99 | sourceSpanWithConnectedBlocks, |
| 100 | ast.sourceSpan, |
| 101 | ast.startSourceSpan, |
| 102 | lastEndSourceSpan, |
| 103 | definedName, |
| 104 | ast.i18n, |
| 105 | ); |
| 106 | |
| 107 | return {node, errors}; |
| 108 | } |
| 109 | |
| 110 | function parseConnectedBlocks( |
| 111 | connectedBlocks: html.Block[], |
no test coverage detected