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

98.47% Statements 194/197
84.61% Branches 44/52
100% Functions 4/4
98.41% Lines 186/189

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 1902x 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 2x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 220x 160x 160x 294x 294x 294x 264x 264x 264x 264x 264x 166x 162x 166x 162x 166x 154x 154x 148x 148x 154x 264x 264x 78x 78x 78x 78x 78x 78x 72x 72x 72x 72x 72x 78x 264x 264x 264x 170x 170x 170x 170x 164x 170x 16x 16x 164x 164x 164x 164x 164x 164x 170x 170x 170x 164x 164x 164x 170x 170x 170x 35x 170x 135x 135x 135x 135x 135x 264x 48x 37x 37x 37x 37x 37x 48x 48x 48x 37x 37x 37x 37x 37x 94x 46x 16x 16x 16x 46x       46x 294x 30x 30x 30x 30x 16x 2x 2x 16x 16x 16x 16x 30x 294x 294x 294x 294x 160x 160x  
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';
import { set, source } from '../../reactivity/sources.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;
 
	let input_source = source(undefined);
 
	let error_source = source(undefined);
 
	/** @type {import('#client').Effect | null} */
	let pending_effect;
 
	/** @type {import('#client').Effect | null} */
	let then_effect;
 
	/** @type {import('#client').Effect | null} */
	let catch_effect;
 
	let pending = false;
 
	/**
	 * @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) {
						set(input_source, value);
						if (pending || then_effect === undefined) {
							then_effect = create_effect(then_fn, input_source);
						}
					}
				},
				(error) => {
					if (promise !== input) return;
					if (pending_effect) pause_effect(pending_effect);
					if (then_effect) pause_effect(then_effect);
					resolved = true;
 
					if (catch_fn) {
						set(error_source, error);
						if (pending || catch_effect === undefined) {
							catch_effect = create_effect(catch_fn, error_source);
						}
					}
				}
			);
 
			if (pending_fn) {
				var parent_effect = current_effect;
				var parent_reaction = current_reaction;
				var show_pending = () => {
					if (resolved) return;
					pending = true;
					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));
						if (then_effect) pause_effect(then_effect);
						if (catch_effect) pause_effect(catch_effect);
					} finally {
						set_current_effect(previous_effect);
						set_current_reaction(previous_reaction);
					}
				};
 
				if (hydrating) {
					show_pending();
				} else {
					pending = false;
					// Wait a microtask before checking if we should show the pending state as
					// the promise might have resolved by the next microtask.
					queue_micro_task(show_pending);
				}
			} else if (catch_fn) {
				var show_catch = () => {
					if (resolved) return;
					pending = true;
					if (pending_effect) pause_effect(pending_effect);
					if (catch_effect) pause_effect(catch_effect);
					if (then_effect) pause_effect(then_effect);
				};
 
				if (!hydrating) {
					pending = false;
					// Wait a microtask before checking if we should show the pending state as
					// the promise might have resolved by the next microtask.
					queue_micro_task(show_catch);
				}
			} else {
				if (then_effect) {
					pending = true;
					pause_effect(then_effect);
				}
				if (catch_effect) {
					pending = true;
					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);
				}
 
				set(input_source, input);
				then_effect = branch(() => then_fn(anchor, /** @type {V} */ (input_source)));
			}
		}
 
		// 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;
	});
}