optimise spread code by passing false to get_spread_update for static attributes

pull/4896/head
Tan Li Hau 5 years ago
parent a4dadf82be
commit 099b6f002a

@ -676,7 +676,7 @@ export default class ElementWrapper extends Wrapper {
initial_props.push(snippet); initial_props.push(snippet);
updates.push(condition ? x`${condition} && ${snippet}` : snippet); updates.push(condition ? x`${condition} && ${snippet}` : x`false`);
} else { } else {
const metadata = attr.get_metadata(); const metadata = attr.get_metadata();
const name = attr.is_indirectly_bound_value() const name = attr.is_indirectly_bound_value()
@ -685,17 +685,14 @@ export default class ElementWrapper extends Wrapper {
const snippet = x`{ ${name}: ${attr.get_value(block)} }`; const snippet = x`{ ${name}: ${attr.get_value(block)} }`;
initial_props.push(snippet); initial_props.push(snippet);
updates.push(condition ? x`${condition} && ${snippet}` : snippet); updates.push(condition ? x`${condition} && ${snippet}` : x`false`);
} }
}); });
block.chunks.init.push(b` block.chunks.init.push(b`
let ${levels} = [${initial_props}]; let ${levels} = [${initial_props}];
let ${data} = {}; let ${data} = @get_attributes_for_spread(${levels});
for (let #i = 0; #i < ${levels}.length; #i += 1) {
${data} = @assign(${data}, ${levels}[#i]);
}
`); `);
const fn = this.node.namespace === namespaces.svg ? x`@set_svg_attributes` : x`@set_attributes`; const fn = this.node.namespace === namespaces.svg ? x`@set_svg_attributes` : x`@set_attributes`;

@ -252,7 +252,7 @@ export default class InlineComponentWrapper extends Wrapper {
changes.push( changes.push(
unchanged unchanged
? x`${levels}[${i}]` ? x`false`
: condition : condition
? x`${condition} && ${change_object}` ? x`${condition} && ${change_object}`
: change_object : change_object
@ -266,9 +266,7 @@ export default class InlineComponentWrapper extends Wrapper {
`); `);
statements.push(b` statements.push(b`
for (let #i = 0; #i < ${levels}.length; #i += 1) { ${props} = @assign(${props}, @get_attributes_for_spread(${levels}))
${props} = @assign(${props}, ${levels}[#i]);
}
`); `);
if (all_dependencies.size) { if (all_dependencies.size) {

@ -1,3 +1,5 @@
import { assign } from "./utils";
export function get_spread_update(levels, updates) { export function get_spread_update(levels, updates) {
const update = {}; const update = {};
@ -11,7 +13,7 @@ export function get_spread_update(levels, updates) {
if (n) { if (n) {
for (const key in o) { for (const key in o) {
if (!(key in n)) to_null_out[key] = 1; if (!(key in n) && !accounted_for[key]) to_null_out[key] = 1;
} }
for (const key in n) { for (const key in n) {
@ -24,6 +26,11 @@ export function get_spread_update(levels, updates) {
levels[i] = n; levels[i] = n;
} else { } else {
for (const key in o) { for (const key in o) {
// if spreads decides to null out the key
// should reset it back for static attribute
if (to_null_out[key]) {
update[key] = o[key];
}
accounted_for[key] = 1; accounted_for[key] = 1;
} }
} }
@ -36,6 +43,14 @@ export function get_spread_update(levels, updates) {
return update; return update;
} }
export function get_attributes_for_spread(levels) {
let attrs = {};
for (let i = 0; i < levels.length; i += 1) {
attrs = assign(attrs, levels[i]);
}
return attrs;
}
export function get_spread_object(spread_props) { export function get_spread_object(spread_props) {
return typeof spread_props === 'object' && spread_props !== null ? spread_props : {}; return typeof spread_props === 'object' && spread_props !== null ? spread_props : {};
} }
Loading…
Cancel
Save