All files / src/internal/client/dom/blocks await.js

100% Statements 122/122
86.2% Branches 25/29
100% Functions 2/2
100% Lines 117/117

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1182x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 156x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 216x 156x 156x 282x 282x 282x 252x 252x 252x 158x 16x 16x 158x 158x 158x 252x 252x 252x 252x 252x 252x 156x 156x 152x 156x 144x 144x 252x 252x 78x 78x 78x 78x 72x 72x 78x 252x 282x 30x 30x 30x 30x 16x 2x 2x 16x 16x 16x 30x 156x 156x  
import { is_promise } from '../../../shared/utils.js';
import {
	current_component_context,
	flush_sync,
	set_current_component_context,
	set_current_effect,
	set_current_reaction,
	set_dev_current_component_function
} from '../../runtime.js';
import { block, branch, destroy_effect, pause_effect } from '../../reactivity/effects.js';
import { INERT } from '../../constants.js';
import { DEV } from 'esm-env';
 
/**
 * @template V
 * @param {Comment} anchor
 * @param {(() => Promise<V>)} get_input
 * @param {null | ((anchor: Node) => void)} pending_fn
 * @param {null | ((anchor: Node, value: V) => void)} then_fn
 * @param {null | ((anchor: Node, error: unknown) => void)} catch_fn
 * @returns {void}
 */
export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {
	const component_context = current_component_context;
	/** @type {any} */
	let component_function;
	if (DEV) {
		component_function = component_context?.function ?? null;
	}
 
	/** @type {any} */
	let input;
 
	/** @type {import('#client').Effect | null} */
	let pending_effect;
 
	/** @type {import('#client').Effect | null} */
	let then_effect;
 
	/** @type {import('#client').Effect | null} */
	let catch_effect;
 
	/**
	 * @param {(anchor: Comment, value: any) => void} fn
	 * @param {any} value
	 */
	function create_effect(fn, value) {
		set_current_effect(effect);
		set_current_reaction(effect); // TODO do we need both?
		set_current_component_context(component_context);
		if (DEV) {
			set_dev_current_component_function(component_function);
		}
		var e = branch(() => fn(anchor, value));
		if (DEV) {
			set_dev_current_component_function(null);
		}
		set_current_component_context(null);
		set_current_reaction(null);
		set_current_effect(null);
 
		// without this, the DOM does not update until two ticks after the promise,
		// resolves which is unexpected behaviour (and somewhat irksome to test)
		flush_sync();
 
		return e;
	}
 
	const effect = block(() => {
		if (input === (input = get_input())) return;
 
		if (is_promise(input)) {
			const promise = /** @type {Promise<any>} */ (input);
 
			if (pending_fn) {
				if (pending_effect && (pending_effect.f & INERT) === 0) {
					destroy_effect(pending_effect);
				}
 
				pending_effect = branch(() => pending_fn(anchor));
			}
 
			if (then_effect) pause_effect(then_effect);
			if (catch_effect) pause_effect(catch_effect);
 
			promise.then(
				(value) => {
					if (promise !== input) return;
					if (pending_effect) pause_effect(pending_effect);
 
					if (then_fn) {
						then_effect = create_effect(then_fn, value);
					}
				},
				(error) => {
					if (promise !== input) return;
					if (pending_effect) pause_effect(pending_effect);
 
					if (catch_fn) {
						catch_effect = create_effect(catch_fn, error);
					}
				}
			);
		} else {
			if (pending_effect) pause_effect(pending_effect);
			if (catch_effect) pause_effect(catch_effect);
 
			if (then_fn) {
				if (then_effect) {
					destroy_effect(then_effect);
				}
 
				then_effect = branch(() => then_fn(anchor, input));
			}
		}
	});
}