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

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

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

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

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

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

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

  createChild(): Child {
    this._assertNotDisposed();
    const result = (_exports.boltffi_factory_create_child as Function)(this._handle);
    return Child._fromHandle(result);
  }
}
