mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 13:47:24 +00:00
- Replaced the main action button text with 'Explore CherryPick Documentation 🍒' instead of 'Docusaurus Tutorial'.
- Updated the button link to target /docs/intro (main docs entry point).
- Changed <Layout> props:
- Page title now uses project title only (siteConfig.title)
- Added a CherryPick-related site description for better SEO and context.
- The homepage is now tailored to reflect CherryPick's purpose as a Dart & Flutter DI library instead of Docusaurus boilerplate.
1.7 KiB
1.7 KiB
sidebar_position
| sidebar_position |
|---|
| 4 |
Disposable
CherryPick can automatically clean up any dependency that implements the Disposable interface. This makes resource management (for controllers, streams, sockets, files, etc.) easy and reliable—especially when scopes or the app are shut down.
If you bind an object implementing Disposable as a singleton or provide it via the DI container, CherryPick will call its dispose() method when the scope is closed or cleaned up.
Key Points
- Supports both synchronous and asynchronous cleanup (dispose may return
voidorFuture). - All
Disposableinstances from the current scope and subscopes will be disposed in the correct order. - Prevents resource leaks and enforces robust cleanup.
- No manual wiring needed once your class implements
Disposable.
Minimal Sync Example
class CacheManager implements Disposable {
void dispose() {
cache.clear();
print('CacheManager disposed!');
}
}
final scope = CherryPick.openRootScope();
scope.installModules([
Module((bind) => bind<CacheManager>().toProvide(() => CacheManager()).singleton()),
]);
// ...later
await CherryPick.closeRootScope(); // prints: CacheManager disposed!
Async Example
class MyServiceWithSocket implements Disposable {
@override
Future<void> dispose() async {
await socket.close();
print('Socket closed!');
}
}
scope.installModules([
Module((bind) => bind<MyServiceWithSocket>().toProvide(() => MyServiceWithSocket()).singleton()),
]);
await CherryPick.closeRootScope(); // awaits async disposal
Tip: Always call await CherryPick.closeRootScope() or await scope.closeSubScope(key) in your shutdown/teardown logic to ensure all resources are released automatically.