---
source: boltffi_bindgen/src/render/typescript/templates.rs
expression: template.render().unwrap()
---
const _RendererFinalizer: FinalizationRegistry<number> | null =
  typeof FinalizationRegistry === "undefined"
    ? null
    : new FinalizationRegistry<number>((handle) => {
        (_exports.boltffi_renderer_free as Function)(handle);
      });

export class Renderer {
  private _handle: number;
  private _disposed: boolean;

  private constructor(handle: number) {
    this._handle = handle;
    this._disposed = false;
    _RendererFinalizer?.register(this, handle, this);
  }

  static _fromHandle(handle: number): Renderer {
    if (handle === 0) {
      throw new Error("Renderer received a null handle");
    }
    return new Renderer(handle);
  }

  [Symbol.dispose](): void {
    this.dispose();
  }

  dispose(): void {
    if (this._disposed) {
      return;
    }
    this._disposed = true;
    _RendererFinalizer?.unregister(this);
    (_exports.boltffi_renderer_free as Function)(this._handle);
    this._handle = 0;
  }

  private _assertNotDisposed(): void {
    if (this._disposed) {
      throw new Error("Renderer has been disposed");
    }
  }

  draw(point: Point): void {
    this._assertNotDisposed();
    const point_writer = _module.allocWriter(PointCodec.size(point));
  PointCodec.encode(point_writer, point);
    try {
      (_exports.boltffi_renderer_draw as Function)(this._handle, point_writer.ptr, point_writer.len);
    } finally {
      _module.freeWriter(point_writer);
    }
  }
}
