Home / File/ watcher.ts — astro Source File

watcher.ts — astro Source File

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

Entity Profile

Dependency Diagram

graph LR
  a0e778e8_6c74_0340_e6e8_947338681c88["watcher.ts"]
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  a0e778e8_6c74_0340_e6e8_947338681c88 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style a0e778e8_6c74_0340_e6e8_947338681c88 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { FSWatcher } from 'vite';

type WatchEventName = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
type WatchEventCallback = (path: string) => void;

export type WrappedWatcher = FSWatcher & {
	removeAllTrackedListeners(): void;
};

// This lets us use the standard Vite FSWatcher, but also track all listeners added by the content loaders
// We do this so we can remove them all when we re-sync.
export function createWatcherWrapper(watcher: FSWatcher): WrappedWatcher {
	const listeners = new Map<WatchEventName, Set<WatchEventCallback>>();

	const handler: ProxyHandler<FSWatcher> = {
		get(target, prop, receiver) {
			// Intercept the 'on' method and track the listener
			if (prop === 'on') {
				return function (event: WatchEventName, callback: WatchEventCallback) {
					if (!listeners.has(event)) {
						listeners.set(event, new Set());
					}

					// Track the listener
					listeners.get(event)!.add(callback);

					// Call the original method
					return Reflect.get(target, prop, receiver).call(target, event, callback);
				};
			}

			// Intercept the 'off' method
			if (prop === 'off') {
				return function (event: WatchEventName, callback: WatchEventCallback) {
					// Remove from our tracking
					listeners.get(event)?.delete(callback);

					// Call the original method
					return Reflect.get(target, prop, receiver).call(target, event, callback);
				};
			}

			// Adds a function to remove all listeners added by us
			if (prop === 'removeAllTrackedListeners') {
				return function () {
					for (const [event, callbacks] of listeners.entries()) {
						for (const callback of callbacks) {
							target.off(event, callback);
						}
						callbacks.clear();
					}
					listeners.clear();
				};
			}

			// Return original method/property for everything else
			return Reflect.get(target, prop, receiver);
		},
	};

	return new Proxy(watcher, handler) as WrappedWatcher;
}

Subdomains

Dependencies

  • vite

Frequently Asked Questions

What does watcher.ts do?
watcher.ts is a source file in the astro codebase, written in typescript. It belongs to the ContentCollections domain, DataLoaders subdomain.
What functions are defined in watcher.ts?
watcher.ts defines 2 function(s): createWatcherWrapper, path.
What does watcher.ts depend on?
watcher.ts imports 1 module(s): vite.
Where is watcher.ts in the architecture?
watcher.ts is located at packages/astro/src/content/watcher.ts (domain: ContentCollections, subdomain: DataLoaders, directory: packages/astro/src/content).

Analyze Your Own Codebase

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

Try Supermodel Free