get rid of hardcoded variable names

pull/568/head
Rich-Harris 8 years ago
parent 23331e605a
commit f8e73c1f36

@ -101,6 +101,9 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
const key = block.getUniqueName( 'key' ); const key = block.getUniqueName( 'key' );
const lookup = block.getUniqueName( `${each_block}_lookup` ); const lookup = block.getUniqueName( `${each_block}_lookup` );
const iteration = block.getUniqueName( `${each_block}_iteration` ); const iteration = block.getUniqueName( `${each_block}_iteration` );
const head = block.getUniqueName( `${each_block}_head` );
const last = block.getUniqueName( `${each_block}_last` );
const expected = block.getUniqueName( `${each_block}_expected` );
if ( node.children[0] && node.children[0].type === 'Element' ) { // TODO or text/tag/raw if ( node.children[0] && node.children[0].type === 'Element' ) { // TODO or text/tag/raw
node._block.first = node.children[0]._state.parentNode; // TODO this is highly confusing node._block.first = node.children[0]._state.parentNode; // TODO this is highly confusing
@ -112,25 +115,25 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
block.builders.create.addBlock( deindent` block.builders.create.addBlock( deindent`
var ${lookup} = Object.create( null ); var ${lookup} = Object.create( null );
var head; var ${head};
var last; var ${last};
for ( var ${i} = 0; ${i} < ${each_block_value}.length; ${i} += 1 ) { for ( var ${i} = 0; ${i} < ${each_block_value}.length; ${i} += 1 ) {
var ${key} = ${each_block_value}[${i}].${node.key}; var ${key} = ${each_block_value}[${i}].${node.key};
var ${iteration} = ${lookup}[${key}] = ${create_each_block}( ${params}, ${each_block_value}, ${each_block_value}[${i}], ${i}, ${block.component}, ${key} ); var ${iteration} = ${lookup}[${key}] = ${create_each_block}( ${params}, ${each_block_value}, ${each_block_value}[${i}], ${i}, ${block.component}, ${key} );
${state.parentNode && `${iteration}.${mountOrIntro}( ${state.parentNode}, null );`} ${state.parentNode && `${iteration}.${mountOrIntro}( ${state.parentNode}, null );`}
if ( last ) last.next = ${iteration}; if ( ${last} ) ${last}.next = ${iteration};
${iteration}.last = last; ${iteration}.last = ${last};
last = ${iteration}; ${last} = ${iteration};
if ( ${i} === 0 ) head = ${iteration}; if ( ${i} === 0 ) ${head} = ${iteration};
} }
` ); ` );
if ( !state.parentNode ) { if ( !state.parentNode ) {
block.builders.mount.addBlock( deindent` block.builders.mount.addBlock( deindent`
var ${iteration} = head; var ${iteration} = ${head};
while ( ${iteration} ) { while ( ${iteration} ) {
${iteration}.${mountOrIntro}( ${block.target}, null ); ${iteration}.${mountOrIntro}( ${block.target}, null );
${iteration} = ${iteration}.next; ${iteration} = ${iteration}.next;
@ -156,9 +159,9 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
` ); ` );
destroy = deindent` destroy = deindent`
while ( expected ) { while ( ${expected} ) {
${fn}( expected ); ${fn}( ${expected} );
expected = expected.next; ${expected} = ${expected}.next;
} }
for ( ${i} = 0; ${i} < discard_pile.length; ${i} += 1 ) { for ( ${i} = 0; ${i} < discard_pile.length; ${i} += 1 ) {
@ -179,9 +182,9 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
` ); ` );
destroy = deindent` destroy = deindent`
while ( expected ) { while ( ${expected} ) {
${fn}( expected ); ${fn}( ${expected} );
expected = expected.next; ${expected} = ${expected}.next;
} }
for ( ${i} = 0; ${i} < discard_pile.length; ${i} += 1 ) { for ( ${i} = 0; ${i} < discard_pile.length; ${i} += 1 ) {
@ -196,8 +199,8 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
block.builders.update.addBlock( deindent` block.builders.update.addBlock( deindent`
var ${each_block_value} = ${snippet}; var ${each_block_value} = ${snippet};
var expected = head; var ${expected} = ${head};
var last; var ${last};
var discard_pile = []; var discard_pile = [];
@ -207,31 +210,31 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
${dynamic && `if ( ${iteration} ) ${iteration}.update( changed, ${params}, ${each_block_value}, ${each_block_value}[${i}], ${i} );`} ${dynamic && `if ( ${iteration} ) ${iteration}.update( changed, ${params}, ${each_block_value}, ${each_block_value}[${i}], ${i} );`}
if ( expected ) { if ( ${expected} ) {
if ( ${key} === expected.key ) { if ( ${key} === ${expected}.key ) {
expected = expected.next; ${expected} = ${expected}.next;
} else { } else {
if ( ${iteration} ) { if ( ${iteration} ) {
// probably a deletion // probably a deletion
do { do {
expected.discard = true; ${expected}.discard = true;
discard_pile.push( expected ); discard_pile.push( ${expected} );
expected = expected.next; ${expected} = ${expected}.next;
} while ( expected && expected.key !== ${key} ); } while ( ${expected} && ${expected}.key !== ${key} );
expected = expected && expected.next; ${expected} = ${expected} && ${expected}.next;
${iteration}.discard = false; ${iteration}.discard = false;
${iteration}.last = last; ${iteration}.last = ${last};
${iteration}.next = expected; ${iteration}.next = ${expected};
${iteration}.mount( ${parentNode}, expected ? expected.first : ${anchor} ); ${iteration}.mount( ${parentNode}, ${expected} ? ${expected}.first : ${anchor} );
} else { } else {
// key is being inserted // key is being inserted
${iteration} = ${lookup}[${key}] = ${create_each_block}( ${params}, ${each_block_value}, ${each_block_value}[${i}], ${i}, ${block.component}, ${key} ); ${iteration} = ${lookup}[${key}] = ${create_each_block}( ${params}, ${each_block_value}, ${each_block_value}[${i}], ${i}, ${block.component}, ${key} );
${iteration}.${mountOrIntro}( ${parentNode}, expected.first ); ${iteration}.${mountOrIntro}( ${parentNode}, ${expected}.first );
if ( expected ) expected.last = ${iteration}; if ( ${expected} ) ${expected}.last = ${iteration};
${iteration}.next = expected; ${iteration}.next = ${expected};
} }
} }
} else { } else {
@ -246,21 +249,21 @@ function keyed ( generator, block, state, node, snippet, { each_block, create_ea
} }
} }
if ( last ) last.next = ${iteration}; if ( ${last} ) ${last}.next = ${iteration};
${iteration}.last = last; ${iteration}.last = ${last};
${node._block.hasIntroMethod && `${iteration}.intro( ${parentNode}, ${anchor} );`} ${node._block.hasIntroMethod && `${iteration}.intro( ${parentNode}, ${anchor} );`}
last = ${iteration}; ${last} = ${iteration};
} }
if ( last ) last.next = null; if ( ${last} ) ${last}.next = null;
${destroy} ${destroy}
head = ${lookup}[${each_block_value}[0] && ${each_block_value}[0].${node.key}]; ${head} = ${lookup}[${each_block_value}[0] && ${each_block_value}[0].${node.key}];
` ); ` );
block.builders.destroy.addBlock( deindent` block.builders.destroy.addBlock( deindent`
var ${iteration} = head; var ${iteration} = ${head};
while ( ${iteration} ) { while ( ${iteration} ) {
${iteration}.destroy( ${state.parentNode ? 'false' : 'detach'} ); ${iteration}.destroy( ${state.parentNode ? 'false' : 'detach'} );
${iteration} = ${iteration}.next; ${iteration} = ${iteration}.next;
@ -316,9 +319,10 @@ function unkeyed ( generator, block, state, node, snippet, { create_each_block,
const start = node._block.hasUpdateMethod ? '0' : `${iterations}.length`; const start = node._block.hasUpdateMethod ? '0' : `${iterations}.length`;
const outro = block.getUniqueName( 'outro' );
const destroy = node._block.hasOutroMethod ? const destroy = node._block.hasOutroMethod ?
deindent` deindent`
function outro ( i ) { function ${outro} ( i ) {
if ( ${iterations}[i] ) { if ( ${iterations}[i] ) {
${iterations}[i].outro( function () { ${iterations}[i].outro( function () {
${iterations}[i].destroy( true ); ${iterations}[i].destroy( true );
@ -327,7 +331,7 @@ function unkeyed ( generator, block, state, node, snippet, { create_each_block,
} }
} }
for ( ; ${i} < ${iterations}.length; ${i} += 1 ) outro( ${i} ); for ( ; ${i} < ${iterations}.length; ${i} += 1 ) ${outro}( ${i} );
` : ` :
deindent` deindent`
${generator.helper( 'destroyEach' )}( ${iterations}, true, ${each_block_value}.length ); ${generator.helper( 'destroyEach' )}( ${iterations}, true, ${each_block_value}.length );

Loading…
Cancel
Save