smart_isolate_manager 0.0.2
smart_isolate_manager: ^0.0.2 copied to clipboard
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