smart_isolate_manager 0.0.2
SDKflutter
Platformandroidioswindowslinuxmacos
Smart and customizable isolate task manager for heavy Dart/Flutter tasks with full error handling.
Smart Isolate Manager 🚀
A powerful and easy-to-use isolate manager for running heavy Dart/Flutter tasks in the background with full error handling, timeouts, and logging support.
Features ✨
- 🏗️ Simple and intuitive API for running tasks in isolates
- ⏱️ Configurable timeouts for tasks
- 📝 Built-in logging with pretty formatting
- 🚦 Proper error handling and propagation
- 🧵 Thread-safe singleton implementation
- 🎨 Configurable logging options
- 🔄 Automatic resource cleanup
Installation 💻
Add this to your pubspec.yaml:
dependencies:
smart_isolate_manager: ^0.0.2 # use the latest version
Then run:
flutter pub get
Usage 🚀
Basic Usage
import 'package:smart_isolate_manager/smart_isolate_manager.dart';
void main() async {
// Initialize the isolate manager
final isolateManager = SmartIsolateManager();
isolateManager.initialize(
config: IsolateConfig(
enableLogging: true,
prettyLog: true,
taskTimeout: Duration(seconds: 30),
),
);
try {
// Define a task
final task = IsolateTask<int, String>(
name: 'Example Task',
argument: 42,
task: (number) async {
// Heavy computation here
await Future.delayed(Duration(seconds: 1));
return 'Processed: ${number * 2}';
},
);
// Run the task in an isolate
final result = await isolateManager.runTask(task);
print('Result: $result'); // Output: Result: Processed: 84
} catch (e) {
print('Error: $e');
}
}
Error Handling
try {
final task = IsolateTask<int, String>(
name: 'Failing Task',
argument: -1,
task: (number) {
if (number < 0) {
throw ArgumentError('Number must be positive');
}
return 'Success';
},
);
final result = await isolateManager.runTask(task);
} on IsolateTimeoutException catch (e) {
print('Task timed out: $e');
} on IsolateException catch (e) {
print('Isolate error: $e');
} catch (e, stack) {
print('Unexpected error: $e\n$stack');
}
Configuration ⚙️
IsolateConfig
| Parameter | Type | Default | Description |
|---|---|---|---|
enableLogging | bool | false | Enable/disable logging |
prettyLog | bool | false | Enable pretty-printed logs |
taskTimeout | Duration | 30s | Maximum time a task can run before timing out |
Best Practices 📝
- Keep tasks focused: Each task should do one thing well.
- Handle errors: Always wrap task execution in try-catch blocks.
- Use meaningful names: Name your tasks for better logging.
- Be mindful of memory: Large objects passed to/from isolates are copied.
- Set appropriate timeouts: Adjust timeouts based on expected task duration.
Contributing 🤝
Contributions are welcome! Please feel free to submit a Pull Request.
License 📄
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by Md Alhaz Mondal Hredhay