Skip to content

Commit

Permalink
start implementing context
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 11, 2018
1 parent 173792f commit 0b39caf
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 93 deletions.
40 changes: 22 additions & 18 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,26 @@ export default class Generator {
if (name === 'event' && isEventHandler) {
// noop
} else if (contexts.has(name)) {
const contextName = contexts.get(name);
if (contextName !== name) {
// this is true for 'reserved' names like `state` and `component`,
// also destructured contexts
code.overwrite(
node.start,
node.start + name.length,
contextName,
{ storeName: true, contentOnly: false }
);

const destructuredName = contextName.replace(/\[\d+\]/, '');
if (destructuredName !== contextName) {
// so that hoisting the context works correctly
usedContexts.add(destructuredName);
}
}

// const contextName = contexts.get(name);
// if (contextName !== name) {
// // this is true for 'reserved' names like `state` and `component`,
// // also destructured contexts
// code.overwrite(
// node.start,
// node.start + name.length,
// contextName,
// { storeName: true, contentOnly: false }
// );

// const destructuredName = contextName.replace(/\[\d+\]/, '');
// if (destructuredName !== contextName) {
// // so that hoisting the context works correctly
// usedContexts.add(destructuredName);
// }
// }

// TODO filthy, temporary hack
if (self.constructor.name === 'DomGenerator') code.prependRight(node.start, `state.`);
usedContexts.add(name);
} else if (helpers.has(name)) {
let object = node;
Expand All @@ -268,6 +270,8 @@ export default class Generator {
const alias = self.templateVars.get(`helpers-${name}`);
if (alias !== name) code.overwrite(object.start, object.end, alias);
} else if (indexes.has(name)) {
if (self.constructor.name === 'DomGenerator') code.prependRight(node.start, `state.`);

const context = indexes.get(name);
usedContexts.add(context); // TODO is this right?
usedIndexes.add(name);
Expand Down
10 changes: 4 additions & 6 deletions src/generators/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface BlockOptions {
contextTypes?: Map<string, string>;
indexes?: Map<string, string>;
changeableIndexes?: Map<string, boolean>;
params?: string[];
indexNames?: Map<string, string>;
listNames?: Map<string, string>;
indexName?: string;
Expand All @@ -41,7 +40,6 @@ export default class Block {
indexes: Map<string, string>;
changeableIndexes: Map<string, boolean>;
dependencies: Set<string>;
params: string[];
indexNames: Map<string, string>;
listNames: Map<string, string>;
indexName: string;
Expand Down Expand Up @@ -90,10 +88,10 @@ export default class Block {
this.changeableIndexes = options.changeableIndexes;
this.dependencies = new Set();

this.params = options.params;
this.indexNames = options.indexNames;
this.listNames = options.listNames;

this.indexName = options.indexName;
this.listName = options.listName;

this.builders = {
Expand All @@ -116,7 +114,7 @@ export default class Block {

this.aliases = new Map();
this.variables = new Map();
this.getUniqueName = this.generator.getUniqueNameMaker(options.params);
this.getUniqueName = this.generator.getUniqueNameMaker([]); // TODO this is wrong... we probably don't need this any more

this.hasUpdateMethod = false; // determined later
}
Expand Down Expand Up @@ -254,7 +252,7 @@ export default class Block {
properties.addBlock(`p: @noop,`);
} else {
properties.addBlock(deindent`
p: function update(changed, ${this.params.join(', ')}) {
p: function update(changed, state) {
${this.builders.update}
},
`);
Expand Down Expand Up @@ -328,7 +326,7 @@ export default class Block {

return deindent`
${this.comment && `// ${escape(this.comment)}`}
function ${this.name}(${this.params.join(', ')}, #component${this.key ? `, ${localKey}` : ''}) {
function ${this.name}(#component, state${this.key ? `, ${localKey}` : ''}) {
${this.variables.size > 0 &&
`var ${Array.from(this.variables.keys())
.map(key => {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class DomGenerator extends Generator {
}

getUniqueNameMaker(params: string[]) {
const localUsedNames = new Set(params);
const localUsedNames = new Set(params); // TODO is this ever called with params?

function add(name: string) {
localUsedNames.add(name);
Expand Down Expand Up @@ -257,7 +257,7 @@ export default function dom(
${generator.slots.size && `this.slots = {};`}
this._fragment = @create_main_fragment(this._state, this);
this._fragment = @create_main_fragment(this, this._state);
${(templateProperties.oncreate) && deindent`
this.root._oncreate.push(_oncreate);
Expand Down
23 changes: 10 additions & 13 deletions src/generators/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export default class AwaitBlock extends Node {
child.block = block.child({
comment: createDebuggingComment(child, this.generator),
name: this.generator.getUniqueName(`create_${status}_block`),
params: block.params.concat(context),
context,
contexts,
contextTypes
Expand Down Expand Up @@ -75,8 +74,6 @@ export default class AwaitBlock extends Node {
const anchor = this.getOrCreateAnchor(block, parentNode, parentNodes);
const updateMountNode = this.getUpdateMountNode(anchor);

const params = block.params.join(', ');

block.contextualise(this.expression);
const { snippet } = this.metadata;

Expand Down Expand Up @@ -106,11 +103,11 @@ export default class AwaitBlock extends Node {
// but it's probably not worth it

block.builders.init.addBlock(deindent`
function ${replace_await_block}(${token}, type, ${value}, ${params}) {
function ${replace_await_block}(${token}, type, ${value}, state) {
if (${token} !== ${await_token}) return;
var ${old_block} = ${await_block};
${await_block} = (${await_block_type} = type)(${params}, ${resolved} = ${value}, #component);
${await_block} = (${await_block_type} = type)(state, ${resolved} = ${value}, #component);
if (${old_block}) {
${old_block}.u();
Expand All @@ -122,33 +119,33 @@ export default class AwaitBlock extends Node {
}
}
function ${handle_promise}(${promise}, ${params}) {
function ${handle_promise}(${promise}, state) {
var ${token} = ${await_token} = {};
if (@isPromise(${promise})) {
${promise}.then(function(${value}) {
var state = #component.get();
${replace_await_block}(${token}, ${create_then_block}, ${value}, ${params});
${replace_await_block}(${token}, ${create_then_block}, ${value}, state);
}, function (${error}) {
var state = #component.get();
${replace_await_block}(${token}, ${create_catch_block}, ${error}, ${params});
${replace_await_block}(${token}, ${create_catch_block}, ${error}, state);
});
// if we previously had a then/catch block, destroy it
if (${await_block_type} !== ${create_pending_block}) {
${replace_await_block}(${token}, ${create_pending_block}, null, ${params});
${replace_await_block}(${token}, ${create_pending_block}, null, state);
return true;
}
} else {
${resolved} = ${promise};
if (${await_block_type} !== ${create_then_block}) {
${replace_await_block}(${token}, ${create_then_block}, ${resolved}, ${params});
${replace_await_block}(${token}, ${create_then_block}, ${resolved}, state);
return true;
}
}
}
${handle_promise}(${promise} = ${snippet}, ${params});
${handle_promise}(${promise} = ${snippet}, state);
`);

block.builders.create.addBlock(deindent`
Expand Down Expand Up @@ -177,15 +174,15 @@ export default class AwaitBlock extends Node {

conditions.push(
`${promise} !== (${promise} = ${snippet})`,
`${handle_promise}(${promise}, ${params})`
`${handle_promise}(${promise}, state)`
);

if (this.pending.block.hasUpdateMethod) {
block.builders.update.addBlock(deindent`
if (${conditions.join(' && ')}) {
// nothing
} else {
${await_block}.p(changed, ${params}, ${resolved});
${await_block}.p(changed, state, ${resolved});
}
`);
} else {
Expand Down
8 changes: 3 additions & 5 deletions src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,18 @@ export default class Component extends Node {

const anchor = this.getOrCreateAnchor(block, parentNode, parentNodes);

const params = block.params.join(', ');

block.builders.init.addBlock(deindent`
var ${switch_vars.value} = ${snippet};
function ${switch_vars.props}(${params}) {
function ${switch_vars.props}(state) {
${statements.length > 0 && statements.join('\n')}
return {
${componentInitProperties.join(',\n')}
};
}
if (${switch_vars.value}) {
var ${name} = new ${expression}(${switch_vars.props}(${params}));
var ${name} = new ${expression}(${switch_vars.props}(state));
${beforecreate}
}
Expand Down Expand Up @@ -327,7 +325,7 @@ export default class Component extends Node {
if (${name}) ${name}.destroy();
if (${switch_vars.value}) {
${name} = new ${switch_vars.value}(${switch_vars.props}(${params}));
${name} = new ${switch_vars.value}(${switch_vars.props}(state));
${name}._fragment.c();
${this.children.map(child => remount(generator, child, name))}
Expand Down
Loading

0 comments on commit 0b39caf

Please sign in to comment.