@@ -13,13 +13,11 @@ import { HYDRATION_START_ELSE } from '../../../../constants.js';
13
13
14
14
/**
15
15
* @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
19
17
* @param {boolean } [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
20
18
* @returns {void }
21
19
*/
22
- export function if_block ( node , get_condition , consequent_fn , alternate_fn = null , elseif = false ) {
20
+ export function if_block ( node , fn , elseif = false ) {
23
21
if ( hydrating ) {
24
22
hydrate_next ( ) ;
25
23
}
@@ -37,8 +35,18 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
37
35
38
36
var flags = elseif ? EFFECT_TRANSPARENT : 0 ;
39
37
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 ;
42
50
43
51
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
44
52
let mismatch = false ;
@@ -60,8 +68,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
60
68
if ( condition ) {
61
69
if ( consequent_effect ) {
62
70
resume_effect ( consequent_effect ) ;
63
- } else {
64
- consequent_effect = branch ( ( ) => consequent_fn ( anchor ) ) ;
71
+ } else if ( fn ) {
72
+ consequent_effect = branch ( ( ) => fn ( anchor ) ) ;
65
73
}
66
74
67
75
if ( alternate_effect ) {
@@ -72,8 +80,8 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
72
80
} else {
73
81
if ( alternate_effect ) {
74
82
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 ) ) ;
77
85
}
78
86
79
87
if ( consequent_effect ) {
@@ -87,6 +95,14 @@ export function if_block(node, get_condition, consequent_fn, alternate_fn = null
87
95
// continue in hydration mode
88
96
set_hydrating ( true ) ;
89
97
}
98
+ } ;
99
+
100
+ block ( ( ) => {
101
+ has_branch = false ;
102
+ fn ( set_branch ) ;
103
+ if ( ! has_branch ) {
104
+ update_branch ( null , null ) ;
105
+ }
90
106
} , flags ) ;
91
107
92
108
if ( hydrating ) {
0 commit comments