@@ -15,6 +15,7 @@ export default function bind_this(component: Component, block: Block, binding: B
15
15
16
16
let lhs ;
17
17
let object ;
18
+ let body ;
18
19
19
20
if ( binding . is_contextual && binding . expression . node . type === 'Identifier' ) {
20
21
// bind:x={y} — we can't just do `y = x`, we need to
@@ -23,10 +24,19 @@ export default function bind_this(component: Component, block: Block, binding: B
23
24
const { snippet } = block . bindings . get ( name ) ;
24
25
lhs = snippet ;
25
26
26
- // TODO we need to invalidate... something
27
+ body = ` ${ lhs } = $$value` ; // TODO we need to invalidate... something
27
28
} else {
28
29
object = flatten_reference ( binding . expression . node ) . name ;
29
30
lhs = component . source . slice ( binding . expression . node . start , binding . expression . node . end ) . trim ( ) ;
31
+
32
+ body = binding . expression . node . type === 'Identifier'
33
+ ? deindent `
34
+ ${ component . invalidate ( object , `${ lhs } = $$value` ) } ;
35
+ `
36
+ : deindent `
37
+ ${ lhs } = $$value;
38
+ ${ component . invalidate ( object ) } ;
39
+ `
30
40
}
31
41
32
42
const contextual_dependencies = Array . from ( binding . expression . contextual_dependencies ) ;
@@ -35,8 +45,9 @@ export default function bind_this(component: Component, block: Block, binding: B
35
45
component . partly_hoisted . push ( deindent `
36
46
function ${ fn } (${ [ '$$value' , ...contextual_dependencies ] . join ( ', ' ) } ) {
37
47
if (${ lhs } === $$value) return;
38
- ${ lhs } = $$value;
39
- ${ object && component . invalidate ( object ) }
48
+ @binding_callbacks[$$value ? 'unshift' : 'push'](() => {
49
+ ${ body }
50
+ });
40
51
}
41
52
` ) ;
42
53
@@ -56,25 +67,29 @@ export default function bind_this(component: Component, block: Block, binding: B
56
67
57
68
const condition = Array . from ( contextual_dependencies ) . map ( name => `${ name } !== ctx.${ name } ` ) . join ( ' || ' ) ;
58
69
70
+ // we push unassign and unshift assign so that references are
71
+ // nulled out before they're created, to avoid glitches
72
+ // with shifting indices
59
73
block . builders . update . add_line ( deindent `
60
74
if (${ condition } ) {
61
75
${ unassign } ();
62
76
${ args . map ( a => `${ a } = ctx.${ a } ` ) . join ( ', ' ) } ;
63
- @add_binding_callback( ${ assign } );
77
+ ${ assign } ( );
64
78
}`
65
79
) ;
66
80
67
81
block . builders . destroy . add_line ( `${ unassign } ();` ) ;
68
- return `@add_binding_callback( ${ assign } );` ;
82
+ return `${ assign } ( );` ;
69
83
}
70
84
71
85
component . partly_hoisted . push ( deindent `
72
86
function ${ fn } ($$value) {
73
- ${ lhs } = $$value;
74
- ${ object && component . invalidate ( object ) }
87
+ @binding_callbacks[$$value ? 'unshift' : 'push'](() => {
88
+ ${ body }
89
+ });
75
90
}
76
91
` ) ;
77
92
78
93
block . builders . destroy . add_line ( `ctx.${ fn } (null);` ) ;
79
- return `@add_binding_callback(() => ctx.${ fn } (${ variable } ) );` ;
94
+ return `ctx.${ fn } (${ variable } );` ;
80
95
}
0 commit comments