Skip to content

Commit c355b02

Browse files
committed
support transitions in await blocks - fixes #956
1 parent d8d9c58 commit c355b02

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

src/compile/nodes/AwaitBlock.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export default class AwaitBlock extends Node {
4242
block.addDependencies(this.expression.dependencies);
4343

4444
let isDynamic = false;
45+
let hasIntros = false;
46+
let hasOutros = false;
4547

4648
['pending', 'then', 'catch'].forEach(status => {
4749
const child = this[status];
@@ -58,11 +60,22 @@ export default class AwaitBlock extends Node {
5860
isDynamic = true;
5961
block.addDependencies(child.block.dependencies);
6062
}
63+
64+
if (child.block.hasIntroMethod) hasIntros = true;
65+
if (child.block.hasOutroMethod) hasOutros = true;
6166
});
6267

6368
this.pending.block.hasUpdateMethod = isDynamic;
6469
this.then.block.hasUpdateMethod = isDynamic;
6570
this.catch.block.hasUpdateMethod = isDynamic;
71+
72+
this.pending.block.hasIntroMethod = hasIntros;
73+
this.then.block.hasIntroMethod = hasIntros;
74+
this.catch.block.hasIntroMethod = hasIntros;
75+
76+
this.pending.block.hasOutroMethod = hasOutros;
77+
this.then.block.hasOutroMethod = hasOutros;
78+
this.catch.block.hasOutroMethod = hasOutros;
6679
}
6780

6881
build(
@@ -92,7 +105,8 @@ export default class AwaitBlock extends Node {
92105
this.then.block.name && `then: ${this.then.block.name}`,
93106
this.catch.block.name && `catch: ${this.catch.block.name}`,
94107
this.then.block.name && `value: '${this.value}'`,
95-
this.catch.block.name && `error: '${this.error}'`
108+
this.catch.block.name && `error: '${this.error}'`,
109+
this.pending.block.hasOutroMethod && `blocks: Array(3)`
96110
].filter(Boolean);
97111

98112
block.builders.init.addBlock(deindent`
@@ -123,7 +137,7 @@ export default class AwaitBlock extends Node {
123137
const anchorNode = parentNode ? 'null' : 'anchor';
124138

125139
block.builders.mount.addBlock(deindent`
126-
${info}.block.m(${initialMountNode}, ${info}.anchor = ${anchorNode});
140+
${info}.block.${this.pending.block.hasIntroMethod ? 'i' : 'm'}(${initialMountNode}, ${info}.anchor = ${anchorNode});
127141
${info}.mount = () => ${updateMountNode};
128142
`);
129143

src/shared/await-block.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assign, isPromise } from './utils.js';
33
export function handlePromise(promise, info) {
44
var token = info.token = {};
55

6-
function update(type, key, value) {
6+
function update(type, index, key, value) {
77
if (info.token !== token) return;
88

99
info.resolved = key && { [key]: value };
@@ -12,32 +12,43 @@ export function handlePromise(promise, info) {
1212
const block = type && (info.current = type)(info.component, child_ctx);
1313

1414
if (info.block) {
15-
info.block.u();
16-
info.block.d();
15+
if (info.blocks) {
16+
info.blocks.forEach((block, i) => {
17+
if (i !== index) block.o(() => {
18+
block.u();
19+
block.d();
20+
});
21+
});
22+
} else {
23+
info.block.u();
24+
info.block.d();
25+
}
26+
1727
block.c();
18-
block.m(info.mount(), info.anchor);
28+
block[block.i ? 'i' : 'm'](info.mount(), info.anchor);
1929

20-
info.component.root.set({});
30+
info.component.root.set({}); // flush any handlers that were created
2131
}
2232

2333
info.block = block;
34+
if (info.blocks) info.blocks[index] = block;
2435
}
2536

2637
if (isPromise(promise)) {
2738
promise.then(value => {
28-
update(info.then, info.value, value);
39+
update(info.then, 1, info.value, value);
2940
}, error => {
30-
update(info.catch, info.error, error);
41+
update(info.catch, 2, info.error, error);
3142
});
3243

3344
// if we previously had a then/catch block, destroy it
3445
if (info.current !== info.pending) {
35-
update(info.pending);
46+
update(info.pending, 0);
3647
return true;
3748
}
3849
} else {
3950
if (info.current !== info.then) {
40-
update(info.then, info.value, promise);
51+
update(info.then, 1, info.value, promise);
4152
return true;
4253
}
4354

test/runtime/samples/transition-js-await-block/_config.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ let promise = new Promise((f, r) => {
77
});
88

99
export default {
10+
solo: 1,
11+
1012
data: {
1113
promise
1214
},
@@ -19,14 +21,17 @@ export default {
1921
assert.equal(p.foo, 0);
2022

2123
raf.tick(50);
22-
assert.equal(p.foo, 0);
24+
assert.equal(p.foo, 0.5);
2325

2426
fulfil(42);
25-
raf.tick(80);
26-
let ps = document.querySelectorAll('p');
27-
assert.equal(p[0].className, 'pending');
28-
assert.equal(p[1].className, 'then');
29-
assert.equal(p[0].foo, 20);
30-
assert.equal(p[1].foo, 30);
31-
},
27+
28+
return promise.then(() => {
29+
raf.tick(80);
30+
let ps = document.querySelectorAll('p');
31+
assert.equal(ps[0].className, 'pending');
32+
assert.equal(ps[1].className, 'then');
33+
assert.equal(ps[0].foo, 0.2);
34+
assert.equal(ps[1].foo, 0.3);
35+
});
36+
}
3237
};

0 commit comments

Comments
 (0)