dont pass around reference to component

pull/1940/head
Richard Harris 6 years ago
parent e504332730
commit 016bf84800

@ -104,9 +104,7 @@ export default class Block {
this.getUniqueName = this.renderer.component.getUniqueNameMaker(); this.getUniqueName = this.renderer.component.getUniqueNameMaker();
this.variables = new Map(); this.variables = new Map();
this.aliases = new Map() this.aliases = new Map().set('ctx', this.getUniqueName('ctx'));
.set('component', this.getUniqueName('component'))
.set('ctx', this.getUniqueName('ctx'));
if (this.key) this.aliases.set('key', this.getUniqueName('key')); if (this.key) this.aliases.set('key', this.getUniqueName('key'));
this.hasUpdateMethod = false; // determined later this.hasUpdateMethod = false; // determined later
@ -410,7 +408,7 @@ export default class Block {
return deindent` return deindent`
${this.comment && `// ${this.comment}`} ${this.comment && `// ${this.comment}`}
function ${this.name}(${this.alias('component')}, ${this.key ? `${localKey}, ` : ''}ctx) { function ${this.name}($$, ${this.key ? `${localKey}, ` : ''}ctx) {
${this.getContents(localKey)} ${this.getContents(localKey)}
} }
`; `;

@ -238,7 +238,7 @@ export default function dom(
if (component.props.length > 0 || component.has_reactive_assignments) args.push('$$props', '$$invalidate'); if (component.props.length > 0 || component.has_reactive_assignments) args.push('$$props', '$$invalidate');
builder.addBlock(deindent` builder.addBlock(deindent`
function create_fragment(${component.alias('component')}, ctx) { function create_fragment($$, ctx) {
${block.getContents()} ${block.getContents()}
} }

@ -135,7 +135,7 @@ export default class AwaitBlockWrapper extends Wrapper {
block.maintainContext = true; block.maintainContext = true;
const infoProps = [ const infoProps = [
block.alias('component') === 'component' ? 'component' : `component: #component`, '$$',
'ctx', 'ctx',
'current: null', 'current: null',
this.pending.block.name && `pending: ${this.pending.block.name}`, this.pending.block.name && `pending: ${this.pending.block.name}`,

@ -216,7 +216,7 @@ export default class EachBlockWrapper extends Wrapper {
// TODO neaten this up... will end up with an empty line in the block // TODO neaten this up... will end up with an empty line in the block
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
if (!${this.vars.each_block_value}.${this.vars.length}) { if (!${this.vars.each_block_value}.${this.vars.length}) {
${each_block_else} = ${this.else.block.name}(#component, ctx); ${each_block_else} = ${this.else.block.name}($$, ctx);
${each_block_else}.c(); ${each_block_else}.c();
} }
`); `);
@ -234,7 +234,7 @@ export default class EachBlockWrapper extends Wrapper {
if (!${this.vars.each_block_value}.${this.vars.length} && ${each_block_else}) { if (!${this.vars.each_block_value}.${this.vars.length} && ${each_block_else}) {
${each_block_else}.p(changed, ctx); ${each_block_else}.p(changed, ctx);
} else if (!${this.vars.each_block_value}.${this.vars.length}) { } else if (!${this.vars.each_block_value}.${this.vars.length}) {
${each_block_else} = ${this.else.block.name}(#component, ctx); ${each_block_else} = ${this.else.block.name}($$, ctx);
${each_block_else}.c(); ${each_block_else}.c();
${each_block_else}.${mountOrIntro}(${initialMountNode}, ${this.vars.anchor}); ${each_block_else}.${mountOrIntro}(${initialMountNode}, ${this.vars.anchor});
} else if (${each_block_else}) { } else if (${each_block_else}) {
@ -250,7 +250,7 @@ export default class EachBlockWrapper extends Wrapper {
${each_block_else} = null; ${each_block_else} = null;
} }
} else if (!${each_block_else}) { } else if (!${each_block_else}) {
${each_block_else} = ${this.else.block.name}(#component, ctx); ${each_block_else} = ${this.else.block.name}($$, ctx);
${each_block_else}.c(); ${each_block_else}.c();
${each_block_else}.${mountOrIntro}(${initialMountNode}, ${this.vars.anchor}); ${each_block_else}.${mountOrIntro}(${initialMountNode}, ${this.vars.anchor});
} }
@ -307,7 +307,7 @@ export default class EachBlockWrapper extends Wrapper {
for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) { for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) {
let child_ctx = ${this.vars.get_each_context}(ctx, ${this.vars.each_block_value}, #i); let child_ctx = ${this.vars.get_each_context}(ctx, ${this.vars.each_block_value}, #i);
let key = ${get_key}(child_ctx); let key = ${get_key}(child_ctx);
${blocks}[#i] = ${lookup}[key] = ${create_each_block}(#component, key, child_ctx); ${blocks}[#i] = ${lookup}[key] = ${create_each_block}($$, key, child_ctx);
} }
`); `);
@ -343,7 +343,7 @@ export default class EachBlockWrapper extends Wrapper {
${this.block.hasOutros && `@group_outros();`} ${this.block.hasOutros && `@group_outros();`}
${this.node.hasAnimation && `for (let #i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].r();`} ${this.node.hasAnimation && `for (let #i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].r();`}
${blocks} = @updateKeyedEach(${blocks}, #component, changed, ${get_key}, ${dynamic ? '1' : '0'}, ctx, ${this.vars.each_block_value}, ${lookup}, ${updateMountNode}, ${destroy}, ${create_each_block}, "${mountOrIntro}", ${anchor}, ${this.vars.get_each_context}); ${blocks} = @updateKeyedEach(${blocks}, $$, changed, ${get_key}, ${dynamic ? '1' : '0'}, ctx, ${this.vars.each_block_value}, ${lookup}, ${updateMountNode}, ${destroy}, ${create_each_block}, "${mountOrIntro}", ${anchor}, ${this.vars.get_each_context});
${this.node.hasAnimation && `for (let #i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].a();`} ${this.node.hasAnimation && `for (let #i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].a();`}
`); `);
@ -378,7 +378,7 @@ export default class EachBlockWrapper extends Wrapper {
var ${iterations} = []; var ${iterations} = [];
for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) { for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) {
${iterations}[#i] = ${create_each_block}(#component, ${this.vars.get_each_context}(ctx, ${this.vars.each_block_value}, #i)); ${iterations}[#i] = ${create_each_block}($$, ${this.vars.get_each_context}(ctx, ${this.vars.each_block_value}, #i));
} }
`); `);
@ -440,7 +440,7 @@ export default class EachBlockWrapper extends Wrapper {
if (${iterations}[#i]) { if (${iterations}[#i]) {
${iterations}[#i].p(changed, child_ctx); ${iterations}[#i].p(changed, child_ctx);
} else { } else {
${iterations}[#i] = ${create_each_block}(#component, child_ctx); ${iterations}[#i] = ${create_each_block}($$, child_ctx);
${iterations}[#i].c(); ${iterations}[#i].c();
} }
${iterations}[#i].i(${updateMountNode}, ${anchor}); ${iterations}[#i].i(${updateMountNode}, ${anchor});
@ -449,13 +449,13 @@ export default class EachBlockWrapper extends Wrapper {
if (${iterations}[#i]) { if (${iterations}[#i]) {
${iterations}[#i].p(changed, child_ctx); ${iterations}[#i].p(changed, child_ctx);
} else { } else {
${iterations}[#i] = ${create_each_block}(#component, child_ctx); ${iterations}[#i] = ${create_each_block}($$, child_ctx);
${iterations}[#i].c(); ${iterations}[#i].c();
${iterations}[#i].m(${updateMountNode}, ${anchor}); ${iterations}[#i].m(${updateMountNode}, ${anchor});
} }
` `
: deindent` : deindent`
${iterations}[#i] = ${create_each_block}(#component, child_ctx); ${iterations}[#i] = ${create_each_block}($$, child_ctx);
${iterations}[#i].c(); ${iterations}[#i].c();
${iterations}[#i].${mountOrIntro}(${updateMountNode}, ${anchor}); ${iterations}[#i].${mountOrIntro}(${updateMountNode}, ${anchor});
`; `;

@ -123,11 +123,11 @@ export default class BindingWrapper {
const bindingGroup = getBindingGroup(parent.renderer, this.node.expression.node); const bindingGroup = getBindingGroup(parent.renderer, this.node.expression.node);
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`(#component.$$.binding_groups[${bindingGroup}] || (#component.$$.binding_groups[${bindingGroup}] = [])).push(${parent.var});` `($$.binding_groups[${bindingGroup}] || ($$.binding_groups[${bindingGroup}] = [])).push(${parent.var});`
); );
block.builders.destroy.addLine( block.builders.destroy.addLine(
`#component.$$.binding_groups[${bindingGroup}].splice(#component.$$.binding_groups[${bindingGroup}].indexOf(${parent.var}), 1);` `$$.binding_groups[${bindingGroup}].splice($$.binding_groups[${bindingGroup}].indexOf(${parent.var}), 1);`
); );
break; break;

@ -222,7 +222,7 @@ export default class IfBlockWrapper extends Wrapper {
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
var ${current_block_type} = ${select_block_type}(ctx); var ${current_block_type} = ${select_block_type}(ctx);
var ${name} = ${current_block_type_and}${current_block_type}(#component, ctx); var ${name} = ${current_block_type_and}${current_block_type}($$, ctx);
`); `);
const mountOrIntro = this.branches[0].block.hasIntroMethod ? 'i' : 'm'; const mountOrIntro = this.branches[0].block.hasIntroMethod ? 'i' : 'm';
@ -237,7 +237,7 @@ export default class IfBlockWrapper extends Wrapper {
const changeBlock = deindent` const changeBlock = deindent`
${if_name}${name}.d(1); ${if_name}${name}.d(1);
${name} = ${current_block_type_and}${current_block_type}(#component, ctx); ${name} = ${current_block_type_and}${current_block_type}($$, ctx);
${if_name}${name}.c(); ${if_name}${name}.c();
${if_name}${name}.${mountOrIntro}(${updateMountNode}, ${anchor}); ${if_name}${name}.${mountOrIntro}(${updateMountNode}, ${anchor});
`; `;
@ -301,12 +301,12 @@ export default class IfBlockWrapper extends Wrapper {
if (hasElse) { if (hasElse) {
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
${current_block_type_index} = ${select_block_type}(ctx); ${current_block_type_index} = ${select_block_type}(ctx);
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, ctx); ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}]($$, ctx);
`); `);
} else { } else {
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
if (~(${current_block_type_index} = ${select_block_type}(ctx))) { if (~(${current_block_type_index} = ${select_block_type}(ctx))) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, ctx); ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}]($$, ctx);
} }
`); `);
} }
@ -332,7 +332,7 @@ export default class IfBlockWrapper extends Wrapper {
const createNewBlock = deindent` const createNewBlock = deindent`
${name} = ${if_blocks}[${current_block_type_index}]; ${name} = ${if_blocks}[${current_block_type_index}];
if (!${name}) { if (!${name}) {
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](#component, ctx); ${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}]($$, ctx);
${name}.c(); ${name}.c();
} }
${name}.${mountOrIntro}(${updateMountNode}, ${anchor}); ${name}.${mountOrIntro}(${updateMountNode}, ${anchor});
@ -391,7 +391,7 @@ export default class IfBlockWrapper extends Wrapper {
const branch = this.branches[0]; const branch = this.branches[0];
block.builders.init.addBlock(deindent` block.builders.init.addBlock(deindent`
var ${name} = (${branch.condition}) && ${branch.block.name}(#component, ctx); var ${name} = (${branch.condition}) && ${branch.block.name}($$, ctx);
`); `);
const mountOrIntro = branch.block.hasIntroMethod ? 'i' : 'm'; const mountOrIntro = branch.block.hasIntroMethod ? 'i' : 'm';
@ -410,7 +410,7 @@ export default class IfBlockWrapper extends Wrapper {
if (${name}) { if (${name}) {
${name}.p(changed, ctx); ${name}.p(changed, ctx);
} else { } else {
${name} = ${branch.block.name}(#component, ctx); ${name} = ${branch.block.name}($$, ctx);
if (${name}) ${name}.c(); if (${name}) ${name}.c();
} }
@ -420,7 +420,7 @@ export default class IfBlockWrapper extends Wrapper {
if (${name}) { if (${name}) {
${name}.p(changed, ctx); ${name}.p(changed, ctx);
} else { } else {
${name} = ${branch.block.name}(#component, ctx); ${name} = ${branch.block.name}($$, ctx);
${name}.c(); ${name}.c();
${name}.m(${updateMountNode}, ${anchor}); ${name}.m(${updateMountNode}, ${anchor});
} }
@ -428,14 +428,14 @@ export default class IfBlockWrapper extends Wrapper {
: (branch.block.hasIntroMethod || branch.block.hasOutroMethod) : (branch.block.hasIntroMethod || branch.block.hasOutroMethod)
? deindent` ? deindent`
if (!${name}) { if (!${name}) {
${name} = ${branch.block.name}(#component, ctx); ${name} = ${branch.block.name}($$, ctx);
${name}.c(); ${name}.c();
} }
${name}.i(${updateMountNode}, ${anchor}); ${name}.i(${updateMountNode}, ${anchor});
` `
: deindent` : deindent`
if (!${name}) { if (!${name}) {
${name} = ${branch.block.name}(#component, ctx); ${name} = ${branch.block.name}($$, ctx);
${name}.c(); ${name}.c();
${name}.m(${updateMountNode}, ${anchor}); ${name}.m(${updateMountNode}, ${anchor});
} }

@ -46,7 +46,7 @@ export default class SlotWrapper extends Wrapper {
const content_name = block.getUniqueName(`slot_content_${sanitize(slotName)}`); const content_name = block.getUniqueName(`slot_content_${sanitize(slotName)}`);
const prop = quotePropIfNecessary(slotName); const prop = quotePropIfNecessary(slotName);
block.addVariable(content_name, `#component.$$.slotted${prop}`); block.addVariable(content_name, `$$.slotted${prop}`);
// TODO can we use isDomNode instead of type === 'Element'? // TODO can we use isDomNode instead of type === 'Element'?
const needsAnchorBefore = this.prev ? this.prev.node.type !== 'Element' : !parentNode; const needsAnchorBefore = this.prev ? this.prev.node.type !== 'Element' : !parentNode;

@ -98,7 +98,7 @@ export function init(component, options, instance, create_fragment, not_equal) {
$$.update(); $$.update();
ready = true; ready = true;
run_all($$.before_render); run_all($$.before_render);
$$.fragment = create_fragment(component, $$.ctx); $$.fragment = create_fragment($$, $$.ctx);
if (options.target) { if (options.target) {
intros.enabled = !!options.intro; intros.enabled = !!options.intro;

@ -11,7 +11,7 @@ export function handlePromise(promise, info) {
info.resolved = key && { [key]: value }; info.resolved = key && { [key]: value };
const child_ctx = assign(assign({}, info.ctx), info.resolved); const child_ctx = assign(assign({}, info.ctx), info.resolved);
const block = type && (info.current = type)(info.component, child_ctx); const block = type && (info.current = type)(info.$$, child_ctx);
if (info.block) { if (info.block) {
if (info.blocks) { if (info.blocks) {
@ -32,7 +32,7 @@ export function handlePromise(promise, info) {
block[block.i ? 'i' : 'm'](info.mount(), info.anchor); block[block.i ? 'i' : 'm'](info.mount(), info.anchor);
// TODO is some of this redundant? // TODO is some of this redundant?
run_all(info.component.$$.after_render); run_all(info.$$.after_render);
flush(); flush();
} }

Loading…
Cancel
Save