Format src/runtime/internal/await_block.js

pull/8569/head
S. Elliott Johnson 3 years ago
parent 5a93b97f5f
commit d5761c85c5

@ -1,102 +1,102 @@
import { is_promise } from './utils'; import { is_promise } from './utils.js';
import { check_outros, group_outros, transition_in, transition_out } from './transitions'; import { check_outros, group_outros, transition_in, transition_out } from './transitions.js';
import { flush } from './scheduler'; import { flush } from './scheduler.js';
import { get_current_component, set_current_component } from './lifecycle'; import { get_current_component, set_current_component } from './lifecycle.js';
/** @param {Promise<T>} promise
/**
* @param {Promise<T>} promise
* @param {PromiseInfo<T>} info * @param {PromiseInfo<T>} info
* @returns {boolean} * @returns {boolean}
*/ */
export function handle_promise(promise, info) { export function handle_promise(promise, info) {
const token = (info.token = {}); const token = (info.token = {});
/** @param {FragmentFactory} type /**
* @param {0 | 1 | 2} index * @param {FragmentFactory} type
* @param {number} key * @param {0 | 1 | 2} index
* @returns {void} * @param {number} key
*/ * @returns {void}
function update(type, index, key, value) { */
if (info.token !== token) function update(type, index, key, value) {
return; if (info.token !== token) return;
info.resolved = value; info.resolved = value;
let child_ctx = info.ctx; let child_ctx = info.ctx;
if (key !== undefined) { if (key !== undefined) {
child_ctx = child_ctx.slice(); child_ctx = child_ctx.slice();
child_ctx[key] = value; child_ctx[key] = value;
} }
const block = type && (info.current = type)(child_ctx); const block = type && (info.current = type)(child_ctx);
let needs_flush = false; let needs_flush = false;
if (info.block) { if (info.block) {
if (info.blocks) { if (info.blocks) {
info.blocks.forEach((block, i) => { info.blocks.forEach((block, i) => {
if (i !== index && block) { if (i !== index && block) {
group_outros(); group_outros();
transition_out(block, 1, 1, () => { transition_out(block, 1, 1, () => {
if (info.blocks[i] === block) { if (info.blocks[i] === block) {
info.blocks[i] = null; info.blocks[i] = null;
} }
}); });
check_outros(); check_outros();
} }
}); });
} } else {
else { info.block.d(1);
info.block.d(1); }
} block.c();
block.c(); transition_in(block, 1);
transition_in(block, 1); block.m(info.mount(), info.anchor);
block.m(info.mount(), info.anchor); needs_flush = true;
needs_flush = true; }
} info.block = block;
info.block = block; if (info.blocks) info.blocks[index] = block;
if (info.blocks) if (needs_flush) {
info.blocks[index] = block; flush();
if (needs_flush) { }
flush(); }
} if (is_promise(promise)) {
} const current_component = get_current_component();
if (is_promise(promise)) { promise.then(
const current_component = get_current_component(); (value) => {
promise.then((value) => { set_current_component(current_component);
set_current_component(current_component); update(info.then, 1, info.value, value);
update(info.then, 1, info.value, value); set_current_component(null);
set_current_component(null); },
}, (error) => { (error) => {
set_current_component(current_component); set_current_component(current_component);
update(info.catch, 2, info.error, error); update(info.catch, 2, info.error, error);
set_current_component(null); set_current_component(null);
if (!info.hasCatch) { if (!info.hasCatch) {
throw error; throw error;
} }
}); }
// if we previously had a then/catch block, destroy it );
if (info.current !== info.pending) { // if we previously had a then/catch block, destroy it
update(info.pending, 0); if (info.current !== info.pending) {
return true; update(info.pending, 0);
} return true;
} }
else { } else {
if (info.current !== info.then) { if (info.current !== info.then) {
update(info.then, 1, info.value, promise); update(info.then, 1, info.value, promise);
return true; return true;
} }
info.resolved = promise; info.resolved = promise;
} }
} }
/** @returns {void} */ /** @returns {void} */
export function update_await_block_branch(info, ctx, dirty) { export function update_await_block_branch(info, ctx, dirty) {
const child_ctx = ctx.slice(); const child_ctx = ctx.slice();
const { resolved } = info; const { resolved } = info;
if (info.current === info.then) { if (info.current === info.then) {
child_ctx[info.value] = resolved; child_ctx[info.value] = resolved;
} }
if (info.current === info.catch) { if (info.current === info.catch) {
child_ctx[info.error] = resolved; child_ctx[info.error] = resolved;
} }
info.block.p(child_ctx, dirty); info.block.p(child_ctx, dirty);
} }
/** @typedef {Object} PromiseInfo /** @typedef {Object} PromiseInfo
* @property {null|any} ctx * @property {null|any} ctx
* @property {{}} token * @property {{}} token
@ -111,5 +111,5 @@ export function update_await_block_branch(info, ctx, dirty) {
* @property {Fragment|null} block * @property {Fragment|null} block
* @property {[null|Fragment,null|Fragment,null|Fragment]} blocks * @property {[null|Fragment,null|Fragment,null|Fragment]} blocks
* @property {()=>HTMLElement} mount * @property {()=>HTMLElement} mount
* @property {HTMLElement} anchor * @property {HTMLElement} anchor
*/ */

Loading…
Cancel
Save