Home / Class/ BufferedRenderer Class — astro Architecture

BufferedRenderer Class — astro Architecture

Architecture documentation for the BufferedRenderer class in util.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  48bba3f8_c792_f2aa_c497_4f494ca2f1da["BufferedRenderer"]
  05241a8b_1820_8286_5770_4da18477ecde["util.ts"]
  48bba3f8_c792_f2aa_c497_4f494ca2f1da -->|defined in| 05241a8b_1820_8286_5770_4da18477ecde
  e2220f21_de7a_f828_41be_c7f9ef260d21["constructor()"]
  48bba3f8_c792_f2aa_c497_4f494ca2f1da -->|method| e2220f21_de7a_f828_41be_c7f9ef260d21
  8810f212_398f_b23a_f478_dead3b83984d["write()"]
  48bba3f8_c792_f2aa_c497_4f494ca2f1da -->|method| 8810f212_398f_b23a_f478_dead3b83984d
  87b072e3_880a_861b_20e6_451043e37f03["flush()"]
  48bba3f8_c792_f2aa_c497_4f494ca2f1da -->|method| 87b072e3_880a_861b_20e6_451043e37f03

Relationship Graph

Source Code

packages/astro/src/runtime/server/render/util.ts lines 191–245

class BufferedRenderer implements RenderDestination, RendererFlusher {
	private chunks: RenderDestinationChunk[] = [];
	private renderPromise: Promise<void> | void;
	private destination: RenderDestination;

	/**
	 * Determines whether buffer has been flushed
	 * to the final destination.
	 */
	private flushed = false;

	public constructor(destination: RenderDestination, renderFunction: RenderFunction) {
		this.destination = destination;
		this.renderPromise = renderFunction(this);

		if (isPromise(this.renderPromise)) {
			// Catch here in case it throws before `flush` is called,
			// to prevent an unhandled rejection.
			Promise.resolve(this.renderPromise).catch(noop);
		}
	}

	public write(chunk: RenderDestinationChunk): void {
		// Before the buffer has been flushed, we want to
		// append to the buffer, afterwards we'll write
		// to the underlying destination if subsequent
		// writes arrive.

		if (this.flushed) {
			this.destination.write(chunk);
		} else {
			this.chunks.push(chunk);
		}
	}

	public flush(): void | Promise<void> {
		if (this.flushed) {
			throw new Error('The render buffer has already been flushed.');
		}

		this.flushed = true;

		// Write the buffered chunks to the real destination
		for (const chunk of this.chunks) {
			this.destination.write(chunk);
		}

		// NOTE: We don't empty `this.chunks` after it's written as benchmarks show
		// that it causes poorer performance, likely due to forced memory re-allocation,
		// instead of letting the garbage collector handle it automatically.
		// (Unsure how this affects on limited memory machines)

		return this.renderPromise;
	}
}

Domain

Frequently Asked Questions

What is the BufferedRenderer class?
BufferedRenderer is a class in the astro codebase, defined in packages/astro/src/runtime/server/render/util.ts.
Where is BufferedRenderer defined?
BufferedRenderer is defined in packages/astro/src/runtime/server/render/util.ts at line 191.

Analyze Your Own Codebase

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

Try Supermodel Free