From 56b167b44fe3bfb93cc0e542ff24a76ee1a9d14b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 25 Nov 2017 09:13:58 -0500 Subject: [PATCH] await-then-catch with non-promise --- src/generators/Generator.ts | 12 +++++++++++- src/generators/dom/visitors/AwaitBlock.ts | 8 ++++++-- .../await-then-catch-non-promise/_config.js | 19 +++++++++++++++++++ .../await-then-catch-non-promise/main.html | 7 +++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 test/runtime/samples/await-then-catch-non-promise/_config.js create mode 100644 test/runtime/samples/await-then-catch-non-promise/main.html diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 284f96b2c5..5fd82e274b 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -732,7 +732,17 @@ export default class Generator { } } - if (node.type === 'IfBlock' || node.type === 'AwaitBlock') { + if (node.type === 'AwaitBlock') { + node.metadata = contextualise(node.expression, contextDependencies, indexes); + + contextDependencies = new Map(contextDependencies); + contextDependencies.set(node.value, node.metadata.dependencies); + contextDependencies.set(node.error, node.metadata.dependencies); + + contextDependenciesStack.push(contextDependencies); + } + + if (node.type === 'IfBlock') { node.metadata = contextualise(node.expression, contextDependencies, indexes); } diff --git a/src/generators/dom/visitors/AwaitBlock.ts b/src/generators/dom/visitors/AwaitBlock.ts index fc28091e84..b4f887167c 100644 --- a/src/generators/dom/visitors/AwaitBlock.ts +++ b/src/generators/dom/visitors/AwaitBlock.ts @@ -88,9 +88,13 @@ export default function visitAwaitBlock( return true; } } else { + ${resolved} = ${promise}; if (${await_block_type} !== ${create_then_block}) { - if (${await_block}) ${await_block}.d(); - ${await_block} = (${await_block_type} = ${create_then_block})(${params}, ${resolved} = promise, #component); + if (${await_block}) { + ${await_block}.u(); + ${await_block}.d(); + } + ${await_block} = (${await_block_type} = ${create_then_block})(${params}, ${resolved}, #component); return true; } } diff --git a/test/runtime/samples/await-then-catch-non-promise/_config.js b/test/runtime/samples/await-then-catch-non-promise/_config.js new file mode 100644 index 0000000000..56b4b59f1b --- /dev/null +++ b/test/runtime/samples/await-then-catch-non-promise/_config.js @@ -0,0 +1,19 @@ +export default { + data: { + thePromise: 'not actually a promise' + }, + + html: ` +

the value is not actually a promise

+ `, + + test(assert, component, target) { + component.set({ + thePromise: 'still not a promise' + }); + + assert.htmlEqual(target.innerHTML, ` +

the value is still not a promise

+ `); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/await-then-catch-non-promise/main.html b/test/runtime/samples/await-then-catch-non-promise/main.html new file mode 100644 index 0000000000..36489b9043 --- /dev/null +++ b/test/runtime/samples/await-then-catch-non-promise/main.html @@ -0,0 +1,7 @@ +{{#await thePromise}} +

loading...

+{{then theValue}} +

the value is {{theValue}}

+{{catch theError}} +

oh no! {{theError.message}}

+{{/await}} \ No newline at end of file