@@ -14,6 +14,8 @@ import { block, branch, pause_effect, resume_effect } from '../../reactivity/eff
1414import { HYDRATION_START_ELSE , UNINITIALIZED } from '../../../../constants.js' ;
1515import { create_text , should_defer_append } from '../operations.js' ;
1616import { current_batch } from '../../reactivity/batch.js' ;
17+ import { BranchManager } from './branches.js' ;
18+ import { noop } from '../../../shared/utils.js' ;
1719
1820// TODO reinstate https://github.com/sveltejs/svelte/pull/15250
1921
@@ -30,12 +32,6 @@ export function if_block(node, fn, elseif = false) {
3032
3133 var anchor = node ;
3234
33- /** @type {Effect | null } */
34- var consequent_effect = null ;
35-
36- /** @type {Effect | null } */
37- var alternate_effect = null ;
38-
3935 /** @type {typeof UNINITIALIZED | boolean | null } */
4036 var condition = UNINITIALIZED ;
4137
@@ -48,42 +44,12 @@ export function if_block(node, fn, elseif = false) {
4844 update_branch ( flag , fn ) ;
4945 } ;
5046
51- /** @type {DocumentFragment | null } */
52- var offscreen_fragment = null ;
53-
54- function commit ( ) {
55- if ( offscreen_fragment !== null ) {
56- // remove the anchor
57- /** @type {Text } */ ( offscreen_fragment . lastChild ) . remove ( ) ;
58-
59- anchor . before ( offscreen_fragment ) ;
60- offscreen_fragment = null ;
61- }
62-
63- var active = condition ? consequent_effect : alternate_effect ;
64- var inactive = condition ? alternate_effect : consequent_effect ;
65-
66- if ( active ) {
67- resume_effect ( active ) ;
68- }
69-
70- if ( inactive ) {
71- pause_effect ( inactive , ( ) => {
72- if ( condition ) {
73- alternate_effect = null ;
74- } else {
75- consequent_effect = null ;
76- }
77- } ) ;
78- }
79- }
47+ var branches = new BranchManager ( anchor ) ;
8048
8149 const update_branch = (
8250 /** @type {boolean | null } */ new_condition ,
8351 /** @type {null | ((anchor: Node) => void) } */ fn
8452 ) => {
85- if ( condition === ( condition = new_condition ) ) return ;
86-
8753 /** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
8854 let mismatch = false ;
8955
@@ -101,33 +67,7 @@ export function if_block(node, fn, elseif = false) {
10167 }
10268 }
10369
104- var defer = should_defer_append ( ) ;
105- var target = anchor ;
106-
107- if ( defer ) {
108- offscreen_fragment = document . createDocumentFragment ( ) ;
109- offscreen_fragment . append ( ( target = create_text ( ) ) ) ;
110- }
111-
112- if ( condition ) {
113- consequent_effect ??= fn && branch ( ( ) => fn ( target ) ) ;
114- } else {
115- alternate_effect ??= fn && branch ( ( ) => fn ( target ) ) ;
116- }
117-
118- if ( defer ) {
119- var batch = /** @type {Batch } */ ( current_batch ) ;
120-
121- var active = condition ? consequent_effect : alternate_effect ;
122- var inactive = condition ? alternate_effect : consequent_effect ;
123-
124- if ( active ) batch . skipped_effects . delete ( active ) ;
125- if ( inactive ) batch . skipped_effects . add ( inactive ) ;
126-
127- batch . add_callback ( commit ) ;
128- } else {
129- commit ( ) ;
130- }
70+ branches . ensure ( new_condition , fn ?? noop ) ;
13171
13272 if ( mismatch ) {
13373 // continue in hydration mode
0 commit comments