attachToJoltAttachments static method
Attaches a disposer to a Jolt object for automatic cleanup.
Parameters:
target: The object to attach the disposer todisposer: The cleanup function to execute when the object is disposed
Returns: A function that can be called to remove the disposer before disposal
The disposer will be automatically executed when the target object is garbage collected or when disposeObject is called.
Example:
final signal = Signal(0);
final cancel = JFinalizer.attachToJoltAttachments(
signal,
() => subscription.cancel(),
);
// Later, if needed:
cancel(); // Manually remove the disposer
Implementation
static Disposer attachToJoltAttachments(Object target, Disposer disposer) {
assert(
(target is JReadonlyValue || target is JEffect)
? !((target as dynamic).isDisposed)
: true,
'Jolt value is disposed');
Set<Disposer>? disposers = joltAttachments[target];
if (disposers == null) {
joltAttachments[target] = disposers = {};
joltFinalizer.attach(target, disposers);
}
disposers.add(disposer);
return () {
disposers!.remove(disposer);
};
}