-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
await-block.js
64 lines (53 loc) · 1.43 KB
/
await-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { assign, is_promise } from './utils.js';
import { check_outros, group_outros, on_outro } from './transitions.js';
import { flush } from '../internal/scheduler.js';
export function handle_promise(promise, info) {
const token = info.token = {};
function update(type, index, key, value) {
if (info.token !== token) return;
info.resolved = key && { [key]: value };
const child_ctx = assign(assign({}, info.ctx), info.resolved);
const block = type && (info.current = type)(child_ctx);
if (info.block) {
if (info.blocks) {
info.blocks.forEach((block, i) => {
if (i !== index && block) {
group_outros();
on_outro(() => {
block.d(1);
info.blocks[i] = null;
});
block.o(1);
check_outros();
}
});
} else {
info.block.d(1);
}
block.c();
if (block.i) block.i(1);
block.m(info.mount(), info.anchor);
flush();
}
info.block = block;
if (info.blocks) info.blocks[index] = block;
}
if (is_promise(promise)) {
promise.then(value => {
update(info.then, 1, info.value, value);
}, error => {
update(info.catch, 2, info.error, error);
});
// if we previously had a then/catch block, destroy it
if (info.current !== info.pending) {
update(info.pending, 0);
return true;
}
} else {
if (info.current !== info.then) {
update(info.then, 1, info.value, promise);
return true;
}
info.resolved = { [info.value]: promise };
}
}