All files / src/compiler/phases/3-transform/client/visitors AwaitBlock.js

100% Statements 69/69
100% Branches 9/9
100% Functions 1/1
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 65 66 67 682x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 86x 86x 86x 86x 86x 86x 74x 74x 74x 74x 74x 68x 68x 68x 68x 68x 18x 18x 68x 74x 74x 74x 86x 86x 49x 49x 49x 49x 49x 45x 45x 45x 45x 45x 17x 17x 45x 49x 49x 49x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x  
/** @import { BlockStatement, Expression, Pattern } from 'estree' */
/** @import { AwaitBlock } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import * as b from '../../../../utils/builders.js';
import { create_derived_block_argument } from '../utils.js';
 
/**
 * @param {AwaitBlock} node
 * @param {ComponentContext} context
 */
export function AwaitBlock(node, context) {
	context.state.template.push('<!>');
 
	let then_block;
	let catch_block;
 
	if (node.then) {
		/** @type {Pattern[]} */
		const args = [b.id('$$anchor')];
		const block = /** @type {BlockStatement} */ (context.visit(node.then));
 
		if (node.value) {
			const argument = create_derived_block_argument(node.value, context);
 
			args.push(argument.id);
 
			if (argument.declarations !== null) {
				block.body.unshift(...argument.declarations);
			}
		}
 
		then_block = b.arrow(args, block);
	}
 
	if (node.catch) {
		/** @type {Pattern[]} */
		const args = [b.id('$$anchor')];
		const block = /** @type {BlockStatement} */ (context.visit(node.catch));
 
		if (node.error) {
			const argument = create_derived_block_argument(node.error, context);
 
			args.push(argument.id);
 
			if (argument.declarations !== null) {
				block.body.unshift(...argument.declarations);
			}
		}
 
		catch_block = b.arrow(args, block);
	}
 
	context.state.init.push(
		b.stmt(
			b.call(
				'$.await',
				context.state.node,
				b.thunk(/** @type {Expression} */ (context.visit(node.expression))),
				node.pending
					? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.pending)))
					: b.literal(null),
				then_block,
				catch_block
			)
		)
	);
}