Home / Function/ default.init() — astro Function Reference

default.init() — astro Function Reference

Architecture documentation for the default.init() function in index.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  67ab8f85_15f7_908f_e947_4423cb68cceb["default.init()"]
  00de791e_c3ec_5c38_2e33_352734c87e5a["index.ts"]
  67ab8f85_15f7_908f_e947_4423cb68cceb -->|defined in| 00de791e_c3ec_5c38_2e33_352734c87e5a
  style 67ab8f85_15f7_908f_e947_4423cb68cceb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/runtime/client/dev-toolbar/apps/audit/index.ts lines 33–228

	async init(canvas, eventTarget) {
		let audits: Audit[] = [];
		let auditWindow = document.createElement(
			'astro-dev-toolbar-audit-window',
		) as DevToolbarAuditListWindow;
		let hasCreatedUI = false;
		auditWindow.popover = '';

		canvas.appendChild(auditWindow);

		await run();

		let mutationDebounce: ReturnType<typeof setTimeout>;
		const observer = new MutationObserver(() => {
			// We don't want to rerun the audit lints on every single mutation, so we'll debounce it.
			if (mutationDebounce) {
				clearTimeout(mutationDebounce);
			}

			mutationDebounce = setTimeout(() => {
				settings.logger.verboseLog('Rerunning audit lints because the DOM has been updated.');

				// Even though we're ready to run the lints, we'll wait for the next idle period to do so, as it is less likely
				// to interfere with any other work the browser is doing post-mutation. For instance, the page or the user might
				// be interacting with the newly added elements, or the browser might be doing some work (layout, paint, etc.)
				if ('requestIdleCallback' in window) {
					window.requestIdleCallback(
						async () => {
							run().then(() => {
								if (showState) createAuditsUI();
							});
						},
						{ timeout: 300 },
					);
				} else {
					// Fallback for old versions of Safari, we'll assume that things are less likely to be busy after 150ms.
					setTimeout(async () => {
						run().then(() => {
							if (showState) createAuditsUI();
						});
					}, 150);
				}
			}, 250);
		});

		setupObserver();

		document.addEventListener('astro:before-preparation', () => {
			observer.disconnect();
		});
		document.addEventListener('astro:after-swap', async () => {
			run();
		});
		document.addEventListener('astro:page-load', async () => {
			refreshLintPositions();

			// HACK: View transitions add a route announcer after this event, so we need to wait for it to be added
			setTimeout(() => {
				setupObserver();
			}, 100);
		});

		eventTarget.addEventListener('app-toggled', (event: any) => {
			if (event.detail.state === true) {
				showState = true;
				createAuditsUI();
			} else {
				showState = false;
			}
		});

		closeOnOutsideClick(eventTarget, () => {
			const activeAudits = audits.filter((audit) => audit.card?.hasAttribute('active'));

			if (activeAudits.length > 0) {
				activeAudits.forEach((audit) => {
					audit.card?.toggleAttribute('active', false);
				});
				return true;
			}

Domain

Subdomains

Frequently Asked Questions

What does default.init() do?
default.init() is a function in the astro codebase, defined in packages/astro/src/runtime/client/dev-toolbar/apps/audit/index.ts.
Where is default.init() defined?
default.init() is defined in packages/astro/src/runtime/client/dev-toolbar/apps/audit/index.ts at line 33.

Analyze Your Own Codebase

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

Try Supermodel Free