Home / File/ controller.ts — astro Source File

controller.ts — astro Source File

Architecture documentation for controller.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 2 imports 6 functions

Entity Profile

Dependency Diagram

graph LR
  417f2af6_8a2f_2b06_192e_b74a580ea934["controller.ts"]
  32f8c7d4_d66e_e0cf_b019_46ec3f2fea31["../core/module-loader/index.js"]
  417f2af6_8a2f_2b06_192e_b74a580ea934 --> 32f8c7d4_d66e_e0cf_b019_46ec3f2fea31
  37c43cd4_601e_8190_8560_b27e65f84596["./server-state.js"]
  417f2af6_8a2f_2b06_192e_b74a580ea934 --> 37c43cd4_601e_8190_8560_b27e65f84596
  style 417f2af6_8a2f_2b06_192e_b74a580ea934 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { LoaderEvents, ModuleLoader } from '../core/module-loader/index.js';
import type { ServerState } from './server-state.js';

import {
	clearRouteError,
	createServerState,
	setRouteError,
	setServerError,
} from './server-state.js';

type ReloadFn = () => void;

export interface DevServerController {
	state: ServerState;
	onFileChange: LoaderEvents['file-change'];
	onHMRError: LoaderEvents['hmr-error'];
}

type CreateControllerParams =
	| {
			loader: ModuleLoader;
	  }
	| {
			reload: ReloadFn;
	  };

export function createController(params: CreateControllerParams): DevServerController {
	if ('loader' in params) {
		return createLoaderController(params.loader);
	} else {
		return createBaseController(params);
	}
}

function createBaseController({ reload }: { reload: ReloadFn }): DevServerController {
	const serverState = createServerState();

	const onFileChange: LoaderEvents['file-change'] = () => {
		if (serverState.state === 'error') {
			reload();
		}
	};

	const onHMRError: LoaderEvents['hmr-error'] = (payload) => {
		let msg = payload?.err?.message ?? 'Unknown error';
		let stack = payload?.err?.stack ?? 'Unknown stack';
		let error = new Error(msg);
		Object.defineProperty(error, 'stack', {
			value: stack,
		});
		setServerError(serverState, error);
	};

	return {
		state: serverState,
		onFileChange,
		onHMRError,
	};
}

function createLoaderController(loader: ModuleLoader): DevServerController {
	const controller = createBaseController({
		reload() {
			loader.clientReload();
		},
	});
	const baseOnFileChange = controller.onFileChange;
	controller.onFileChange = (...args) => {
		if (controller.state.state === 'error') {
			// If we are in an error state, check if there are any modules with errors
			// and if so invalidate them so that they will be updated on refresh.
			loader.eachModule((mod) => {
				if (mod.ssrError) {
					loader.invalidateModule(mod);
				}
			});
		}
		baseOnFileChange(...args);
	};

	loader.events.on('file-change', controller.onFileChange);
	loader.events.on('hmr-error', controller.onHMRError);

	return controller;
}

interface RunWithErrorHandlingParams {
	controller: DevServerController;
	pathname: string;
	run: () => Promise<any>;
	onError: (error: unknown) => Error | undefined;
}

export async function runWithErrorHandling({
	controller: { state },
	pathname,
	run,
	onError,
}: RunWithErrorHandlingParams) {
	try {
		await run();
		clearRouteError(state, pathname);
	} catch (err) {
		const error = onError(err);
		setRouteError(state, pathname, error);
	}
}

Domain

Subdomains

Dependencies

  • ../core/module-loader/index.js
  • ./server-state.js

Frequently Asked Questions

What does controller.ts do?
controller.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in controller.ts?
controller.ts defines 6 function(s): Promise, createBaseController, createController, createLoaderController, error, runWithErrorHandling.
What does controller.ts depend on?
controller.ts imports 2 module(s): ../core/module-loader/index.js, ./server-state.js.
Where is controller.ts in the architecture?
controller.ts is located at packages/astro/src/vite-plugin-astro-server/controller.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-astro-server).

Analyze Your Own Codebase

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

Try Supermodel Free