Skip to content

Commit

Permalink
reverse bad fix from yesterday - only have state object in mount if i…
Browse files Browse the repository at this point in the history
…ts a yield block
  • Loading branch information
Rich-Harris committed Jul 27, 2017
1 parent ab60726 commit fc7e104
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/generators/dom/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ const preprocessors = {
generator.components.has(node.name) || node.name === ':Self';

if (isComponent) {
node._state = getChildState(state);
node._state = getChildState(state, {
isYield: true
});
} else {
const name = block.getUniqueName(
node.name.replace(/[^a-zA-Z0-9_$]/g, '_')
Expand Down
16 changes: 5 additions & 11 deletions src/generators/dom/visitors/Element/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,8 @@ export default function visitBinding(
const { name } = getObject(attribute.value);
const tailSnippet = getTailSnippet(attribute.value);

if (state.inEachBlock === true) {
updateElement = deindent`
var ${value} = ${snippet};
`;
} else {
updateElement = deindent`
var ${value} = #component.get( '${name}' )${tailSnippet};
`;
}
updateElement += `
updateElement = deindent`
var ${value} = ${snippet};
for ( var #i = 0; #i < ${state.parentNode}.options.length; #i += 1 ) {
var ${option} = ${state.parentNode}.options[#i];
Expand Down Expand Up @@ -168,8 +160,10 @@ export default function visitBinding(
`@addListener( ${state.parentNode}, '${eventName}', ${handler} );`
);

if (node.name !== 'audio' && node.name !== 'video')
if (node.name !== 'audio' && node.name !== 'video') {
node.initialUpdate = updateElement;
node.initialUpdateNeedsStateObject = !block.contexts.has(name);
}

if (updateCondition !== null) {
// audio/video duration is read-only, it never updates
Expand Down
11 changes: 11 additions & 0 deletions src/generators/dom/visitors/Element/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ export default function visitElement(
}

if (node.initialUpdate) {
// special case — if we're in a yield block, then we may call mount
// long after the fragment is created. We may therefore need to get
// the latest `state` object
// TODO what about contexts in yield blocks? do we need to do
// `var ${contextName} = [something complicated]`?
const needsStateObject = node.initialUpdateNeedsStateObject && state.isYield;

if ( needsStateObject ) {
block.builders.mount.addLine(`var state = #component.get()`);
}

block.builders.mount.addBlock(node.initialUpdate);
}

Expand Down

0 comments on commit fc7e104

Please sign in to comment.