Skip to content

API Reference / @gtkx/runtime / registerClass

Function: registerClass()

registerClass<T, TProperties>(klass, options?): RegisteredClass<T, TProperties>

Defined in: packages/runtime/dist/register-class.d.ts:158

Registers a subclass of a wrapper class as a new GType, wiring up any class and interface virtual functions it overrides, both for the interfaces it inherits and for the ones RegisterClassOptions.implements names.

Throws when the class does not extend a registered wrapper class, when it has no derivable type name, when an entry in RegisterClassOptions.implements is not a registered interface, when a listed interface has a prerequisite that neither the parent type nor another listed interface meets, and when an entry in RegisterClassOptions.properties names its GObject.ParamSpec something other than the canonical spelling of the key it sits under.

A slot is filled from the vfunc-prefixed methods on the class's prototype chain, up to but not including the registered ancestor the class extends, so a method an intermediate base class declares fills a slot the same way one the class itself declares does. A slot nothing on that chain fills is left untouched.

Declare every slot as a method: a class field holding a function, such as vfuncGetNItems = () => 1, is assigned to each instance after registration and never reaches the vtable.

An override of vfuncConstructed runs from inside the base constructor, before JavaScript installs the subclass's field initializers and runs its constructor body, so a field still reads undefined there and reading a #private field throws. Declare state the override touches without an initializer, and assign private state from the constructor body after super(). An instance a native caller creates, through GObject.newv or Gtk.Builder, never runs the subclass constructor at all, so its declared fields stay uninitialized for the object's whole life.

Type Parameters

T

T extends AnyClass

TProperties

TProperties extends Record<string, object> = Record<never, object>

Parameters

klass

T

The subclass to register.

options?

RegisterClassOptions<T["prototype"], TProperties>

What the new GType gains beyond the vtable slots the class overrides.

Returns

RegisteredClass<T, TProperties>

The same class, now registered, with every name in options.properties in its property map.

Released under the MPL-2.0 License.