@@ -13,13 +13,11 @@ import { HYDRATION_START_ELSE } from '../../../../constants.js';
1313
1414/**
1515 * @param {TemplateNode } node
16- * @param {() => boolean } get_condition
17- * @param {(anchor: Node) => void } consequent_fn
18- * @param {null | ((anchor: Node) => void) } [alternate_fn]
16+ * @param {(branch: (fn: (anchor: Node) => void, flag?: boolean) => void) => void } fn
1917 * @param {boolean } [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
2018 * @returns {void }
2119 */
22- export function if_block ( node , get_condition , consequent_fn , alternate_fn = null , elseif = false ) {
20+ export function if_block ( node , fn , elseif = false ) {
2321 if ( hydrating ) {
2422 hydrate_next ( ) ;
2523 }
@@ -37,8 +35,18 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
3735
3836 var flags = elseif ? EFFECT_TRANSPARENT : 0 ;
3937
40- block ( ( ) => {
41- if ( condition === ( condition = ! ! get_condition ( ) ) ) return ;
38+ var has_branch = false ;
39+
40+ const set_branch = ( /** @type {(anchor: Node) => void } */ fn , flag = true ) => {
41+ has_branch = true ;
42+ update_branch ( flag , fn ) ;
43+ } ;
44+
45+ const update_branch = (
46+ /** @type {boolean | null } */ new_condition ,
47+ /** @type {null | ((anchor: Node) => void) } */ fn
48+ ) => {
49+ if ( condition === ( condition = new_condition ) ) return ;
4250
4351 /** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
4452 let mismatch = false ;
@@ -60,8 +68,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
6068 if ( condition ) {
6169 if ( consequent_effect ) {
6270 resume_effect ( consequent_effect ) ;
63- } else {
64- consequent_effect = branch ( ( ) => consequent_fn ( anchor ) ) ;
71+ } else if ( fn ) {
72+ consequent_effect = branch ( ( ) => fn ( anchor ) ) ;
6573 }
6674
6775 if ( alternate_effect ) {
@@ -72,8 +80,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
7280 } else {
7381 if ( alternate_effect ) {
7482 resume_effect ( alternate_effect ) ;
75- } else if ( alternate_fn ) {
76- alternate_effect = branch ( ( ) => alternate_fn ( anchor ) ) ;
83+ } else if ( fn ) {
84+ alternate_effect = branch ( ( ) => fn ( anchor ) ) ;
7785 }
7886
7987 if ( consequent_effect ) {
@@ -87,6 +95,14 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
8795 // continue in hydration mode
8896 set_hydrating ( true ) ;
8997 }
98+ } ;
99+
100+ block ( ( ) => {
101+ has_branch = false ;
102+ fn ( set_branch ) ;
103+ if ( ! has_branch ) {
104+ update_branch ( null , null ) ;
105+ }
90106 } , flags ) ;
91107
92108 if ( hydrating ) {
0 commit comments