Skip to content

API Reference / @gtkx/runtime / registerClass

Function: registerClass()

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

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

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 or the name is not a valid GType 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, when the list names Gio.AsyncInitable as an interface the parent type does not already implement and no method on the chain fills vfuncInitAsync, since the default init_async would run vfuncInit on a worker thread, when an entry in RegisterClassOptions.properties names its GObject.ParamSpec something the key it sits under does not spell, when an entry in RegisterClassOptions.signals carries an invalid name, a name spelled with an uppercase letter rather than dashed, a name the type already knows, a GType that cannot hold a value, or an accumulator the spec does not admit, and when RegisterClassOptions.cssName is given for a class that does not extend Gtk.Widget. An exception thrown by RegisterClassOptions.classInit also propagates, after the type has already been registered.

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.

A method named on<SignalName> — the signal's name in camelCase after the on, so onClicked for clicked and onItemsChanged for items-changed — becomes that signal's default handler when the type carries the signal, whether an ancestor type or an implemented interface brings it or RegisterClassOptions.signals declares it. The method is installed as a class-closure override, so it runs on every emission, on the instances a native caller creates included, in the stage the signal's flags name rather than alongside connected handlers. It receives the emission's arguments without the leading emitter, with this bound to the emitter, and what it returns becomes the emission's result when the signal declares one. The same discovery walks the prototype chain vfunc discovery walks, and a subclass registering its own on<SignalName> replaces the handler for its instances, where super.on<SignalName>() reaches the replaced one. An on-prefixed method naming no signal the type carries is left alone as the ordinary method it is.

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>

TSignals

TSignals extends Record<string, SignalSpec> = Record<never, SignalSpec>

Parameters

klass

T

The subclass to register.

options?

RegisterClassOptions<T["prototype"], TProperties, TSignals>

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

Returns

RegisteredClass<T, TProperties, TSignals>

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

Released under the MPL-2.0 License.