@@ -2493,6 +2493,12 @@ export const template_visitors = {
24932493 getters : { ...context . state . getters }
24942494 } ;
24952495
2496+ /** The state used when generating the key function, if necessary */
2497+ const key_state = {
2498+ ...context . state ,
2499+ getters : { ...context . state . getters }
2500+ } ;
2501+
24962502 /**
24972503 * @param {Pattern } expression_for_id
24982504 * @returns {Binding['mutation'] }
@@ -2550,8 +2556,12 @@ export const template_visitors = {
25502556 if ( node . index ) {
25512557 child_state . getters [ node . index ] = ( id ) => {
25522558 const index_with_loc = with_loc ( index , id ) ;
2553- return b . call ( '$.unwrap' , index_with_loc ) ;
2559+ return ( flags & EACH_INDEX_REACTIVE ) === 0
2560+ ? index_with_loc
2561+ : b . call ( '$.get' , index_with_loc ) ;
25542562 } ;
2563+
2564+ key_state . getters [ node . index ] = b . id ( node . index ) ;
25552565 }
25562566
25572567 /** @type {Statement[] } */
@@ -2565,6 +2575,8 @@ export const template_visitors = {
25652575 true
25662576 )
25672577 ) ;
2578+
2579+ key_state . getters [ node . context . name ] = node . context ;
25682580 } else {
25692581 const unwrapped = getter ( binding . node ) ;
25702582 const paths = extract_paths ( node . context ) ;
@@ -2592,23 +2604,20 @@ export const template_visitors = {
25922604 if ( context . state . options . dev ) {
25932605 declarations . push ( b . stmt ( getter ) ) ;
25942606 }
2607+
2608+ key_state . getters [ name ] = path . node ;
25952609 }
25962610 }
25972611
25982612 const block = /** @type {BlockStatement } */ ( context . visit ( node . body , child_state ) ) ;
25992613
2600- const key_function = node . key
2601- ? b . arrow (
2602- [ node . context . type === 'Identifier' ? node . context : b . id ( '$$item' ) , index ] ,
2603- declarations . length > 0
2604- ? b . block (
2605- declarations . concat (
2606- b . return ( /** @type {Expression } */ ( context . visit ( node . key , child_state ) ) )
2607- )
2608- )
2609- : /** @type {Expression } */ ( context . visit ( node . key , child_state ) )
2610- )
2611- : b . id ( '$.index' ) ;
2614+ /** @type {Expression } */
2615+ let key_function = b . id ( '$.index' ) ;
2616+
2617+ if ( node . key ) {
2618+ const expression = /** @type {Expression } */ ( context . visit ( node . key , key_state ) ) ;
2619+ key_function = b . arrow ( [ node . context , index ] , expression ) ;
2620+ }
26122621
26132622 if ( node . index && each_node_meta . contains_group_binding ) {
26142623 // We needed to create a unique identifier for the index above, but we want to use the
0 commit comments