disposito 0.2.3
disposito: ^0.2.3 copied to clipboard
Semi-automatic dispose holder that allows centralised disposal of objects
disposito - Semi-Automatic disposal system for Dart #
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.
Key Concepts #
- 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
DisposeHolderinstances for lifecycle management
Installation #
Add this to your pubspec.yaml:
dependencies:
disposito: ^0.2.2
Quickstart #
1. Create disposeHolder:
-
via
DisposeHolder()constructor -
via mixin
DisposeHolderHostMixinThis mixin automatically:
- creates under the hood
DisposeHolderand tags by original classidentityHashcode - transforms your class into
Disposableextending original class withdisposemethod
- creates under the hood
2. Bind each disposable object using bindDisposable
This method automatically binds disposables to a disposeHolder, which was created in step 1.
Usage Example #
DisposeHolderHost Object declaration #
// 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`
}
Somewhere in code #
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
}
Contributing #
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.