Home / Function/ createWatcherWrapper() — astro Function Reference

createWatcherWrapper() — astro Function Reference

Architecture documentation for the createWatcherWrapper() function in watcher.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  cca54ef6_03d3_4515_b6ac_62e00c926151["createWatcherWrapper()"]
  a0e778e8_6c74_0340_e6e8_947338681c88["watcher.ts"]
  cca54ef6_03d3_4515_b6ac_62e00c926151 -->|defined in| a0e778e8_6c74_0340_e6e8_947338681c88
  style cca54ef6_03d3_4515_b6ac_62e00c926151 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/watcher.ts lines 12–62

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

Frequently Asked Questions

What does createWatcherWrapper() do?
createWatcherWrapper() is a function in the astro codebase, defined in packages/astro/src/content/watcher.ts.
Where is createWatcherWrapper() defined?
createWatcherWrapper() is defined in packages/astro/src/content/watcher.ts at line 12.

Analyze Your Own Codebase

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

Try Supermodel Free