Skip to content

Commit f5048fc

Browse files
committed
deduplicate each block context generation for keyed blocks
1 parent dc0cb60 commit f5048fc

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

src/compile/nodes/EachBlock.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class EachBlock extends Node {
6666

6767
this.var = block.getUniqueName(`each`);
6868
this.iterations = block.getUniqueName(`${this.var}_blocks`);
69-
this.get_each_context = block.getUniqueName(`get_${this.var}_context`);
69+
this.get_each_context = this.compiler.getUniqueName(`get_${this.var}_context`);
7070

7171
const { dependencies } = this.expression;
7272
block.addDependencies(dependencies);
@@ -296,9 +296,7 @@ export default class EachBlock extends Node {
296296
const ${get_key} = ctx => ${this.key.snippet};
297297
298298
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
299-
let child_ctx = @assign(@assign({}, ctx), {
300-
${this.contextProps.join(',\n')}
301-
});
299+
let child_ctx = ${this.get_each_context}(ctx, ${each_block_value}, #i);
302300
let key = ${get_key}(child_ctx);
303301
${blocks}[#i] = ${lookup}[key] = ${create_each_block}(#component, key, child_ctx);
304302
}
@@ -327,11 +325,7 @@ export default class EachBlock extends Node {
327325
block.builders.update.addBlock(deindent`
328326
var ${each_block_value} = ${snippet};
329327
330-
${blocks} = @updateKeyedEach(${blocks}, #component, changed, ${get_key}, ${dynamic ? '1' : '0'}, ${each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", ${anchor}, function(#i) {
331-
return @assign(@assign({}, ctx), {
332-
${this.contextProps.join(',\n')}
333-
});
334-
});
328+
${blocks} = @updateKeyedEach(${blocks}, #component, changed, ${get_key}, ${dynamic ? '1' : '0'}, ctx, ${each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", ${anchor}, ${this.get_each_context});
335329
`);
336330

337331
if (!parentNode) {

src/shared/keyed-each.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function outroAndDestroyBlock(block, lookup) {
1010
});
1111
}
1212

13-
export function updateKeyedEach(old_blocks, component, changed, get_key, dynamic, list, lookup, node, has_outro, create_each_block, intro_method, next, get_context) {
13+
export function updateKeyedEach(old_blocks, component, changed, get_key, dynamic, ctx, list, lookup, node, has_outro, create_each_block, intro_method, next, get_context) {
1414
var o = old_blocks.length;
1515
var n = list.length;
1616

@@ -24,15 +24,15 @@ export function updateKeyedEach(old_blocks, component, changed, get_key, dynamic
2424

2525
var i = n;
2626
while (i--) {
27-
var ctx = get_context(i);
28-
var key = get_key(ctx);
27+
var child_ctx = get_context(ctx, list, i);
28+
var key = get_key(child_ctx);
2929
var block = lookup[key];
3030

3131
if (!block) {
32-
block = create_each_block(component, key, ctx);
32+
block = create_each_block(component, key, child_ctx);
3333
block.c();
3434
} else if (dynamic) {
35-
block.p(changed, ctx);
35+
block.p(changed, child_ctx);
3636
}
3737

3838
new_blocks[i] = new_lookup[key] = block;

test/js/samples/deconflict-builtins/expected-bundle.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,7 @@ function create_main_fragment(component, ctx) {
161161
var each_blocks = [];
162162

163163
for (var i = 0; i < each_value.length; i += 1) {
164-
each_blocks[i] = create_each_block(component, assign(assign({}, ctx), {
165-
each_value: each_value,
166-
node: each_value[i],
167-
node_index: i
168-
}));
164+
each_blocks[i] = create_each_block(component, get_each_context(ctx, each_value, i));
169165
}
170166

171167
return {
@@ -190,16 +186,12 @@ function create_main_fragment(component, ctx) {
190186
each_value = ctx.createElement;
191187

192188
for (var i = 0; i < each_value.length; i += 1) {
193-
var each_context = assign(assign({}, ctx), {
194-
each_value: each_value,
195-
node: each_value[i],
196-
node_index: i
197-
});
189+
const child_ctx = get_each_context(ctx, each_value, i);
198190

199191
if (each_blocks[i]) {
200-
each_blocks[i].p(changed, each_context);
192+
each_blocks[i].p(changed, child_ctx);
201193
} else {
202-
each_blocks[i] = create_each_block(component, each_context);
194+
each_blocks[i] = create_each_block(component, child_ctx);
203195
each_blocks[i].c();
204196
each_blocks[i].m(each_anchor.parentNode, each_anchor);
205197
}
@@ -256,6 +248,14 @@ function create_each_block(component, ctx) {
256248
};
257249
}
258250

251+
function get_each_context(ctx, list, i) {
252+
return assign(assign({}, ctx), {
253+
each_value: list,
254+
node: list[i],
255+
node_index: i
256+
});
257+
}
258+
259259
function SvelteComponent(options) {
260260
init(this, options);
261261
this._state = assign({}, options.data);

test/js/samples/deconflict-builtins/expected.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ function create_main_fragment(component, ctx) {
99
var each_blocks = [];
1010

1111
for (var i = 0; i < each_value.length; i += 1) {
12-
each_blocks[i] = create_each_block(component, assign(assign({}, ctx), {
13-
each_value: each_value,
14-
node: each_value[i],
15-
node_index: i
16-
}));
12+
each_blocks[i] = create_each_block(component, get_each_context(ctx, each_value, i));
1713
}
1814

1915
return {
@@ -38,16 +34,12 @@ function create_main_fragment(component, ctx) {
3834
each_value = ctx.createElement;
3935

4036
for (var i = 0; i < each_value.length; i += 1) {
41-
var each_context = assign(assign({}, ctx), {
42-
each_value: each_value,
43-
node: each_value[i],
44-
node_index: i
45-
});
37+
const child_ctx = get_each_context(ctx, each_value, i);
4638

4739
if (each_blocks[i]) {
48-
each_blocks[i].p(changed, each_context);
40+
each_blocks[i].p(changed, child_ctx);
4941
} else {
50-
each_blocks[i] = create_each_block(component, each_context);
42+
each_blocks[i] = create_each_block(component, child_ctx);
5143
each_blocks[i].c();
5244
each_blocks[i].m(each_anchor.parentNode, each_anchor);
5345
}
@@ -104,6 +96,14 @@ function create_each_block(component, ctx) {
10496
};
10597
}
10698

99+
function get_each_context(ctx, list, i) {
100+
return assign(assign({}, ctx), {
101+
each_value: list,
102+
node: list[i],
103+
node_index: i
104+
});
105+
}
106+
107107
function SvelteComponent(options) {
108108
init(this, options);
109109
this._state = assign({}, options.data);

0 commit comments

Comments
 (0)