Skip to content

Commit

Permalink
Refactor project structure and imports
Browse files Browse the repository at this point in the history
  • Loading branch information
cezres committed Feb 1, 2024
1 parent 7bf02f3 commit 1480da4
Show file tree
Hide file tree
Showing 22 changed files with 197 additions and 77 deletions.
124 changes: 100 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,115 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
# Task Manager

For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
Task Manager is a tool for managing and scheduling task execution, designed to simplify and optimize the execution of asynchronous tasks.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.
- Supports canceling and pausing ongoing tasks
- Allows adjustment of task priority
- Enables persistent storage of task states, supporting continued execution after application interruption and restart

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.
Add the following dependencies to your `pubspec.yaml file:

```
task_manager:
git:
url: https://github.com/cezres/task_manager.git
ref: main
```

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

### Create a task

The following example demonstrates how to create a simple task:

```dart
class ExampleOperation extends Operation<int, String> {
const ExampleOperation();
@override
FutureOr<Result<int, String>> run(OperationContext<int, void> context) async {
await Future.delayed(const Duration(seconds: 1));
return Result.completed('Hello World - ${context.data}');
}
}
void example() async {
// Create a worker
final worker = Worker();
worker.maxConcurrencies = 2;
// Add a task
final task = worker.addTask(const ExampleOperation(), 1);
// Wait for the task to complete
await task.wait(); // Result.completed('Hello World - 1')
}
```

### Pause or cancel a task

For tasks in progress, you need to check if the operation should be paused or canceled, as shown below:

```dart
const like = 'sample';
class CountdownOperation extends Operation<int, void> {
const CountdownOperation();
@override
FutureOr<Result<int, void>> run(OperationContext<int, void> context) async {
int data = context.data;
while (data > 0) {
await Future.delayed(const Duration(milliseconds: 1000));
data -= 1;
/// Check if the operation should be paused or canceled
if (context.shouldPause) {
return Result.paused(data);
} else if (context.shouldCancel) {
return Result.canceled();
} else {
context.emit(data);
}
}
return Result.completed();
}
}
void example() {
task.cancel();
task.pause();
task.resume();
}
```

## Additional information
### Create hydrated task

To create a hydrated task, refer to the following code:

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
```dart
class ExampleHydratedOperation extends HydratedOperation<int, void> {
const ExampleHydratedOperation();
@override
FutureOr<Result<int, void>> run(OperationContext<int, void> context) async {
await Future.delayed(const Duration(seconds: 1));
return Result.completed('Hello World - ${context.data}');
}
@override
toJson(int data) {
return data;
}
@override
int fromJson(json) {
return json;
}
}
void example() {
StorageManager.registerStorage(CustomStorage());
StorageManager.registerOperation(() => const ExampleHydratedOperation());
}
```
55 changes: 46 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import 'package:example/countdown_operation.dart';
import 'package:example/storage/custom_storage.dart';
import 'package:example/task_manager_view.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:task_manager/task_manager.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

StorageManager.registerStorage(CustomStorage.adapter());
StorageManager.registerOperation(() => const CountdownOperation());

if (!kIsWeb) {
final directory = await getApplicationDocumentsDirectory();
debugPrint('directory: ${directory.path}');
}

runApp(const MyApp());
}

Expand Down Expand Up @@ -63,9 +54,55 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

void _showMaxConcurrenciesDialog() {
final controller =
TextEditingController(text: worker.maxConcurrencies.toString());
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Max Concurrencies"),
content: TextField(
controller: controller,
keyboardType: TextInputType.number,
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Cancel"),
),
TextButton(
onPressed: () {
final value = int.tryParse(controller.text);
if (value != null) {
worker.maxConcurrencies = value;
}
Navigator.pop(context);
},
child: const Text("OK"),
),
],
),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Task Manager Example"),
centerTitle: true,
actions: [
IconButton.outlined(
onPressed: _showMaxConcurrenciesDialog,
icon: const Icon(Icons.settings),
),
const SizedBox(
width: 24,
)
],
),
body: TaskManagerView(worker: worker),
floatingActionButton: FloatingActionButton(
onPressed: _addTasks,
Expand Down
47 changes: 27 additions & 20 deletions example/lib/task_manager_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,34 @@ class TaskManagerView extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Row(
return Column(
children: [
_buildExpandedTaskList(
context,
title: 'Running',
color: Colors.green[300],
tasks: (worker) => worker.runningTasks,
),
const VerticalDivider(width: 1, thickness: 1),
_buildExpandedTaskList(
context,
title: 'Pending',
color: Colors.orange[300],
tasks: (worker) => worker.pendingTasks,
),
const VerticalDivider(width: 1, thickness: 1),
_buildExpandedTaskList(
context,
title: 'Pasued',
color: Colors.blue[300],
tasks: (worker) => worker.pausedTasks,
const Divider(height: 1, thickness: 1),
Expanded(
child: Row(
children: [
_buildExpandedTaskList(
context,
title: 'Running',
color: Colors.green[300],
tasks: (worker) => worker.runningTasks,
),
const VerticalDivider(width: 1, thickness: 1),
_buildExpandedTaskList(
context,
title: 'Pending',
color: Colors.orange[300],
tasks: (worker) => worker.pendingTasks,
),
const VerticalDivider(width: 1, thickness: 1),
_buildExpandedTaskList(
context,
title: 'Pasued',
color: Colors.blue[300],
tasks: (worker) => worker.pausedTasks,
),
],
),
),
],
);
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

class IsolateOperationContextImpl<D, R> extends OperationContextImpl<D, R> {
IsolateOperationContextImpl({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

class OperationContextImpl<D, R> extends OperationContext<D, R> {
OperationContextImpl({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

typedef TaskIdentifier = String;
typedef TaskId = String;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

final Map<String, WorkerImpl> _workers = {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

class IsolateWorker extends WorkerImpl {
IsolateWorker() : super._('IsolateWorker');
Expand Down
2 changes: 1 addition & 1 deletion lib/storage/storage.dart → lib/src/storage/storage.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

abstract class Storage {
const Storage();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

typedef OperationCreater<T extends HydratedOperation> = T Function();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

class HydratedTaskImpl<D, R, O extends HydratedOperation<D, R>>
extends TaskImpl<D, R, O> {
Expand Down
2 changes: 1 addition & 1 deletion lib/task/result.dart → lib/src/task/result.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

class Result<D, R> {
final D? data;
Expand Down
2 changes: 1 addition & 1 deletion lib/task/task_impl.dart → lib/src/task/task_impl.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../task_manager.dart';
part of '../../task_manager.dart';

class TaskImpl<D, R, O extends Operation<D, R>> extends Task<D, R>
with PriorityMixin {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 13 additions & 13 deletions lib/task_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import 'dart:async';
import 'dart:isolate';

import 'package:flutter/foundation.dart';
import 'package:task_manager/task/task_priority.dart';
import 'package:task_manager/utils/generate_incremental_id.dart';
import 'package:task_manager/utils/priority_queue.dart';
import 'package:task_manager/src/task/task_priority.dart';
import 'package:task_manager/src/utils/generate_incremental_id.dart';
import 'package:task_manager/src/utils/priority_queue.dart';

part 'task/task_impl.dart';
part 'task/hydrated_task_impl.dart';
part 'task/result.dart';
part 'src/task/task_impl.dart';
part 'src/task/hydrated_task_impl.dart';
part 'src/task/result.dart';

part 'operation/operation_context_impl.dart';
part 'operation/isolate_operation_context_impl.dart';
part 'src/operation/operation_context_impl.dart';
part 'src/operation/isolate_operation_context_impl.dart';

part 'scheduling/scheduler.dart';
part 'scheduling/worker.dart';
part 'scheduling/worker_isolate.dart';
part 'src/scheduling/scheduler.dart';
part 'src/scheduling/worker.dart';
part 'src/scheduling/worker_isolate.dart';

part 'storage/storage.dart';
part 'storage/storage_manager.dart';
part 'src/storage/storage.dart';
part 'src/storage/storage_manager.dart';

abstract class OperationContext<D, R> {
String get id;
Expand Down
2 changes: 1 addition & 1 deletion test/priority_queue_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:task_manager/utils/priority_queue.dart';
import 'package:task_manager/src/utils/priority_queue.dart';

void main() {
test('priority_queue', () {
Expand Down

0 comments on commit 1480da4

Please sign in to comment.