Skip to content

Commit

Permalink
conservative updates for await blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 21, 2019
1 parent fa440fd commit 393757d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/compiler/compile/render_dom/wrappers/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,35 @@ export default class AwaitBlockWrapper extends Wrapper {
conditions.push(
`(${dependencies.map(dep => `'${dep}' in changed`).join(' || ')})`
);
}

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

block.builders.update.add_line(
`${info}.ctx = ctx;`
);
block.builders.update.add_line(
`${info}.ctx = ctx;`
);

if (this.pending.block.has_update_method) {
block.builders.update.add_block(deindent`
if (${conditions.join(' && ')}) {
// nothing
} else {
${info}.block.p(changed, @assign(@assign({}, ctx), ${info}.resolved));
}
`);
if (this.pending.block.has_update_method) {
block.builders.update.add_block(deindent`
if (${conditions.join(' && ')}) {
// nothing
} else {
${info}.block.p(changed, @assign(@assign({}, ctx), ${info}.resolved));
}
`);
} else {
block.builders.update.add_block(deindent`
${conditions.join(' && ')}
`);
}
} else {
block.builders.update.add_block(deindent`
${conditions.join(' && ')}
`);
if (this.pending.block.has_update_method) {
block.builders.update.add_block(deindent`
${info}.block.p(changed, @assign(@assign({}, ctx), ${info}.resolved));
`);
}
}

if (this.pending.block.has_outro_method) {
Expand Down
16 changes: 16 additions & 0 deletions test/runtime/samples/await-conservative-update/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { sleep } from './sleep.js';

export default {
html: `
<p>loading...</p>
`,

test({ assert, component, target }) {
return sleep(50).then(() => {
assert.htmlEqual(target.innerHTML, `
<p>the answer is 42</p>
<p>count: 1</p>
`);
});
}
};
19 changes: 19 additions & 0 deletions test/runtime/samples/await-conservative-update/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
import { sleep } from './sleep.js';
let count = 0;
const get_promise = () => {
return sleep(10).then(() => {
count += 1;
return 42;
});
};
</script>

{#await get_promise()}
<p>loading...</p>
{:then value}
<p>the answer is {value}</p>
<p>count: {count}</p>
{/await}
11 changes: 11 additions & 0 deletions test/runtime/samples/await-conservative-update/sleep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export let stopped = false;

export const stop = () => stopped = true;

export const sleep = ms => new Promise(f => {
if (stopped) return;
setTimeout(() => {
if (stopped) return;
f();
}, ms);
});

0 comments on commit 393757d

Please sign in to comment.