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

100% Statements 154/154
88.57% Branches 31/35
100% Functions 3/3
100% Lines 147/147

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 1482x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 158x 158x 286x 286x 286x 256x 256x 256x 256x 256x 160x 156x 160x 156x 160x 148x 148x 256x 256x 78x 78x 78x 78x 72x 72x 78x 256x 256x 256x 162x 162x 162x 162x 162x 16x 16x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 162x 162x 34x 162x 128x 128x 128x 128x 162x 256x 256x 256x 286x 30x 30x 30x 30x 16x 2x 2x 16x 16x 16x 30x 286x 286x 286x 286x 158x 158x  
import { is_promise, noop } from '../../../shared/utils.js';
import {
	current_component_context,
	current_effect,
	current_reaction,
	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';
import { queue_micro_task } from '../task.js';
import { hydrating } from '../hydration.js';
 
/**
 * @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);
			var resolved = false;
 
			promise.then(
				(value) => {
					if (promise !== input) return;
					resolved = true;
					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);
					}
				}
			);
 
			if (pending_fn) {
				var parent_effect = current_effect;
				var parent_reaction = current_reaction;
				var show_pending = () => {
					if (resolved) return;
					if (pending_effect && (pending_effect.f & INERT) === 0) {
						destroy_effect(pending_effect);
					}
					var previous_effect = current_effect;
					var previous_reaction = current_reaction;
					try {
						set_current_effect(parent_effect);
						set_current_reaction(parent_reaction);
						pending_effect = branch(() => pending_fn(anchor));
					} finally {
						set_current_effect(previous_effect);
						set_current_reaction(previous_reaction);
					}
				};
				if (hydrating) {
					show_pending();
				} else {
					// Wait a microtask before checking if we should the pending state as
					// the promise might have resolved by the next microtask.
					queue_micro_task(show_pending);
				}
			}
 
			if (then_effect) pause_effect(then_effect);
			if (catch_effect) pause_effect(catch_effect);
		} 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));
			}
		}
 
		// Inert effects are proactively detached from the effect tree. Returning a noop
		// teardown function is an easy way to ensure that this is not discarded
		return noop;
	});
}