You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/src/shared/await-block.js

60 lines
1.4 KiB

import { assign, isPromise } from './utils.js';
import { transitionManager } from './transitions.js';
export function handlePromise(promise, info) {
var 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)(info.component, child_ctx);
if (info.block) {
if (info.blocks) {
info.blocks.forEach((block, i) => {
if (i !== index && block) {
transitionManager.groupOutros();
block.o(() => {
block.d(1);
info.blocks[i] = null;
});
}
});
} else {
info.block.d(1);
}
block.c();
block[block.i ? 'i' : 'm'](info.mount(), info.anchor);
info.component.root.set({}); // flush any handlers that were created
}
info.block = block;
if (info.blocks) info.blocks[index] = block;
}
if (isPromise(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 };
}
}