Skip to content

WIP: separate create from mount #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions compiler/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,34 @@ export default function generate ( parsed, source, options ) {

const generator = {
addElement ( name, renderStatement, needsIdentifier = false ) {
const needsTeardown = generator.current.localElementDepth === 0;
if ( needsIdentifier || needsTeardown ) {
const isToplevel = generator.current.localElementDepth === 0;
if ( needsIdentifier || isToplevel ) {
generator.current.initStatements.push( deindent`
var ${name} = ${renderStatement};
${generator.appendToTarget( name )};
` );
generator.createMountStatement( name );
} else {
generator.current.initStatements.push( deindent`
${generator.current.target}.appendChild( ${renderStatement} );
` );
}
if ( needsTeardown ) {
if ( isToplevel ) {
generator.current.teardownStatements.push( deindent`
if ( detach ) ${name}.parentNode.removeChild( ${name} );
` );
}
},
appendToTarget ( name ) {
if ( generator.current.useAnchor && generator.current.target === 'target' ) {
return `anchor.parentNode.insertBefore( ${name}, anchor )`;

createMountStatement ( name ) {
if ( generator.current.target === 'target' ) {
generator.current.mountStatements.push( deindent`
target.insertBefore( ${name}, anchor );
` );
} else {
generator.current.initStatements.push( deindent`
${generator.current.target}.appendChild( ${name} );
` );
}
return `${generator.current.target}.appendChild( ${name} )`;
},

addRenderer ( fragment ) {
Expand All @@ -45,10 +51,17 @@ export default function generate ( parsed, source, options ) {
}

renderers.push( deindent`
function ${fragment.name} ( ${fragment.params}, component, target${fragment.useAnchor ? ', anchor' : ''} ) {
function ${fragment.name} ( ${fragment.params}, component ) {
var target, anchor;
${fragment.initStatements.join( '\n\n' )}

return {
mount: function ( _target, _anchor ) {
target = _target;
anchor = _anchor;
${fragment.mountStatements.join( '\n\n' )}
},

update: function ( changed, ${fragment.params} ) {
${fragment.updateStatements.join( '\n\n' )}
},
Expand Down Expand Up @@ -236,6 +249,7 @@ export default function generate ( parsed, source, options ) {
localElementDepth: 0,

initStatements: [],
mountStatements: [],
updateStatements: [],
teardownStatements: [],

Expand Down Expand Up @@ -364,13 +378,17 @@ export default function generate ( parsed, source, options ) {
if ( generator.hasComplexBindings ) {
initStatements.push( deindent`
this.__bindings = [];
var mainFragment = renderMainFragment( state, this, options.target );
var mainFragment = renderMainFragment( state, this );
if ( options.target ) this.mount( options.target );
while ( this.__bindings.length ) this.__bindings.pop()();
` );

setStatements.push( `while ( this.__bindings.length ) this.__bindings.pop()();` );
} else {
initStatements.push( `var mainFragment = renderMainFragment( state, this, options.target );` );
initStatements.push( deindent`
var mainFragment = renderMainFragment( state, this );
if ( options.target ) this.mount( options.target );
` );
}

if ( generator.hasComponents ) {
Expand Down Expand Up @@ -449,6 +467,10 @@ export default function generate ( parsed, source, options ) {
${setStatements.join( '\n\n' )}
};

this.mount = function mount ( target, anchor ) {
mainFragment.mount( target, anchor );
}

this.observe = function ( key, callback, options ) {
var group = ( options && options.defer ) ? observers.deferred : observers.immediate;

Expand Down
39 changes: 24 additions & 15 deletions compiler/generate/visitors/EachBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ export default {
const i = generator.counters.each++;
const name = `eachBlock_${i}`;
const anchor = `${name}_anchor`;
const iterations = `${name}_iterations`;
const renderer = `renderEachBlock_${i}`;

const listName = `${name}_value`;

const isToplevel = generator.current.localElementDepth === 0;

generator.addSourcemapLocations( node.expression );

const { dependencies, snippet } = generator.contextualise( node.expression );
Expand All @@ -18,39 +21,44 @@ export default {

generator.current.initStatements.push( deindent`
var ${name}_value = ${snippet};
var ${name}_fragment = document.createDocumentFragment();
var ${name}_iterations = [];
var ${iterations} = [];

for ( var i = 0; i < ${name}_value.length; i += 1 ) {
${name}_iterations[i] = ${renderer}( ${generator.current.params}, ${listName}, ${listName}[i], i, component, ${name}_fragment );
${iterations}[i] = ${renderer}( ${generator.current.params}, ${listName}, ${listName}[i], i, component );
${!isToplevel ? `${iterations}[i].mount( ${anchor}.parentNode, ${anchor} );` : ''}
}

${anchor}.parentNode.insertBefore( ${name}_fragment, ${anchor} );
` );

if ( isToplevel ) {
generator.current.mountStatements.push( deindent`
for ( var i = 0; i < ${iterations}.length; i += 1 ) {
${iterations}[i].mount( ${anchor}.parentNode, ${anchor} );
}
` );
}

generator.current.updateStatements.push( deindent`
var ${name}_value = ${snippet};

for ( var i = 0; i < ${name}_value.length; i += 1 ) {
if ( !${name}_iterations[i] ) {
${name}_iterations[i] = ${renderer}( ${generator.current.params}, ${listName}, ${listName}[i], i, component, ${name}_fragment );
if ( !${iterations}[i] ) {
${iterations}[i] = ${renderer}( ${generator.current.params}, ${listName}, ${listName}[i], i, component );
${iterations}[i].mount( ${anchor}.parentNode, ${anchor} );
} else {
${name}_iterations[i].update( changed, ${generator.current.params}, ${listName}, ${listName}[i], i );
${iterations}[i].update( changed, ${generator.current.params}, ${listName}, ${listName}[i], i );
}
}

for ( var i = ${name}_value.length; i < ${name}_iterations.length; i += 1 ) {
${name}_iterations[i].teardown( true );
for ( var i = ${name}_value.length; i < ${iterations}.length; i += 1 ) {
${iterations}[i].teardown( true );
}

${anchor}.parentNode.insertBefore( ${name}_fragment, ${anchor} );
${name}_iterations.length = ${listName}.length;
${iterations}.length = ${listName}.length;
` );

const needsTeardown = generator.current.localElementDepth === 0;
generator.current.teardownStatements.push( deindent`
for ( var i = 0; i < ${name}_iterations.length; i += 1 ) {
${name}_iterations[i].teardown( ${needsTeardown ? 'detach' : 'false'} );
for ( var i = 0; i < ${iterations}.length; i += 1 ) {
${iterations}[i].teardown( ${isToplevel ? 'detach' : 'false'} );
}
` );

Expand Down Expand Up @@ -88,6 +96,7 @@ export default {
params,

initStatements: [],
mountStatements: [],
updateStatements: [ Object.keys( contexts ).map( contextName => {
const listName = listNames[ contextName ];
const indexName = indexNames[ contextName ];
Expand Down
12 changes: 8 additions & 4 deletions compiler/generate/visitors/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
allUsedContexts: new Set(),

init: [],
mount: [],
update: [],
teardown: []
};
Expand Down Expand Up @@ -57,19 +58,22 @@ export default {
${statements.join( '\n\n' )}

var ${name} = new template.components.${node.name}({
target: ${generator.current.target},
target: ${!shouldDetach ? generator.current.target: 'null'},
parent: component,
data: ${name}_initialData
});
` );
} else {
local.init.unshift( deindent`
var ${name} = new template.components.${node.name}({
target: ${generator.current.target},
target: ${!shouldDetach ? generator.current.target: 'null'},
parent: component
});
` );
}
if ( shouldDetach ) {
local.mount.unshift( `${name}.mount( target, anchor );` );
}

if ( local.dynamicAttributes.length ) {
const updates = local.dynamicAttributes.map( attribute => {
Expand Down Expand Up @@ -146,6 +150,7 @@ export default {

generator.current.initStatements.push( local.init.join( '\n' ) );
if ( local.update.length ) generator.current.updateStatements.push( local.update.join( '\n' ) );
if ( local.mount.length ) generator.current.mountStatements.push( local.mount.join( '\n' ) );
generator.current.teardownStatements.push( local.teardown.join( '\n' ) );

generator.push({
Expand All @@ -166,7 +171,6 @@ export default {

if ( isComponent ) return;

generator.current.initStatements.push(
generator.appendToTarget( name ) );
generator.createMountStatement( name );
}
};
22 changes: 17 additions & 5 deletions compiler/generate/visitors/IfBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ function generateBlock ( generator, node, name ) {
localElementDepth: 0,

initStatements: [],
mountStatements: [],
updateStatements: [],
teardownStatements: [],

counter: counter()
});
node.children.forEach( generator.visit );
//generator.visit( node.children );
generator.addRenderer( generator.current );
generator.pop();
// unset the children, to avoid them being visited again
node.children = [];
}

function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
generator.addSourcemapLocations( node.expression );
const name = `${_name}_${i}`;
Expand Down Expand Up @@ -54,12 +55,13 @@ export default {
enter ( generator, node ) {
const i = generator.counters.if++;

const { params, target } = generator.current;
const { params } = generator.current;
const name = `ifBlock_${i}`;
const anchor = `${name}_anchor`;
const getBlock = `getBlock_${i}`;
const currentBlock = `currentBlock_${i}`;

const isToplevel = generator.current.localElementDepth === 0;
const conditionsAndBlocks = getConditionsAndBlocks( generator, node, `renderIfBlock_${i}` );

generator.addElement( anchor, `document.createComment( ${JSON.stringify( `#if ${generator.source.slice( node.expression.start, node.expression.end )}` )} )`, true );
Expand All @@ -72,20 +74,30 @@ export default {
}

var ${currentBlock} = ${getBlock}( ${params} );
var ${name} = ${currentBlock} && ${currentBlock}( ${params}, component, ${target}, ${anchor} );
var ${name} = ${currentBlock} && ${currentBlock}( ${params}, component );
` );

const mountStatement = `if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );`;
if ( isToplevel ) {
generator.current.mountStatements.push( mountStatement );
} else {
generator.current.initStatements.push( mountStatement );
}

generator.current.updateStatements.push( deindent`
var _${currentBlock} = ${currentBlock};
${currentBlock} = ${getBlock}( ${params} );
if ( _${currentBlock} === ${currentBlock} && ${name}) {
${name}.update( changed, ${params} );
} else {
if ( ${name} ) ${name}.teardown( true );
${name} = ${currentBlock} && ${currentBlock}( ${params}, component, ${target}, ${anchor} );
${name} = ${currentBlock} && ${currentBlock}( ${params}, component );
if ( ${name} ) ${name}.mount( ${anchor}.parentNode, ${anchor} );
}
` );

generator.current.teardownStatements.push( `if ( ${name} ) ${name}.teardown( detach );` );
generator.current.teardownStatements.push( deindent`
if ( ${name} ) ${name}.teardown( ${isToplevel ? 'detach' : 'false'} );
` );
}
};
33 changes: 33 additions & 0 deletions test/compiler/each-block-random-permute/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const VALUES = Array.from( 'abcdefghijklmnopqrstuvwxyz' );

function permute () {
const values = VALUES.slice();
const number = Math.floor(Math.random() * VALUES.length);
const permuted = [];
for (let i = 0; i < number; i++) {
permuted.push( ...values.splice( Math.floor( Math.random() * ( number - i ) ), 1 ) );
}

return {
data: permuted,
expected: permuted.length ? `(${permuted.join(')(')})` : ''
};
}

let step = permute();

export default {
data: {
values: step.data
},

html: step.expected,

test ( assert, component, target ) {
for (let i = 0; i < 100; i++) {
step = permute();
component.set({ values: step.data });
assert.htmlEqual( target.innerHTML, step.expected );
}
}
};
3 changes: 3 additions & 0 deletions test/compiler/each-block-random-permute/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each values as value}}
({{value}})
{{/each}}