disposito
is a lightweight utility for managing the lifecycle of disposable objects in Dart, ensuring they are automatically disposed when their parent object is garbage collected.
- Automatic Disposal: Disposes bound objects when the parent object is no longer referenced
- Lifecycle Management: Coordinates cleanup with the lifecycle of parent
DisposeHolderHostMixin
- DisposeRegistry: Maintains map of existing
DisposeHolder
instances for lifecycle management
Add this to your pubspec.yaml
:
dependencies:
disposito: ^0.2.2
-
via
DisposeHolder()
constructor -
via mixin
DisposeHolderHostMixin
This mixin automatically:
- creates under the hood
DisposeHolder
and tags by original classidentityHashcode
- transforms your class into
Disposable
extending original class withdispose
method
- creates under the hood
This method automatically binds disposables to a disposeHolder
, which was created in step 1.
// Each `DisposeHolderHostMixin` extends your class into `Disposable`
class SomeCoolObject with DisposeHolderHostMixin /* implements Disposable */ {
// Bind your object using the `bindDisposable` method
late final myCoolStreamController = bindDisposable(
StreamController<int>(),
dispose: (controller) => controller.close(),
);
// After using a mixin, your class will automatically get this method and a holder under the hood
late final disposeHolder = DisposeHolder(...); // from `DisposeHolderHostMixin`
void dispose() => disposeHolder.dispose(); // from `Disposable`
}
void main() {
final object = SomeCoolObject();
myCoolStreamController.add(1); // Adds to controller `1`
/* await */ object.dispose(); // Use await if you want to wait until the disposition of all bound objects
myCoolStreamController.add(2); // Throws exception because the `myCoolStreamController` is closed
}
Contributions are welcome! Please follow these guidelines when submitting pull requests:
- Follow package
analysis_options.yaml
- Add documentation for new features
For reporting issues, please use the GitHub Issues page.