From 92c9993d9ed777633e33b96e20eba19ca451f73b Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sun, 6 Oct 2019 10:20:23 +0800 Subject: [PATCH] fix reactive not updated when handle promise --- src/runtime/internal/await_block.ts | 8 ++++++- .../_config.js | 13 ++++++++++++ .../main.svelte | 21 +++++++++++++++++++ .../promise.js | 7 +++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/await-set-simultaneous-reactive/_config.js create mode 100644 test/runtime/samples/await-set-simultaneous-reactive/main.svelte create mode 100644 test/runtime/samples/await-set-simultaneous-reactive/promise.js diff --git a/src/runtime/internal/await_block.ts b/src/runtime/internal/await_block.ts index 4037caa98e..0bb7b9273f 100644 --- a/src/runtime/internal/await_block.ts +++ b/src/runtime/internal/await_block.ts @@ -14,6 +14,8 @@ export function handle_promise(promise, info) { const child_ctx = assign(assign({}, info.ctx), info.resolved); const block = type && (info.current = type)(child_ctx); + let needFlush = false; + if (info.block) { if (info.blocks) { info.blocks.forEach((block, i) => { @@ -33,11 +35,15 @@ export function handle_promise(promise, info) { transition_in(block, 1); block.m(info.mount(), info.anchor); - flush(); + needFlush = true; } info.block = block; if (info.blocks) info.blocks[index] = block; + + if (needFlush) { + flush(); + } } if (is_promise(promise)) { diff --git a/test/runtime/samples/await-set-simultaneous-reactive/_config.js b/test/runtime/samples/await-set-simultaneous-reactive/_config.js new file mode 100644 index 0000000000..a591bf424f --- /dev/null +++ b/test/runtime/samples/await-set-simultaneous-reactive/_config.js @@ -0,0 +1,13 @@ +export default { + html: `

wait for it...

`, + test({ assert, component, target }) { + + return component.promise + .then(() => { + assert.htmlEqual(target.innerHTML, ` +

the answer is 42!

+

the answer100 is 4200!

+ `); + }); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/await-set-simultaneous-reactive/main.svelte b/test/runtime/samples/await-set-simultaneous-reactive/main.svelte new file mode 100644 index 0000000000..c1bbac66be --- /dev/null +++ b/test/runtime/samples/await-set-simultaneous-reactive/main.svelte @@ -0,0 +1,21 @@ + + +{#if promise} + {#await promise} +

wait for it...

+ {:then _} +

the answer is {answer}!

+

the answer100 is {answer100}!

+ {:catch error} +

well that's odd

+ {/await} +{/if} \ No newline at end of file diff --git a/test/runtime/samples/await-set-simultaneous-reactive/promise.js b/test/runtime/samples/await-set-simultaneous-reactive/promise.js new file mode 100644 index 0000000000..1f06582c9e --- /dev/null +++ b/test/runtime/samples/await-set-simultaneous-reactive/promise.js @@ -0,0 +1,7 @@ +let _resolvePromise; + +export const resolvePromise = () => _resolvePromise(); + +export const promise = new Promise(resolve => { + _resolvePromise = resolve(); +});