From ec488fe3470c5e3f0f89e15fc5f30c2ab6c5a078 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 11 Feb 2018 16:20:29 -0500 Subject: [PATCH] dont create context for pending block --- src/generators/Generator.ts | 1 - src/generators/nodes/AwaitBlock.ts | 40 +++++++++++++++++------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index d5c779a5f4..cac7858c19 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -246,7 +246,6 @@ export default class Generator { if (contextName !== name) { // this is true for 'reserved' names like `state` and `component`, // also destructured contexts - code.overwrite( node.start, node.start + name.length, diff --git a/src/generators/nodes/AwaitBlock.ts b/src/generators/nodes/AwaitBlock.ts index 168109614f..8cfa0a49e4 100644 --- a/src/generators/nodes/AwaitBlock.ts +++ b/src/generators/nodes/AwaitBlock.ts @@ -35,21 +35,19 @@ export default class AwaitBlock extends Node { ].forEach(([status, arg]) => { const child = this[status]; - const context = arg || '_'; - const contexts = new Map(block.contexts); - contexts.set(arg, context); - - const contextTypes = new Map(block.contextTypes); - contextTypes.set(arg, status); - child.block = block.child({ comment: createDebuggingComment(child, this.generator), name: this.generator.getUniqueName(`create_${status}_block`), - context, - contexts, - contextTypes + contexts: new Map(block.contexts), + contextTypes: new Map(block.contextTypes) }); + if (arg) { + child.block.context = arg; + child.block.contexts.set(arg, arg); // TODO should be using getUniqueName + child.block.contextTypes.set(arg, status); + } + child.initChildren(child.block, stripWhitespace, nextSibling); this.generator.blocks.push(child.block); @@ -107,7 +105,7 @@ export default class AwaitBlock extends Node { if (${token} !== ${await_token}) return; var ${old_block} = ${await_block}; - ${await_block} = (${await_block_type} = type)(#component, state); + ${await_block} = type && (${await_block_type} = type)(#component, state); if (${old_block}) { ${old_block}.u(); @@ -124,13 +122,21 @@ export default class AwaitBlock extends Node { if (@isPromise(${promise})) { ${promise}.then(function(${value}) { - var state = #component.get(); - ${resolved} = { ${this.then.block.context}: ${value} }; - ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved})); + ${this.then.block.context ? deindent` + var state = #component.get(); + ${resolved} = { ${this.then.block.context}: ${value} }; + ${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved})); + ` : deindent` + ${replace_await_block}(${token}, null, null); + `} }, function (${error}) { - var state = #component.get(); - ${resolved} = { ${this.catch.block.context}: ${error} }; - ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved})); + ${this.catch.block.context ? deindent` + var state = #component.get(); + ${resolved} = { ${this.catch.block.context}: ${error} }; + ${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved})); + ` : deindent` + ${replace_await_block}(${token}, null, null); + `} }); // if we previously had a then/catch block, destroy it