Skip to content

Commit

Permalink
remove _context stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 11, 2018
1 parent b94f63e commit 71f2c35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 59 deletions.
10 changes: 10 additions & 0 deletions src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ export default class Block {
this.hasUpdateMethod = true;
});

this.indexNames.forEach((alias, name) => {
// TODO only the ones that are actually used in this block...
const assignment = `${alias} = state.${alias}`; // TODO this is wrong!!!

initializers.push(assignment);
updaters.push(`${assignment};`);

this.hasUpdateMethod = true;
});

// minor hack – we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());

Expand Down
67 changes: 8 additions & 59 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export default class Component extends Node {

const allContexts = new Set();
const statements: string[] = [];
const name_context = block.getUniqueName(`${name}_context`);

let name_updating: string;
let name_initial_data: string;
Expand All @@ -93,11 +92,11 @@ export default class Component extends Node {

const bindings = this.attributes
.filter(a => a.type === 'Binding')
.map(a => mungeBinding(a, block, name_context));
.map(a => mungeBinding(a, block));

const eventHandlers = this.attributes
.filter((a: Node) => a.type === 'EventHandler')
.map(a => mungeEventHandler(generator, this, a, block, name_context, allContexts));
.map(a => mungeEventHandler(generator, this, a, block, allContexts));

const ref = this.attributes.find((a: Node) => a.type === 'Ref');
if (ref) generator.usesRefs = true;
Expand Down Expand Up @@ -156,8 +155,8 @@ export default class Component extends Node {
const tail = binding.value.type === 'MemberExpression' ? getTailSnippet(binding.value) : '';

setFromChild = deindent`
var list = ${name_context}.${block.listNames.get(key)};
var index = ${name_context}.${block.indexNames.get(key)};
var list = state.${block.listNames.get(key)};
var index = ${block.indexNames.get(key)};
list[index]${tail} = childState.${binding.name};
${binding.dependencies
Expand Down Expand Up @@ -248,41 +247,6 @@ export default class Component extends Node {
}
}

// maintain component context
if (allContexts.size) {
const contexts = Array.from(allContexts);

const initialProps = contexts
.map(contextName => {
if (contextName === 'state') return `state: state`;

const listName = block.listNames.get(contextName);
const indexName = block.indexNames.get(contextName);

return `${listName}: state.${listName},\n${indexName}: state.${indexName}`;
})
.join(',\n');

const updates = contexts
.map(contextName => {
if (contextName === 'state') return `${name_context}.state = state;`;

const listName = block.listNames.get(contextName);
const indexName = block.indexNames.get(contextName);

return `${name_context}.${listName} = state.${listName};\n${name_context}.${indexName} = state.${indexName};`;
})
.join('\n');

block.builders.init.addBlock(deindent`
var ${name_context} = {
${initialProps}
};
`);

block.builders.update.addBlock(updates);
}

const isDynamicComponent = this.name === ':Component';

const switch_vars = isDynamicComponent && {
Expand Down Expand Up @@ -504,7 +468,7 @@ function mungeAttribute(attribute: Node, block: Block): Attribute {
};
}

function mungeBinding(binding: Node, block: Block, name_context: string): Binding {
function mungeBinding(binding: Node, block: Block): Binding {
const { name } = getObject(binding.value);
const { contexts } = block.contextualise(binding.value);
const { dependencies, snippet } = binding.metadata;
Expand All @@ -515,8 +479,8 @@ function mungeBinding(binding: Node, block: Block, name_context: string): Bindin
let prop;

if (contextual) {
obj = `${name_context}.${block.listNames.get(name)}`;
prop = `${name_context}.${block.indexNames.get(name)}`;
obj = `state.${block.listNames.get(name)}`;
prop = `${block.indexNames.get(name)}`;
} else if (binding.value.type === 'MemberExpression') {
prop = `[✂${binding.value.property.start}-${binding.value.property.end}✂]`;
if (!binding.value.computed) prop = `'${prop}'`;
Expand All @@ -537,7 +501,7 @@ function mungeBinding(binding: Node, block: Block, name_context: string): Bindin
};
}

function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, block: Block, name_context: string, allContexts: Set<string>) {
function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, block: Block, allContexts: Set<string>) {
let body;

if (handler.expression) {
Expand All @@ -547,30 +511,15 @@ function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, b
`${block.alias('component')}.`
);

const usedContexts: string[] = [];

handler.expression.arguments.forEach((arg: Node) => {
const { contexts } = block.contextualise(arg, null, true);

contexts.forEach(context => {
if (!~usedContexts.indexOf(context)) usedContexts.push(context);
allContexts.add(context);
});
});

// TODO hoist event handlers? can do `this.__component.method(...)`
const declarations = usedContexts.map(name => {
if (name === 'state') return `var state = ${name_context}.state;`;

const listName = block.listNames.get(name);
const indexName = block.indexNames.get(name);

return `var ${listName} = ${name_context}.${listName}, ${indexName} = ${name_context}.${indexName}, ${name} = ${listName}[${indexName}]`;
});

body = deindent`
${declarations}
[✂${handler.expression.start}-${handler.expression.end}✂];
`;
} else {
Expand Down

0 comments on commit 71f2c35

Please sign in to comment.