A GC finalizer registry that invokes a callback when a watched object is garbage-collected. Modeled after JavaScript's FinalizationRegistry.

The callback receives a held value (not the collected object itself), which is safe because the object may already be in an invalid state.

Not all targets support GC finalizers. On unsupported targets, the constructor throws NotImplementedException.

Constructor

new(callback:T ‑> Void)

Creates a new GcFinalizer with the given cleanup callback. The callback will be invoked with the held value when a registered target object is garbage-collected.

Methods

register(target:{}, heldValue:T):IHandle

Registers target for clean-up. When target is garbage-collected, the callback will be invoked with heldValue.

Returns an IHandle handle. Calling close() on the handle cancels the registration, preventing the callback from firing.