|
| 1 | +import Wrapper from './shared/Wrapper'; |
| 2 | +import Renderer from '../Renderer'; |
| 3 | +import Block from '../Block'; |
| 4 | +import FragmentWrapper from './Fragment'; |
| 5 | +import { b, x } from 'code-red'; |
| 6 | +import { Identifier } from 'estree'; |
| 7 | +import DynamicElement from '../../nodes/DynamicElement'; |
| 8 | +import ElementWrapper from './Element/index'; |
| 9 | +import create_debugging_comment from './shared/create_debugging_comment'; |
| 10 | +import Element from '../../nodes/Element'; |
| 11 | + |
| 12 | +export default class DynamicElementWrapper extends Wrapper { |
| 13 | + fragment: FragmentWrapper; |
| 14 | + node: DynamicElement; |
| 15 | + elementWrapper: ElementWrapper; |
| 16 | + block: Block; |
| 17 | + dependencies: string[]; |
| 18 | + var: Identifier = { type: 'Identifier', name: 'dynamic_element' }; |
| 19 | + |
| 20 | + constructor( |
| 21 | + renderer: Renderer, |
| 22 | + block: Block, |
| 23 | + parent: Wrapper, |
| 24 | + node: DynamicElement, |
| 25 | + strip_whitespace: boolean, |
| 26 | + next_sibling: Wrapper |
| 27 | + ) { |
| 28 | + super(renderer, block, parent, node); |
| 29 | + |
| 30 | + this.not_static_content(); |
| 31 | + this.dependencies = node.tag.dynamic_dependencies(); |
| 32 | + |
| 33 | + if (this.dependencies.length) { |
| 34 | + block = block.child({ |
| 35 | + comment: create_debugging_comment(node, renderer.component), |
| 36 | + name: renderer.component.get_unique_name('dynamic_element_block'), |
| 37 | + type: 'dynamic_element' |
| 38 | + }); |
| 39 | + renderer.blocks.push(block); |
| 40 | + } |
| 41 | + |
| 42 | + (node as unknown as Element).dynamic_tag = node.tag; |
| 43 | + |
| 44 | + this.block = block; |
| 45 | + this.elementWrapper = new ElementWrapper( |
| 46 | + renderer, |
| 47 | + this.block, |
| 48 | + parent, |
| 49 | + (node as unknown) as Element, |
| 50 | + strip_whitespace, |
| 51 | + next_sibling |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + render(block: Block, parent_node: Identifier, parent_nodes: Identifier) { |
| 56 | + if (this.dependencies.length === 0) { |
| 57 | + this.render_static_tag(block, parent_node, parent_nodes); |
| 58 | + } else { |
| 59 | + this.render_dynamic_tag(block, parent_node, parent_nodes); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + render_static_tag( |
| 64 | + _block: Block, |
| 65 | + parent_node: Identifier, |
| 66 | + parent_nodes: Identifier |
| 67 | + ) { |
| 68 | + this.elementWrapper.render(this.block, parent_node, parent_nodes); |
| 69 | + } |
| 70 | + |
| 71 | + render_dynamic_tag( |
| 72 | + block: Block, |
| 73 | + parent_node: Identifier, |
| 74 | + parent_nodes: Identifier |
| 75 | + ) { |
| 76 | + this.elementWrapper.render( |
| 77 | + this.block, |
| 78 | + null, |
| 79 | + (x`#nodes` as unknown) as Identifier |
| 80 | + ); |
| 81 | + |
| 82 | + const has_transitions = !!( |
| 83 | + this.block.has_intro_method || this.block.has_outro_method |
| 84 | + ); |
| 85 | + const dynamic = this.block.has_update_method; |
| 86 | + |
| 87 | + const previous_tag = block.get_unique_name('previous_tag'); |
| 88 | + const snippet = this.node.tag.manipulate(block); |
| 89 | + block.add_variable(previous_tag, snippet); |
| 90 | + |
| 91 | + const not_equal = this.renderer.component.component_options.immutable |
| 92 | + ? x`@not_equal` |
| 93 | + : x`@safe_not_equal`; |
| 94 | + const condition = x`${this.renderer.dirty( |
| 95 | + this.dependencies |
| 96 | + )} && ${not_equal}(${previous_tag}, ${previous_tag} = ${snippet})`; |
| 97 | + |
| 98 | + block.chunks.init.push(b` |
| 99 | + let ${this.var} = ${this.block.name}(#ctx); |
| 100 | + `); |
| 101 | + |
| 102 | + block.chunks.create.push(b`${this.var}.c();`); |
| 103 | + |
| 104 | + if (this.renderer.options.hydratable) { |
| 105 | + block.chunks.claim.push(b`${this.var}.l(${parent_nodes});`); |
| 106 | + } |
| 107 | + |
| 108 | + block.chunks.mount.push( |
| 109 | + b`${this.var}.m(${parent_node || '#target'}, ${ |
| 110 | + parent_node ? 'null' : '#anchor' |
| 111 | + });` |
| 112 | + ); |
| 113 | + |
| 114 | + const anchor = this.get_or_create_anchor(block, parent_node, parent_nodes); |
| 115 | + const body = b` |
| 116 | + ${ |
| 117 | + has_transitions |
| 118 | + ? b` |
| 119 | + @group_outros(); |
| 120 | + @transition_out(${this.var}, 1, 1, @noop); |
| 121 | + @check_outros(); |
| 122 | + ` |
| 123 | + : b`${this.var}.d(1);` |
| 124 | + } |
| 125 | + ${this.var} = ${this.block.name}(#ctx); |
| 126 | + ${this.var}.c(); |
| 127 | + ${has_transitions && b`@transition_in(${this.var})`} |
| 128 | + ${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor}); |
| 129 | + `; |
| 130 | + |
| 131 | + if (dynamic) { |
| 132 | + block.chunks.update.push(b` |
| 133 | + if (${condition}) { |
| 134 | + ${body} |
| 135 | + } else { |
| 136 | + ${this.var}.p(#ctx, #dirty); |
| 137 | + } |
| 138 | + `); |
| 139 | + } else { |
| 140 | + block.chunks.update.push(b` |
| 141 | + if (${condition}) { |
| 142 | + ${body} |
| 143 | + } |
| 144 | + `); |
| 145 | + } |
| 146 | + |
| 147 | + if (has_transitions) { |
| 148 | + block.chunks.intro.push(b`@transition_in(${this.var})`); |
| 149 | + block.chunks.outro.push(b`@transition_out(${this.var})`); |
| 150 | + } |
| 151 | + |
| 152 | + block.chunks.destroy.push(b`${this.var}.d(detaching)`); |
| 153 | + } |
| 154 | +} |
0 commit comments