Home / File/ render-context.js — svelte Source File

render-context.js — svelte Source File

Architecture documentation for render-context.js, a javascript file in the svelte codebase. 4 imports, 2 dependents.

File javascript ServerRuntime Serialization 4 imports 2 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  006232e6_7197_38b5_bd13_0b28c51c2ac8["render-context.js"]
  cb946435_ce66_d1e8_6bee_287bdb07e7c5["utils.js"]
  006232e6_7197_38b5_bd13_0b28c51c2ac8 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  ca823eda_572f_96a7_a6c1_3275230578c1["deferred"]
  006232e6_7197_38b5_bd13_0b28c51c2ac8 --> ca823eda_572f_96a7_a6c1_3275230578c1
  fa007f2b_f437_c5ef_5c2d_ea8b5902500f["noop"]
  006232e6_7197_38b5_bd13_0b28c51c2ac8 --> fa007f2b_f437_c5ef_5c2d_ea8b5902500f
  aa72dc21_2f0c_e44b_b27f_dee45869de27["errors.js"]
  006232e6_7197_38b5_bd13_0b28c51c2ac8 --> aa72dc21_2f0c_e44b_b27f_dee45869de27
  2d51c6c9_d3dc_2c19_12a2_18adb2ef1c08["hydratable.js"]
  2d51c6c9_d3dc_2c19_12a2_18adb2ef1c08 --> 006232e6_7197_38b5_bd13_0b28c51c2ac8
  25166256_49ce_81f2_0877_fdbc689bed91["renderer.js"]
  25166256_49ce_81f2_0877_fdbc689bed91 --> 006232e6_7197_38b5_bd13_0b28c51c2ac8
  style 006232e6_7197_38b5_bd13_0b28c51c2ac8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// @ts-ignore -- we don't include node types in the production build
/** @import { AsyncLocalStorage } from 'node:async_hooks' */
/** @import { RenderContext } from '#server' */

import { deferred, noop } from '../shared/utils.js';
import * as e from './errors.js';

/** @type {Promise<void> | null} */
let current_render = null;

/** @type {RenderContext | null} */
let context = null;

/** @returns {RenderContext} */
export function get_render_context() {
	const store = context ?? als?.getStore();

	if (!store) {
		e.server_context_required();
	}

	return store;
}

/**
 * @template T
 * @param {() => Promise<T>} fn
 * @returns {Promise<T>}
 */
export async function with_render_context(fn) {
	context = {
		hydratable: {
			lookup: new Map(),
			comparisons: [],
			unresolved_promises: new Map()
		}
	};

	if (in_webcontainer()) {
		const { promise, resolve } = deferred();
		const previous_render = current_render;
		current_render = promise;
		await previous_render;
		return fn().finally(resolve);
	}

	try {
		if (als === null) {
			e.async_local_storage_unavailable();
		}
		return als.run(context, fn);
	} finally {
		context = null;
	}
}

/** @type {AsyncLocalStorage<RenderContext | null> | null} */
let als = null;
/** @type {Promise<void> | null} */
let als_import = null;

/**
 *
 * @returns {Promise<void>}
 */
export function init_render_context() {
	// It's important the right side of this assignment can run a maximum of one time
	// otherwise it's possible for a very, very well-timed race condition to assign to `als`
	// at the beginning of a render, and then another render to assign to it again, which causes
	// the first render's second half to use a new instance of `als` which doesn't have its
	// context anymore.
	// @ts-ignore -- we don't include node types in the production build
	als_import ??= import('node:async_hooks')
		.then((hooks) => {
			als = new hooks.AsyncLocalStorage();
		})
		.then(noop, noop);
	return als_import;
}

// this has to be a function because rollup won't treeshake it if it's a constant
function in_webcontainer() {
	// @ts-ignore -- this will fail when we run typecheck because we exclude node types
	// eslint-disable-next-line n/prefer-global/process
	return !!globalThis.process?.versions?.webcontainer;
}

Domain

Subdomains

Frequently Asked Questions

What does render-context.js do?
render-context.js is a source file in the svelte codebase, written in javascript. It belongs to the ServerRuntime domain, Serialization subdomain.
What functions are defined in render-context.js?
render-context.js defines 4 function(s): get_render_context, in_webcontainer, init_render_context, with_render_context.
What does render-context.js depend on?
render-context.js imports 4 module(s): deferred, errors.js, noop, utils.js.
What files import render-context.js?
render-context.js is imported by 2 file(s): hydratable.js, renderer.js.
Where is render-context.js in the architecture?
render-context.js is located at packages/svelte/src/internal/server/render-context.js (domain: ServerRuntime, subdomain: Serialization, directory: packages/svelte/src/internal/server).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free