@@ -27,7 +27,6 @@ import {
27
27
import { source , mutable_source , set } from '../../reactivity/sources.js' ;
28
28
import { is_array , is_frozen , map_get , map_set } from '../../utils.js' ;
29
29
import { STATE_SYMBOL } from '../../constants.js' ;
30
- import { create_block } from './utils.js' ;
31
30
32
31
var NEW_ITEM = - 1 ;
33
32
var LIS_ITEM = - 2 ;
@@ -56,8 +55,6 @@ export function set_current_each_item(item) {
56
55
* @returns {void }
57
56
*/
58
57
function each ( anchor , get_collection , flags , get_key , render_fn , fallback_fn , reconcile_fn ) {
59
- var block = create_block ( ) ;
60
-
61
58
/** @type {import('#client').EachState } */
62
59
var state = { flags, items : [ ] } ;
63
60
@@ -72,124 +69,116 @@ function each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, re
72
69
/** @type {import('#client').Effect | null } */
73
70
var fallback = null ;
74
71
75
- var effect = render_effect (
76
- ( ) => {
77
- var collection = get_collection ( ) ;
72
+ var effect = render_effect ( ( ) => {
73
+ var collection = get_collection ( ) ;
78
74
79
- var array = is_array ( collection )
80
- ? collection
81
- : collection == null
82
- ? [ ]
83
- : Array . from ( collection ) ;
75
+ var array = is_array ( collection )
76
+ ? collection
77
+ : collection == null
78
+ ? [ ]
79
+ : Array . from ( collection ) ;
84
80
85
- var keys = get_key === null ? array : array . map ( get_key ) ;
81
+ var keys = get_key === null ? array : array . map ( get_key ) ;
86
82
87
- var length = array . length ;
83
+ var length = array . length ;
88
84
89
- // If we are working with an array that isn't proxied or frozen, then remove strict equality and ensure the items
90
- // are treated as reactive, so they get wrapped in a signal.
91
- var flags = state . flags ;
92
- if ( ( flags & EACH_IS_STRICT_EQUALS ) !== 0 && ! is_frozen ( array ) && ! ( STATE_SYMBOL in array ) ) {
93
- flags ^= EACH_IS_STRICT_EQUALS ;
85
+ // If we are working with an array that isn't proxied or frozen, then remove strict equality and ensure the items
86
+ // are treated as reactive, so they get wrapped in a signal.
87
+ var flags = state . flags ;
88
+ if ( ( flags & EACH_IS_STRICT_EQUALS ) !== 0 && ! is_frozen ( array ) && ! ( STATE_SYMBOL in array ) ) {
89
+ flags ^= EACH_IS_STRICT_EQUALS ;
94
90
95
- // Additionally if we're in an keyed each block, we'll need ensure the items are all wrapped in signals.
96
- if ( ( flags & EACH_KEYED ) !== 0 && ( flags & EACH_ITEM_REACTIVE ) === 0 ) {
97
- flags ^= EACH_ITEM_REACTIVE ;
98
- }
91
+ // Additionally if we're in an keyed each block, we'll need ensure the items are all wrapped in signals.
92
+ if ( ( flags & EACH_KEYED ) !== 0 && ( flags & EACH_ITEM_REACTIVE ) === 0 ) {
93
+ flags ^= EACH_ITEM_REACTIVE ;
99
94
}
95
+ }
100
96
101
- /** `true` if there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
102
- let mismatch = false ;
103
-
104
- if ( hydrating ) {
105
- var is_else =
106
- /** @type {Comment } */ ( current_hydration_fragment ?. [ 0 ] ) ?. data === 'ssr:each_else' ;
97
+ /** `true` if there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
98
+ let mismatch = false ;
99
+
100
+ if ( hydrating ) {
101
+ var is_else =
102
+ /** @type {Comment } */ ( current_hydration_fragment ?. [ 0 ] ) ?. data === 'ssr:each_else' ;
103
+
104
+ if ( is_else !== ( length === 0 ) ) {
105
+ // hydration mismatch — remove the server-rendered DOM and start over
106
+ remove ( current_hydration_fragment ) ;
107
+ set_current_hydration_fragment ( null ) ;
108
+ mismatch = true ;
109
+ } else if ( is_else ) {
110
+ // Remove the each_else comment node or else it will confuse the subsequent hydration algorithm
111
+ /** @type {import('#client').TemplateNode[] } */ ( current_hydration_fragment ) . shift ( ) ;
112
+ }
113
+ }
107
114
108
- if ( is_else !== ( length === 0 ) ) {
109
- // hydration mismatch — remove the server-rendered DOM and start over
110
- remove ( current_hydration_fragment ) ;
111
- set_current_hydration_fragment ( null ) ;
115
+ // this is separate to the previous block because `hydrating` might change
116
+ if ( hydrating ) {
117
+ var b_items = [ ] ;
118
+
119
+ // Hydrate block
120
+ var hydration_list = /** @type {import('#client').TemplateNode[] } */ (
121
+ current_hydration_fragment
122
+ ) ;
123
+ var hydrating_node = hydration_list [ 0 ] ;
124
+
125
+ for ( var i = 0 ; i < length ; i ++ ) {
126
+ var fragment = get_hydration_fragment ( hydrating_node ) ;
127
+ set_current_hydration_fragment ( fragment ) ;
128
+ if ( ! fragment ) {
129
+ // If fragment is null, then that means that the server rendered less items than what
130
+ // the client code specifies -> break out and continue with client-side node creation
112
131
mismatch = true ;
113
- } else if ( is_else ) {
114
- // Remove the each_else comment node or else it will confuse the subsequent hydration algorithm
115
- /** @type {import('#client').TemplateNode[] } */ ( current_hydration_fragment ) . shift ( ) ;
132
+ break ;
116
133
}
117
- }
118
134
119
- // this is separate to the previous block because `hydrating` might change
120
- if ( hydrating ) {
121
- var b_items = [ ] ;
135
+ b_items [ i ] = create_item ( array [ i ] , keys ?. [ i ] , i , render_fn , flags ) ;
122
136
123
- // Hydrate block
124
- var hydration_list = /** @type {import('#client').TemplateNode[] } */ (
125
- current_hydration_fragment
137
+ // TODO helperise this
138
+ hydrating_node = /** @type {import('#client').TemplateNode } */ (
139
+ /** @type {Node } */ (
140
+ /** @type {Node } */ ( fragment [ fragment . length - 1 ] || hydrating_node ) . nextSibling
141
+ ) . nextSibling
126
142
) ;
127
- var hydrating_node = hydration_list [ 0 ] ;
128
-
129
- for ( var i = 0 ; i < length ; i ++ ) {
130
- var fragment = get_hydration_fragment ( hydrating_node ) ;
131
- set_current_hydration_fragment ( fragment ) ;
132
- if ( ! fragment ) {
133
- // If fragment is null, then that means that the server rendered less items than what
134
- // the client code specifies -> break out and continue with client-side node creation
135
- mismatch = true ;
136
- break ;
137
- }
138
-
139
- b_items [ i ] = create_item ( array [ i ] , keys ?. [ i ] , i , render_fn , flags ) ;
140
-
141
- // TODO helperise this
142
- hydrating_node = /** @type {import('#client').TemplateNode } */ (
143
- /** @type {Node } */ (
144
- /** @type {Node } */ ( fragment [ fragment . length - 1 ] || hydrating_node ) . nextSibling
145
- ) . nextSibling
146
- ) ;
147
- }
143
+ }
148
144
149
- remove_excess_hydration_nodes ( hydration_list , hydrating_node ) ;
145
+ remove_excess_hydration_nodes ( hydration_list , hydrating_node ) ;
150
146
151
- state . items = b_items ;
152
- }
147
+ state . items = b_items ;
148
+ }
153
149
154
- if ( ! hydrating ) {
155
- // TODO add 'empty controlled block' optimisation here
156
- reconcile_fn ( array , state , anchor , render_fn , flags , keys ) ;
157
- }
150
+ if ( ! hydrating ) {
151
+ // TODO add 'empty controlled block' optimisation here
152
+ reconcile_fn ( array , state , anchor , render_fn , flags , keys ) ;
153
+ }
158
154
159
- if ( fallback_fn !== null ) {
160
- if ( length === 0 ) {
161
- if ( fallback ) {
162
- resume_effect ( fallback ) ;
163
- } else {
164
- fallback = render_effect (
165
- ( ) => {
166
- var dom = fallback_fn ( anchor ) ;
167
-
168
- return ( ) => {
169
- if ( dom !== undefined ) {
170
- remove ( dom ) ;
171
- }
172
- } ;
173
- } ,
174
- block ,
175
- true
176
- ) ;
177
- }
178
- } else if ( fallback !== null ) {
179
- pause_effect ( fallback , ( ) => {
180
- fallback = null ;
181
- } ) ;
155
+ if ( fallback_fn !== null ) {
156
+ if ( length === 0 ) {
157
+ if ( fallback ) {
158
+ resume_effect ( fallback ) ;
159
+ } else {
160
+ fallback = render_effect ( ( ) => {
161
+ var dom = fallback_fn ( anchor ) ;
162
+
163
+ return ( ) => {
164
+ if ( dom !== undefined ) {
165
+ remove ( dom ) ;
166
+ }
167
+ } ;
168
+ } , true ) ;
182
169
}
170
+ } else if ( fallback !== null ) {
171
+ pause_effect ( fallback , ( ) => {
172
+ fallback = null ;
173
+ } ) ;
183
174
}
175
+ }
184
176
185
- if ( mismatch ) {
186
- // Set a fragment so that Svelte continues to operate in hydration mode
187
- set_current_hydration_fragment ( [ ] ) ;
188
- }
189
- } ,
190
- block ,
191
- false
192
- ) ;
177
+ if ( mismatch ) {
178
+ // Set a fragment so that Svelte continues to operate in hydration mode
179
+ set_current_hydration_fragment ( [ ] ) ;
180
+ }
181
+ } ) ;
193
182
194
183
effect . ondestroy = ( ) => {
195
184
for ( var item of state . items ) {
@@ -618,19 +607,15 @@ function create_item(value, key, index, render_fn, flags) {
618
607
try {
619
608
current_each_item = item ;
620
609
621
- item . e = render_effect (
622
- ( ) => {
623
- var dom = render_fn ( null , item . v , item . i ) ;
610
+ item . e = render_effect ( ( ) => {
611
+ var dom = render_fn ( null , item . v , item . i ) ;
624
612
625
- return ( ) => {
626
- if ( dom !== undefined ) {
627
- remove ( dom ) ;
628
- }
629
- } ;
630
- } ,
631
- item ,
632
- true
633
- ) ;
613
+ return ( ) => {
614
+ if ( dom !== undefined ) {
615
+ remove ( dom ) ;
616
+ }
617
+ } ;
618
+ } , true ) ;
634
619
635
620
return item ;
636
621
} finally {
0 commit comments